Skip to content

[FrameworkBundle][HttpKernel][TwigBundle] Bundle templates and translations configuration #40631

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

Closed
wants to merge 3 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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__,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\CustomBundle\src\Entity\CustomPerson:
attributes:
name:
serialized_name: 'full_name'
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\CustomBundle\src\Entity\CustomPerson:
properties:
age:
- GreaterThan:
value: 18
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OK
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ok_label: OK
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
];
1 change: 1 addition & 0 deletions src/Symfony/Bundle/TwigBundle/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
],
]));
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/HttpKernel/Bundle/Bundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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).
*/
Expand Down
18 changes: 18 additions & 0 deletions src/Symfony/Component/HttpKernel/Bundle/BundleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions src/Symfony/Component/HttpKernel/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
-----
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,8 @@ protected function getKernelParameters()
$bundlesMetadata[$name] = [
'path' => $bundle->getPath(),
'namespace' => $bundle->getNamespace(),
'translations_path' => $bundle->getTranslationsPath(),
'templates_path' => $bundle->getTemplatesPath(),
];
}

Expand Down