Skip to content

[StimulusBundle] Fix controller name conversion #953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function loadCustomControllers(): array
foreach ($finder as $file) {
$name = $file->getRelativePathname();
$name = str_replace(['_controller.js', '-controller.js'], '', $name);
$name = str_replace('/', '--', $name);
$name = str_replace(['_', '/'], ['-', '--'], $name);

$asset = $this->assetMapper->getAssetFromSourcePath($file->getRealPath());
$isLazy = preg_match('/\/\*\s*stimulusFetch:\s*\'lazy\'\s*\*\//i', $asset->content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,21 @@ public function testGetControllersMap()
$map = $generator->getControllersMap();
// + 3 controller.json UX controllers
// - 1 controllers.json UX controller is disabled
// + 4 custom controllers (1 file is not a controller & 1 is overridden)
$this->assertCount(6, $map);
// + 8 custom controllers (1 file is not a controller & 1 is overridden)
$this->assertCount(10, $map);
$packageNames = array_keys($map);
sort($packageNames);
$this->assertSame([
'bye',
'fake-vendor--ux-package1--controller-second',
'fake-vendor--ux-package2--hello-controller',
'hello',
'hello-with-dashes',
'hello-with-underscores',
'other',
'subdir--deeper',
'subdir--deeper-with-dashes',
'subdir--deeper-with-underscores',
], $packageNames);

$controllerSecond = $map['fake-vendor--ux-package1--controller-second'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ public function testFullApplicationLoad()
$this->assertSame([
// 1x import from loader.js (which is aliased to @symfony/stimulus-bundle via importmap)
'/assets/@symfony/stimulus-bundle/controllers.js',
// 2x from "controllers" (hello is overridden)
// 6x from "controllers" (hello is overridden)
'/assets/controllers/bye_controller.js',
'/assets/controllers/hello-with-dashes-controller.js',
'/assets/controllers/hello_with_underscores-controller.js',
'/assets/controllers/subdir/deeper-controller.js',
'/assets/controllers/subdir/deeper-with-dashes-controller.js',
'/assets/controllers/subdir/deeper_with_underscores-controller.js',
// 2x from UX packages, which are enabled in controllers.json
'/assets/fake-vendor/ux-package1/package-controller-second.js',
'/assets/fake-vendor/ux-package2/package-hello-controller.js',
Expand All @@ -61,14 +65,18 @@ public function testFullApplicationLoad()
$preLoadHrefs = $crawler->filter('link[rel="modulepreload"]')->each(function ($link) {
return $link->attr('href');
});
$this->assertCount(6, $preLoadHrefs);
$this->assertCount(10, $preLoadHrefs);
sort($preLoadHrefs);
$this->assertStringStartsWith('/assets/@symfony/stimulus-bundle/controllers-', $preLoadHrefs[0]);
$this->assertStringStartsWith('/assets/@symfony/stimulus-bundle/loader-', $preLoadHrefs[1]);
$this->assertStringStartsWith('/assets/app-', $preLoadHrefs[2]);
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-controller-', $preLoadHrefs[3]);
$this->assertStringStartsWith('/assets/fake-vendor/ux-package2/package-hello-controller-', $preLoadHrefs[4]);
$this->assertStringStartsWith('/assets/more-controllers/hello-controller-', $preLoadHrefs[5]);
$this->assertStringStartsWith('/assets/controllers/hello-with-dashes-controller-', $preLoadHrefs[3]);
$this->assertStringStartsWith('/assets/controllers/hello_with_underscores-controller-', $preLoadHrefs[4]);
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-controller-', $preLoadHrefs[5]);
$this->assertStringStartsWith('/assets/controllers/subdir/deeper-with-dashes-controller-', $preLoadHrefs[6]);
$this->assertStringStartsWith('/assets/controllers/subdir/deeper_with_underscores-controller-', $preLoadHrefs[7]);
$this->assertStringStartsWith('/assets/fake-vendor/ux-package2/package-hello-controller-', $preLoadHrefs[8]);
$this->assertStringStartsWith('/assets/more-controllers/hello-controller-', $preLoadHrefs[9]);
}

protected static function getKernelClass(): string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// hello-controller.js

import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello--with-dashes-controller.js
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// hello_with_underscores-controller.js
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// subdir/deeper-with-dashes-controller.js
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// subdir/deeper_with_underscores-controller.js
import { Controller } from '@hotwired/stimulus';

export default class extends Controller {
}