8000 [FrameworkBundle][HttpKernel] Fix resources loading for bundles with custom structure by chalasr · Pull Request #21113 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

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 1 commit into from
Jan 4, 2017
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 @@ -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
Original file line number Diff line number Diff line change
@@ -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__.'/..';
}
}
E864
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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