Skip to content

Commit 99c7962

Browse files
committed
phpcs
1 parent ecb6c82 commit 99c7962

File tree

6 files changed

+90
-6
lines changed

6 files changed

+90
-6
lines changed

src/Chartjs/src/DependencyInjection/ChartjsExtension.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
use Symfony\UX\Chartjs\Builder\ChartBuilder;
2121
use Symfony\UX\Chartjs\Builder\ChartBuilderInterface;
2222
use Symfony\UX\Chartjs\Twig\ChartExtension;
23-
use Symfony\UX\StimulusBundle\Dto\StimulusAttributes;
24-
use Twig\Environment;
2523

2624
/**
2725
* @author Titouan Galopin <galopintitouan@gmail.com>

src/Chartjs/tests/ChartjsBundleTest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
1212
namespace Symfony\UX\Chartjs\Tests;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\HttpKernel\Kernel;
16-
use Symfony\UX\Chartjs\Tests\Kernel\EmptyAppKernel;
17-
use Symfony\UX\Chartjs\Tests\Kernel\FrameworkAppKernel;
1815
use Symfony\UX\Chartjs\Tests\Kernel\TwigAppKernel;
1916

2017
/**

src/Notify/src/Twig/NotifyRuntime.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
use Symfony\Component\Mercure\HubInterface;
1515
use Symfony\UX\StimulusBundle\Helper\StimulusHelper;
16-
use Twig\Environment;
1716
use Twig\Extension\RuntimeExtensionInterface;
1817

1918
/**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// This file is dynamically rewritten by StimulusBundle + AssetMapper.
2+
/** @type {Object<string, Controller>} */
3+
export const eagerControllers = {};
4+
/** @type {Object<string, string>} */
5+
6+
/** @type {Object<string, () => Promise<{default: Controller}>>} */
7+
export const lazyControllers = {};
8+
9+
export const isApplicationDebug = false;

src/StimulusBundle/assets/loader.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Starts the Stimulus application and reads a map dump in the DOM to load controllers.
3+
*
4+
* Inspired by stimulus-loading.js from stimulus-rails.
5+
*/
6+
import { Application } from '@hotwired/stimulus';
7+
import { eagerControllers, lazyControllers, isApplicationDebug } from './controllers.js';
8+
9+
const controllerAttribute = 'data-controller';
10+
11+
export const loadControllers = (application) => {
12+
// loop over the controllers map and require each controller
13+
for (const name in eagerControllers) {
14+
registerController(name, eagerControllers[name], application);
15+
}
16+
17+
loadLazyControllers(application);
18+
};
19+
20+
export const startStimulusApp = () => {
21+
const application = Application.start();
22+
application.debug = isApplicationDebug;
23+
24+
loadControllers(application);
25+
26+
return application;
27+
};
28+
29+
function registerController(name, controller, application) {
30+
if (canRegisterController(name, application)) {
31+
application.register(name, controller)
32+
}
33+
}
34+
35+
function loadLazyControllers(application) {
36+
lazyLoadExistingControllers(application, document);
37+
lazyLoadNewControllers(application, document)
38+
}
39+
40+
function lazyLoadExistingControllers(application, element) {
41+
queryControllerNamesWithin(element).forEach(controllerName => loadController(controllerName, application))
42+
}
43+
function queryControllerNamesWithin(element) {
44+
return Array.from(element.querySelectorAll(`[${controllerAttribute}]`)).map(extractControllerNamesFrom).flat()
45+
}
46+
function extractControllerNamesFrom(element) {
47+
return element.getAttribute(controllerAttribute).split(/\s+/).filter(content => content.length)
48+
}
49+
function lazyLoadNewControllers(application, element) {
50+
new MutationObserver((mutationsList) => {
51+
for (const { attributeName, target, type } of mutationsList) {
52+
switch (type) {
53+
case 'attributes': {
54+
if (attributeName === controllerAttribute && target.getAttribute(controllerAttribute)) {
55+
extractControllerNamesFrom(target).forEach(controllerName => loadController(controllerName, application))
56+
}
57+
}
58+
59+
case 'childList': {
60+
lazyLoadExistingControllers(application, target)
61+
}
62+
}
63+
}
64+
}).observe(element, { attributeFilter: [controllerAttribute], subtree: true, childList: true })
65+
}
66+
function canRegisterController(name, application){
67+
return !application.router.modulesByIdentifier.has(name)
68+
}
69+
70+
async function loadController(name, application) {
71+
if (canRegisterController(name, application)) {
72+
if (lazyControllers[name] === undefined) {
73+
console.error(`Failed to autoload controller: ${name}`);
74+
}
75+
76+
const controllerModule = await (lazyControllers[name]());
77+
78+
registerController(name, controllerModule.default, application);
79+
}
80+
}

src/StimulusBundle/config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Symfony\UX\StimulusBundle\Twig\StimulusTwigExtension;
1010
use Symfony\UX\StimulusBundle\Ux\UxPackageReader;
1111
use Twig\Environment;
12+
1213
use function Symfony\Component\DependencyInjection\Loader\Configurator\abstract_arg;
1314
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
1415
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;

0 commit comments

Comments
 (0)