Skip to content

Commit 45877a0

Browse files
committed
bug #953 [StimulusBundle] Fix controller name conversion (jon-ht)
This PR was merged into the 2.x branch. Discussion ---------- [StimulusBundle] Fix controller name conversion | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Tickets | N/A | License | MIT <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/releases): - Always add tests and ensure they pass. - Never break backward compatibility (see https://symfony.com/bc). - Features and deprecations must be submitted against branch main. --> Stimulus controllers must follow some naming convention/normalization according to https://stimulus.hotwired.dev/reference/controllers#identifiers There was a case when controllers file with underscores in their name would not be a valid entry for Stimulus. I wasn't sure about using `Symfony\UX\StimulusBundle\Dto\StimulusAttributes::normalizeControllerName` because it seems related to Twig (due to its `private Environment $env` in construct) Commits ------- 9368ef2 [StimulusBundle] Fix controller name conversion
2 parents 46d885d + 9368ef2 commit 45877a0

File tree

8 files changed

+40
-9
lines changed

8 files changed

+40
-9
lines changed

src/StimulusBundle/src/AssetMapper/ControllersMapGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ private function loadCustomControllers(): array
6262
foreach ($finder as $file) {
6363
$name = $file->getRelativePathname();
6464
$name = str_replace(['_controller.js', '-controller.js'], '', $name);
65-
$name = str_replace('/', '--', $name);
65+
$name = str_replace(['_', '/'], ['-', '--'], $name);
6666

6767
$asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath());
6868
$isLazy = preg_match('/\/\*\s*stimulusFetch:\s*\'lazy\'\s*\*\//i', $asset->content);

src/StimulusBundle/tests/AssetMapper/ControllerMapGeneratorTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,21 @@ public function testGetControllersMap()
5454
$map = $generator->getControllersMap();
5555
// + 3 controller.json UX controllers
5656
// - 1 controllers.json UX controller is disabled
57-
// + 4 custom controllers (1 file is not a controller & 1 is overridden)
58-
$this->assertCount(6, $map);
57+
// + 8 custom controllers (1 file is not a controller & 1 is overridden)
58+
$this->assertCount(10, $map);
5959
$packageNames = array_keys($map);
6060
sort($packageNames);
6161
$this->assertSame([
6262
'bye',
6363
'fake-vendor--ux-package1--controller-second',
6464
'fake-vendor--ux-package2--hello-controller',
6565
'hello',
66+
'hello-with-dashes',
67+
'hello-with-underscores',
6668
'other',
6769
'subdir--deeper',
70+
'subdir--deeper-with-dashes',
71+
'subdir--deeper-with-underscores',
6872
], $packageNames);
6973

7074
$controllerSecond = $map['fake-vendor--ux-package1--controller-second'];

src/StimulusBundle/tests/AssetMapper/StimulusControllerLoaderFunctionalTest.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@ public function testFullApplicationLoad()
4040
$this->assertSame([
4141
// 1x import from loader.js (which is aliased to @symfony/stimulus-bundle via importmap)
4242
'/assets/@symfony/stimulus-bundle/controllers.js',
43-
// 2x from "controllers" (hello is overridden)
43+
// 6x from "controllers" (hello is overridden)
4444
'/assets/controllers/bye_controller.js',
45+
'/assets/controllers/hello-with-dashes-controller.js',
46+
'/assets/controllers/hello_with_underscores-controller.js',
4547
'/assets/controllers/subdir/deeper-controller.js',
48+
'/assets/controllers/subdir/deeper-with-dashes-controller.js',
49+
'/assets/controllers/subdir/deeper_with_underscores-controller.js',
4650
// 2x from UX packages, which are enabled in controllers.json
4751
'/assets/fake-vendor/ux-package1/package-controller-second.js',
4852
'/assets/fake-vendor/ux-package2/package-hello-controller.js',
@@ -61,14 +65,18 @@ public function testFullApplicationLoad()
6165
$preLoadHrefs = $crawler->filter('link[rel="modulepreload"]')->each(function ($link) {
6266
return $link->attr('href');
6367
});
64-
$this->assertCount(6, $preLoadHrefs);
68+
$this->assertCount(10, $preLoadHrefs);
6569
sort($preLoadHrefs);
6670
$this->assertStringStartsWith('/assets/@symfony/stimulus-bundle/controllers-', $preLoadHrefs[0]);
6771
$this->assertStringStartsWith('/assets/@symfony/stimulus-bundle/loader-', $preLoadHrefs[1]);
6872
$this->assertStringStartsWith('/assets/app-', $preLoadHrefs[2]);
69-
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-controller-', $preLoadHrefs[3]);
70-
$this->assertStringStartsWith('/assets/fake-vendor/ux-package2/package-hello-controller-', $preLoadHrefs[4]);
71-
$this->assertStringStartsWith('/assets/more-controllers/hello-controller-', $preLoadHrefs[5]);
73+
$this->assertStringStartsWith('/assets/controllers/hello-with-dashes-controller-', $preLoadHrefs[3]);
74+
$this->assertStringStartsWith('/assets/controllers/hello_with_underscores-controller-', $preLoadHrefs[4]);
75+
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-controller-', $preLoadHrefs[5]);
76+
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-with-dashes-controller-', $preLoadHrefs[6]);
77+
$this->assertStringStartsWith('/assets/controllers/subdir/deeper_with_underscores-controller-', $preLoadHrefs[7]);
78+
$this->assertStringStartsWith('/assets/fake-vendor/ux-package2/package-hello-controller-', $preLoadHrefs[8]);
79+
$this->assertStringStartsWith('/assets/more-controllers/hello-controller-', $preLoadHrefs[9]);
7280
}
7381

7482
protected static function getKernelClass(): string

src/StimulusBundle/tests/fixtures/assets/controllers/hello-controller.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// hello-controller.js
2-
32
import { Controller } from '@hotwired/stimulus';
43

54
export default class extends Controller {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// hello--with-dashes-controller.js
2+
import { Controller } from '@hotwired/stimulus';
3+
4+
export default class extends Controller {
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// hello_with_underscores-controller.js
2+
import { Controller } from '@hotwired/stimulus';
3+
4+
export default class extends Controller {
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// subdir/deeper-with-dashes-controller.js
2+
import { Controller } from '@hotwired/stimulus';
3+
4+
export default class extends Controller {
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// subdir/deeper_with_underscores-controller.js
2+
import { Controller } from '@hotwired/stimulus';
3+
4+
export default class extends Controller {
5+
}

0 commit comments

Comments
 (0)