From aa8434354377355d84de09378288d9e5b87c84e1 Mon Sep 17 00:00:00 2001 From: Kumulo Date: Mon, 29 Mar 2021 15:17:41 +0200 Subject: [PATCH 1/3] Makes bundle templates and translations directory depending of bundle configuration, not Kernel --- .../FrameworkExtension.php | 2 +- .../FrameworkExtensionTest.php | 8 ++--- .../CustomBundle/config/serialization.yaml | 4 +++ .../CustomBundle/config/validation.yaml | 5 +++ .../custom/templates/index.html.twig | 1 + .../custom/translations/custom.en.yaml | 1 + .../Bundle/CustomBundle/public/custom.css | 0 .../Bundle/CustomBundle/src/CustomBundle.php | 32 +++++++++++++++++++ .../CustomBundle/src/Entity/CustomPerson.php | 24 ++++++++++++++ .../Tests/Functional/BundlePathsTest.php | 5 +++ .../Functional/app/BundlePaths/bundles.php | 2 ++ .../DependencyInjection/TwigExtension.php | 2 +- .../DependencyInjection/TwigExtensionTest.php | 2 ++ .../Component/HttpKernel/Bundle/Bundle.php | 16 ++++++++++ .../HttpKernel/Bundle/BundleInterface.php | 18 +++++++++++ src/Symfony/Component/HttpKernel/Kernel.php | 2 ++ 16 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/serialization.yaml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/validation.yaml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/templates/index.html.twig create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/translations/custom.en.yaml create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/public/custom.css create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/CustomBundle.php create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/Entity/CustomPerson.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 4b3e4427a0cba..806c90f5ce6a7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1251,7 +1251,7 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder } $defaultDir = $container->getParameterBag()->resolveValue($config['default_path']); foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) { - if ($container->fileExists($dir = $bundle['path'].'/Resources/translations') || $container->fileExists($dir = $bundle['path'].'/translations')) { + if ($container->fileExists($dir = $bundle['translations_path']) || $container->fileExists($dir = $bundle['path'].'/Resources/translations') || $container->fileExists($dir = $bundle['path'].'/translations')) { $dirs[] = $dir; } else { $nonExistingDirs[] = $dir; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 9d5d1aa9fb544..67fd6c1f2c79d 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -1016,7 +1016,7 @@ public function testValidationPaths() $container = $this->createContainerFromFile('validation_annotations', [ 'kernel.bundles' => ['TestBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\TestBundle'], - 'kernel.bundles_metadata' => ['TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle']], + 'kernel.bundles_metadata' => ['TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle', 'templates_path' => __DIR__.'/Fixtures/TestBundle/templates', 'translations_path' => __DIR__.'/Fixtures/TestBundle/translations']], ]); $calls = $container->getDefinition('validator.builder')->getMethodCalls(); @@ -1053,7 +1053,7 @@ public function testValidationPathsUsingCustomBundlePath() $container = $this->createContainerFromFile('validation_annotations', [ 'kernel.bundles' => ['CustomPathBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\CustomPathBundle'], - 'kernel.bundles_metadata' => ['TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/CustomPathBundle']], + 'kernel.bundles_metadata' => ['TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/CustomPathBundle', 'templates_path' => __DIR__.'/Fixtures/CustomPathBundle/templates', 'translations_path' => __DIR__.'/Fixtures/CustomPathBundle/translations']], ]); $calls = $container->getDefinition('validator.builder')->getMethodCalls(); @@ -1307,7 +1307,7 @@ public function testSerializerCacheActivatedDebug() public function testSerializerMapping() { - $container = $this->createContainerFromFile('serializer_mapping', ['kernel.bundles_metadata' => ['TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle']]]); + $container = $this->createContainerFromFile('serializer_mapping', ['kernel.bundles_metadata' => ['TestBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'path' => __DIR__.'/Fixtures/TestBundle', 'templates_path' => __DIR__.'/Fixtures/TestBundle/templates', 'translations_path' => __DIR__.'/Fixtures/TestBundle/translations']]]); $projectDir = $container->getParameter('kernel.project_dir'); $configDir = __DIR__.'/Fixtures/TestBundle/Resources/config'; $expectedLoaders = [ @@ -1729,7 +1729,7 @@ protected function createContainer(array $data = []) { return new ContainerBuilder(new EnvPlaceholderParameterBag(array_merge([ 'kernel.bundles' => ['FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'], - 'kernel.bundles_metadata' => ['FrameworkBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..']], + 'kernel.bundles_metadata' => ['FrameworkBundle' => ['namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..', 'templates_path' => __DIR__.'/../../templates', 'translations_path' => __DIR__.'/../../translations']], 'kernel.cache_dir' => __DIR__, 'kernel.build_dir' => __DIR__, 'kernel.project_dir' => __DIR__, diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/serialization.yaml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/serialization.yaml new file mode 100644 index 0000000000000..53422d1376956 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/serialization.yaml @@ -0,0 +1,4 @@ +Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\CustomBundle\src\Entity\CustomPerson: + attributes: + name: + serialized_name: 'full_name' diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/validation.yaml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/validation.yaml new file mode 100644 index 0000000000000..b73ac945c79df --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/config/validation.yaml @@ -0,0 +1,5 @@ +Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\CustomBundle\src\Entity\CustomPerson: + properties: + age: + - GreaterThan: + value: 18 diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/templates/index.html.twig b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/templates/index.html.twig new file mode 100644 index 0000000000000..d86bac9de59ab --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/templates/index.html.twig @@ -0,0 +1 @@ +OK diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/translations/custom.en.yaml b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/translations/custom.en.yaml new file mode 100644 index 0000000000000..1860a9d6340b6 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/custom/translations/custom.en.yaml @@ -0,0 +1 @@ +ok_label: OK diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/public/custom.css b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/public/custom.css new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/CustomBundle.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/CustomBundle.php new file mode 100644 index 0000000000000..cbb0209ad2103 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/CustomBundle.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\CustomBundle\src; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +class CustomBundle extends Bundle +{ + public function getPath(): string + { + return \dirname(__DIR__); + } + + public function getTemplatesPath() + { + return $this->getPath() . '/custom/templates'; + } + + public function getTranslationsPath() + { + return $this->getPath() . '/custom/translations'; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/Entity/CustomPerson.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/Entity/CustomPerson.php new file mode 100644 index 0000000000000..7212e4229f3c1 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/CustomBundle/src/Entity/CustomPerson.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\CustomBundle\src\Entity; + +class CustomPerson +{ + public $name; + public $age; + + public function __construct(string $name, string $age) + { + $this->name = $name; + $this->age = $age; + } +} diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php index a079837c9e336..a122f7e0d5335 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/BundlePathsTest.php @@ -31,6 +31,7 @@ public function testBundlePublicDir() $exitCode = (new CommandTester($command))->execute(['target' => $projectDir.'/public']); $this->assertSame(0, $exitCode); + $this->assertFileExists($projectDir.'/public/bundles/custom/custom.css'); $this->assertFileExists($projectDir.'/public/bundles/modern/modern.css'); $this->assertFileExists($projectDir.'/public/bundles/legacy/legacy.css'); @@ -48,6 +49,9 @@ public function testBundleTwigTemplatesDir() $this->assertSame([$bundlesMetadata['ModernBundle']['path'].'/templates'], $twig->getLoader()->getPaths('Modern')); $this->assertSame("OK\n", $twig->render('@Modern/index.html.twig')); + + $this->assertSame([$bundlesMetadata['CustomBundle']['templates_path']], $twig->getLoader()->getPaths('Custom')); + $this->assertSame("OK\n", $twig->render('@Custom/index.html.twig')); } public function testBundleTranslationsDir() @@ -57,6 +61,7 @@ public function testBundleTranslationsDir() $this->assertSame('OK', $translator->trans('ok_label', [], 'legacy')); $this->assertSame('OK', $translator->trans('ok_label', [], 'modern')); + $this->assertSame('OK', $translator->trans('ok_label', [], 'custom')); } public function testBundleValidationConfigDir() diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/BundlePaths/bundles.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/BundlePaths/bundles.php index 7e01199ea7909..bfe22aa315841 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/BundlePaths/bundles.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/BundlePaths/bundles.php @@ -12,11 +12,13 @@ use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\LegacyBundle\LegacyBundle; use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\ModernBundle\src\ModernBundle; +use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\CustomBundle\src\CustomBundle; use Symfony\Bundle\TwigBundle\TwigBundle; return [ new FrameworkBundle(), new TwigBundle(), + new CustomBundle(), new ModernBundle(), new LegacyBundle(), ]; diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php index 20095eb45a79d..e0e6278f6db77 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php @@ -162,7 +162,7 @@ private function getBundleTemplatePaths(ContainerBuilder $container, array $conf } $container->addResource(new FileExistenceResource($defaultOverrideBundlePath)); - if (file_exists($dir = $bundle['path'].'/Resources/views') || file_exists($dir = $bundle['path'].'/templates')) { + if (file_exists($dir = $bundle['templates_path']) || file_exists($dir = $bundle['path'].'/Resources/views') || file_exists($dir = $bundle['path'].'/templates')) { $bundleHierarchy[$name][] = $dir; } $container->addResource(new FileExistenceResource($dir)); diff --git a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php index 12eaa4ba4c41b..3a2963c9a4b7c 100644 --- a/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php +++ b/src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php @@ -278,6 +278,8 @@ private function createContainer() 'AcmeBundle' => [ 'namespace' => 'Symfony\Bundle\TwigBundle\Tests\DependencyInjection\AcmeBundle', 'path' => __DIR__.'/AcmeBundle', + 'templates_path' => __DIR__.'/AcmeBundle/templates', + 'translations_path' => __DIR__.'/AcmeBundle/translations', ], ], ])); diff --git a/src/Symfony/Component/HttpKernel/Bundle/Bundle.php b/src/Symfony/Component/HttpKernel/Bundle/Bundle.php index 2e65f67c9db28..56dd5158b7acb 100644 --- a/src/Symfony/Component/HttpKernel/Bundle/Bundle.php +++ b/src/Symfony/Component/HttpKernel/Bundle/Bundle.php @@ -114,6 +114,22 @@ public function getPath() return $this->path; } + /** + * {@inheritdoc} + */ + public function getTranslationsPath() + { + return $this->getPath() . '/translations'; + } + + /** + * {@inheritdoc} + */ + public function getTemplatesPath() + { + return $this->getPath() . '/templates'; + } + /** * Returns the bundle name (the class short name). */ diff --git a/src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php b/src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php index 88a95d8332942..bef61512771ff 100644 --- a/src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php +++ b/src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php @@ -68,4 +68,22 @@ public function getNamespace(); * @return string The Bundle absolute path */ public function getPath(); + + /** + * Gets the Bundle template path. + * + * The path should always be returned as a Unix path (with /). + * + * @return string The Bundle template absolute path + */ + public function getTemplatesPath(); + + /** + * Gets the Bundle translation directory path. + * + * The path should always be returned as a Unix path (with /). + * + * @return string The Bundle translations absolute path + */ + public function getTranslationsPath(); } diff --git a/src/Symfony/Component/HttpKernel/Kernel.php b/src/Symfony/Component/HttpKernel/Kernel.php index dbe5a111dae2f..ff65b78528edc 100644 --- a/src/Symfony/Component/HttpKernel/Kernel.php +++ b/src/Symfony/Component/HttpKernel/Kernel.php @@ -600,6 +600,8 @@ protected function getKernelParameters() $bundlesMetadata[$name] = [ 'path' => $bundle->getPath(), 'namespace' => $bundle->getNamespace(), + 'translations_path' => $bundle->getTranslationsPath(), + 'templates_path' => $bundle->getTemplatesPath(), ]; } From a0e27041b1f8c4925dfeaf3e39fdb08d3f667093 Mon Sep 17 00:00:00 2001 From: Kumulo Date: Mon, 29 Mar 2021 15:33:37 +0200 Subject: [PATCH 2/3] Changelog --- src/Symfony/Component/HttpKernel/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Symfony/Component/HttpKernel/CHANGELOG.md b/src/Symfony/Component/HttpKernel/CHANGELOG.md index 6d310de13e81b..ab55740faedd0 100644 --- a/src/Symfony/Component/HttpKernel/CHANGELOG.md +++ b/src/Symfony/Component/HttpKernel/CHANGELOG.md @@ -12,6 +12,7 @@ CHANGELOG * Deprecate `HttpKernelInterface::MASTER_REQUEST` and add `HttpKernelInterface::MAIN_REQUEST` as replacement * Deprecate `KernelEvent::isMasterRequest()` and add `isMainRequest()` as replacement * Add `#[AsController]` attribute for declaring standalone controllers on PHP 8 + * Add templates and translations paths definitions to `Symfony\Component\HttpKernel\Bundle\Bundle` 5.2.0 ----- From db1b6aad294f9494da134b8b7352493ae61deb1a Mon Sep 17 00:00:00 2001 From: Kumulo Date: Mon, 29 Mar 2021 15:34:39 +0200 Subject: [PATCH 3/3] Bundles Changelog --- src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md | 1 + src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md index 17f36c33c8615..3a8868fc98003 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md @@ -18,6 +18,7 @@ CHANGELOG * Deprecate all other values than "none", "php_array" and "file" for `framework.annotation.cache` * Add `KernelTestCase::getContainer()` as the best way to get a container in tests * Rename the container parameter `profiler_listener.only_master_requests` to `profiler_listener.only_main_requests` + * Add templates and translations paths definitions to `Symfony\Component\HttpKernel\Bundle\Bundle` 5.2.0 ----- diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index b1642ea1af00b..404bcd5d4039e 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * Added support for the new `serialize` filter (from Twig Bridge) +* Add templates and translations paths definitions to `Symfony\Component\HttpKernel\Bundle\Bundle` 5.2.0 -----