Skip to content

Commit cd5c429

Browse files
committed
Fix serializer/translations/validator resources loading for bundles overriding getPath()
1 parent 5ad1586 commit cd5c429

File tree

10 files changed

+73
-22
lines changed

10 files changed

+73
-22
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -692,12 +692,11 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder
692692
$dirs[] = dirname(dirname($r->getFileName())).'/Resources/translations';
693693
}
694694
$rootDir = $container->getParameter('kernel.root_dir');
695-
foreach ($container->getParameter('kernel.bundles') as $bundle => $class) {
696-
$reflection = new \ReflectionClass($class);
697-
if (is_dir($dir = dirname($reflection->getFileName()).'/Resources/translations')) {
695+
foreach ($container->getParameter('kernel.bundles_metadata') as $name => $bundle) {
696+
if (is_dir($dir = $bundle['path'].'/Resources/translations')) {
698697
$dirs[] = $dir;
699698
}
700-
if (is_dir($dir = $rootDir.sprintf('/Resources/%s/translations', $bundle))) {
699+
if (is_dir($dir = $rootDir.sprintf('/Resources/%s/translations', $name))) {
701700
$dirs[] = $dir;
702701
}
703702
}
@@ -809,11 +808,8 @@ private function getValidatorMappingFiles(ContainerBuilder $container)
809808
$container->addResource(new FileResource($files[0][0]));
810809
}
811810

812-
$bundles = $container->getParameter('kernel.bundles');
813-
foreach ($bundles as $bundle) {
814-
$reflection = new \ReflectionClass($bundle);
815-
$dirname = dirname($reflection->getFileName());
816-
811+
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
812+
$dirname = $bundle['path'];
817813
if (is_file($file = $dirname.'/Resources/config/validation.xml')) {
818814
$files[0][] = $file;
819815
$container->addResource(new FileResource($file));
@@ -924,10 +920,8 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder
924920
$serializerLoaders[] = $annotationLoader;
925921
}
926922

927-
$bundles = $container->getParameter('kernel.bundles');
928-
foreach ($bundles as $bundle) {
929-
$reflection = new \ReflectionClass($bundle);
930-
$dirname = dirname($reflection->getFileName());
923+
foreach ($container->getParameter('kernel.bundles_metadata') as $bundle) {
924+
$dirname = $bundle['path'];
931925

932926
if (is_file($file = $dirname.'/Resources/config/serialization.xml')) {
933927
$definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array($file));

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/CustomPathBundle/Resources/config/validation.xml

Whitespace-only changes.

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/CustomPathBundle/Resources/config/validation.yml

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Tests;
13+
14+
class CustomPathBundle extends \Symfony\Component\HttpKernel\Bundle\Bundle
15+
{
16+
public function getPath()
17+
{
18+
return __DIR__.'/..';
19+
}
20+
}

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

+30-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ public function testValidationPaths()
342342
require_once __DIR__.'/Fixtures/TestBundle/TestBundle.php';
343343

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

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

374+
public function testValidationPathsUsingCustomBundlePath()
375+
{
376+
require_once __DIR__.'/Fixtures/CustomPathBundle/src/CustomPathBundle.php';
377+
378+
$container = $this->createContainerFromFile('validation_annotations', array(
379+
'kernel.bundles' => array('CustomPathBundle' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\CustomPathBundle'),
380+
'kernel.bundles_metadata' => array('TestBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle\\Tests', 'parent' => null, 'path' => __DIR__.'/Fixtures/CustomPathBundle')),
381+
));
382+
383+
$calls = $container->getDefinition('validator.builder')->getMethodCalls();
384+
$xmlMappings = $calls[3][1][0];
385+
$this->assertCount(2, $xmlMappings);
386+
387+
try {
388+
// Testing symfony/symfony
389+
$this->assertStringEndsWith('Component'.DIRECTORY_SEPARATOR.'Form/Resources/config/validation.xml', $xmlMappings[0]);
390+
} catch (\Exception $e) {
391+
// Testing symfony/framework-bundle with deps=high
392+
$this->assertStringEndsWith('symfony'.DIRECTORY_SEPARATOR.'form/Resources/config/validation.xml', $xmlMappings[0]);
393+
}
394+
$this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.xml', $xmlMappings[1]);
395+
396+
$yamlMappings = $calls[4][1][0];
397+
$this->assertCount(1, $yamlMappings);
398+
$this->assertStringEndsWith('CustomPathBundle/Resources/config/validation.yml', $yamlMappings[0]);
399+
}
400+
373401
public function testValidationNoStaticMethod()
374402
{
375403
$container = $this->createContainerFromFile('validation_no_static_method');
@@ -472,6 +500,7 @@ protected function createContainer(array $data = array())
472500
{
473501
return new ContainerBuilder(new ParameterBag(array_merge(array(
474502
'kernel.bundles' => array('FrameworkBundle' => 'Symfony\\Bundle\\FrameworkBundle\\FrameworkBundle'),
503+
'kernel.bundles_metadata' => array('FrameworkBundle' => array('namespace' => 'Symfony\\Bundle\\FrameworkBundle', 'path' => __DIR__.'/../..', 'parent' => null)),
475504
'kernel.cache_dir' => __DIR__,
476505
'kernel.debug' => false,
477506
'kernel.environment' => 'test',

src/Symfony/Bundle/FrameworkBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"symfony/event-dispatcher": "~2.5",
2424
"symfony/finder": "~2.0,>=2.0.5",
2525
"symfony/http-foundation": "~2.7",
26-
"symfony/http-kernel": "~2.7.15|~2.8.8",
26+
"symfony/http-kernel": "~2.7.23|~2.8.16",
2727
"symfony/filesystem": "~2.3",
2828
"symfony/routing": "~2.6,>2.6.4",
2929
"symfony/security-core": "~2.6.13|~2.7.9|~2.8",

src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,13 @@ public function load(array $configs, ContainerBuilder $container)
8989
}
9090

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

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

src/Symfony/Bundle/TwigBundle/Tests/DependencyInjection/TwigExtensionTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ private function createContainer()
255255
'kernel.charset' => 'UTF-8',
256256
'kernel.debug' => false,
257257
'kernel.bundles' => array('TwigBundle' => 'Symfony\\Bundle\\TwigBundle\\TwigBundle'),
258+
'kernel.bundles_metadata' => array('TwigBundle' => array('namespace' => 'Symfony\\Bundle\\TwigBundle', 'parent' => null, 'path' => realpath(__DIR__.'/../..'))),
258259
)));
259260

260261
return $container;

src/Symfony/Bundle/TwigBundle/composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"symfony/routing": "~2.1",
3333
"symfony/templating": "~2.1",
3434
"symfony/yaml": "~2.3",
35-
"symfony/framework-bundle": "~2.7",
35+
"symfony/framework-bundle": "~2.7.23",
3636
"doctrine/annotations": "~1.0"
3737
},
3838
"autoload": {

src/Symfony/Component/HttpKernel/Kernel.php

+8
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,15 @@ protected function initializeContainer()
531531
protected function getKernelParameters()
532532
{
533533
$bundles = array();
534+
$bundlesMetadata = array();
535+
534536
foreach ($this->bundles as $name => $bundle) {
535537
$bundles[$name] = get_class($bundle);
538+
$bundlesMetadata[$name] = array(
539+
'parent' => $bundle->getParent(),
540+
'path' => $bundle->getPath(),
541+
'namespace' => $bundle->getNamespace(),
542+
);
536543
}
537544

538545
return array_merge(
@@ -544,6 +551,7 @@ protected function getKernelParameters()
544551
'kernel.cache_dir' => realpath($this->getCacheDir()) ?: $this->getCacheDir(),
545552
'kernel.logs_dir' => realpath($this->getLogDir()) ?: $this->getLogDir(),
546553
'kernel.bundles' => $bundles,
554+
'kernel.bundles_metadata' => $bundlesMetadata,
547555
'kernel.charset' => $this->getCharset(),
548556
'kernel.container_class' => $this->getContainerClass(),
549557
),

0 commit comments

Comments
 (0)