Skip to content

Commit

Permalink
bug #21113 [FrameworkBundle][HttpKernel] Fix resources loading for bu…
Browse files Browse the repository at this point in the history
…ndles with custom structure (chalasr)

This PR was merged into the 2.7 branch.

Discussion
----------

[FrameworkBundle][HttpKernel] Fix resources loading for bundles with custom structure

| Q             | A
| ------------- | ---
| Branch?       | 2.7
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18563
| License       | MIT
| Doc PR        |  n/a

This fixes twig/translator/validator/serializer resource loading for bundles overriding `Bundle::getPath()`, adding a kernel parameter containing the bundle metadata (i.e. `path`, `namespace` and `parent`).

Fixes #18563 and unlocks #19586

Commits
-------

fef3146 Fix serializer/translations/validator resources loading for bundles overriding getPath()
  • Loading branch information
fabpot committed Jan 4, 2017
2 parents d294051 + fef3146 commit da88e6b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 22 deletions.
Expand Up @@ -692,12 +692,11 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
$dirs[] = dirname(dirname($r->getFileName())).'/Resources/translations';
}
$rootDir = $container->getParameter('kernel.root_dir');
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
$reflection = new \ReflectionClass($class);
if (is_dir($dir = dirname($reflection->getFileName()).'/Resources/translations')) {
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if (is_dir($dir = $bundle['path'].'/Resources/translations')) {
$dirs[] = $dir;
}
if (is_dir($dir = $rootDir.sprintf('/Resources/%s/translations', $bundle))) {
if (is_dir($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
$dirs[] = $dir;
}
}
Expand Down Expand Up @@ -809,11 +808,8 @@ private function getValidatorMappingFiles(ContainerBuilder $container)
$container->addResource(new FileResource($files[0][0]));
}

$bundles = $container->getParameter('kernel.bundles');
foreach ($bundles as $bundle) {
$reflection = new \ReflectionClass($bundle);
$dirname = dirname($reflection->getFileName());

foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
$dirname = $bundle['path'];
if (is_file($file = $dirname.'/Resources/config/validation.xml')) {
$files[0][] = $file;
$container->addResource(new FileResource($file));
Expand Down Expand Up @@ -924,10 +920,8 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
$serializerLoaders[] = $annotationLoader;
}

$bundles = $container->getParameter('kernel.bundles');
foreach ($bundles as $bundle) {
$reflection = new \ReflectionClass($bundle);
$dirname = dirname($reflection->getFileName());
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
$dirname = $bundle['path'];

if (is_file($file = $dirname.'/Resources/config/serialization.xml')) {
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($file));
Expand Down
@@ -0,0 +1,20 @@
<?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;

class CustomPathBundle extends \Symfony\Component\HttpKernel\Bundle\Bundle
{
public function getPath()
{
return __DIR__.'/..';
}
}
Expand Up @@ -342,7 +342,8 @@ public function testValidationPaths()
require_once __DIR__.'/Fixtures/TestBundle/TestBundle.php';

$container = $this->createContainerFromFile('validation_annotations', array(
'kernel.bundles' => array('TestBundle' => 'Symfony\Bundle\FrameworkBundle\Tests\TestBundle'),
'kernel.bundles' => array('TestBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\TestBundle'),
'kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'parent' => null, 'path' => __DIR__.'/Fixtures/TestBundle')),
));

$calls = $container->getDefinition('validator.builder')->getMethodCalls();
Expand Down Expand Up @@ -370,6 +371,33 @@ public function testValidationPaths()
$this->assertStringEndsWith('TestBundle/Resources/config/validation.yml', $yamlMappings[0]);
}

public function testValidationPathsUsingCustomBundlePath()
{
require_once __DIR__.'/Fixtures/CustomPathBundle/src/CustomPathBundle.php';

$container = $this->createContainerFromFile('validation_annotations', array(
'kernel.bundles' => array('CustomPathBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\CustomPathBundle'),
'kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'parent' => null, 'path' => __DIR__.'/Fixtures/CustomPathBundle')),
));

$calls = $container->getDefinition('validator.builder')->getMethodCalls();
$xmlMappings = $calls[3][1][0];
$this->assertCount(2, $xmlMappings);

try {
// Testing symfony/symfony
$this->assertStringEndsWith('Component'.DIRECTORY_SEPARATOR.'Form/Resources/config/validation.xml', $xmlMappings[0]);
} catch (\Exception $e) {
// Testing symfony/framework-bundle with deps=high
$this->assertStringEndsWith('symfony'.DIRECTORY_SEPARATOR.'form/Resources/config/validation.xml', $xmlMappings[0]);
}
$this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.xml', $xmlMappings[1]);

$yamlMappings = $calls[4][1][0];
$this->assertCount(1, $yamlMappings);
$this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.yml', $yamlMappings[0]);
}

public function testValidationNoStaticMethod()
{
$container = $this->createContainerFromFile('validation_no_static_method');
Expand Down Expand Up @@ -472,6 +500,7 @@ protected function createContainer(array $data = array())
{
return new ContainerBuilder(new ParameterBag(array_merge(array(
'kernel.bundles' => array('FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'),
'kernel.bundles_metadata' => array('FrameworkBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..', 'parent' => null)),
'kernel.cache_dir' => __DIR__,
'kernel.debug' => false,
'kernel.environment' => 'test',
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Expand Up @@ -23,7 +23,7 @@
"symfony/event-dispatcher": "~2.5",
"symfony/finder": "~2.0,>=2.0.5",
"symfony/http-foundation": "~2.7",
"symfony/http-kernel": "~2.7.15|~2.8.8",
"symfony/http-kernel": "~2.7.23|~2.8.16",
"symfony/filesystem": "~2.3",
"symfony/routing": "~2.6,>2.6.4",
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",
Expand Down
Expand Up @@ -89,14 +89,13 @@ public function load(array $configs, ContainerBuilder $container)
}

// register bundles as Twig namespaces
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$bundle.'/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
if (is_dir($dir = $container->getParameter('kernel.root_dir').'/Resources/'.$name.'/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $name);
}

$reflection = new \ReflectionClass($class);
if (is_dir($dir = dirname($reflection->getFileName()).'/Resources/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $bundle);
if (is_dir($dir = $bundle['path'].'/Resources/views')) {
$this->addTwigPath($twigFilesystemLoaderDefinition, $dir, $name);
}
}

Expand Down
Expand Up @@ -255,6 +255,7 @@ private function createContainer()
'kernel.charset' => 'UTF-8',
'kernel.debug' => false,
'kernel.bundles' => array('TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle'),
'kernel.bundles_metadata' => array('TwigBundle' => array('namespace' => 'Symfony\\Bundle\\TwigBundle', 'parent' => null, 'path' => realpath(__DIR__.'/../..'))),
)));

return $container;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/composer.json
Expand Up @@ -21,7 +21,7 @@
"symfony/twig-bridge": "~2.7",
"twig/twig": "~1.28|~2.0",
"symfony/http-foundation": "~2.5",
"symfony/http-kernel": "~2.7"
"symfony/http-kernel": "~2.7.23|~2.8.16"
},
"require-dev": {
"symfony/stopwatch": "~2.2",
Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -531,8 +531,15 @@ protected function initializeContainer()
protected function getKernelParameters()
{
$bundles = array();
$bundlesMetadata = array();

foreach ($this->bundles as $name => $bundle) {
$bundles[$name] = get_class($bundle);
$bundlesMetadata[$name] = array(
'parent' => $bundle->getParent(),
'path' => $bundle->getPath(),
'namespace' => $bundle->getNamespace(),
);
}

return array_merge(
Expand All @@ -544,6 +551,7 @@ protected function getKernelParameters()
'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(),
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
'kernel.bundles' => $bundles,
'kernel.bundles_metadata' => $bundlesMetadata,
'kernel.charset' => $this->getCharset(),
'kernel.container_class' => $this->getContainerClass(),
),
Expand Down

0 comments on commit da88e6b

Please sign in to comment.