From c7f196f775c90292365e8326c416d88c09d61502 Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Mon, 18 Apr 2016 15:44:52 +0200 Subject: [PATCH 1/2] [FrameworkBundle] Use Bundle#getPath() to load config files Replace breaking new instance by reflection Prevent unsupported method call for php < 5.4 --- .../FrameworkExtension.php | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 15b1e1fdf3b28..aa89a2917ed28 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -694,8 +694,8 @@ private function registerTranslatorConfiguration(array $config, ContainerBuilder } $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')) { + $dirname = $this->getBundlePath($class); + if (is_dir($dir = $dirname.'/Resources/translations')) { $dirs[] = $dir; } if (is_dir($dir = $rootDir.sprintf('/Resources/%s/translations', $bundle))) { @@ -808,8 +808,7 @@ private function getValidatorMappingFiles(ContainerBuilder $container) $bundles = $container->getParameter('kernel.bundles'); foreach ($bundles as $bundle) { - $reflection = new \ReflectionClass($bundle); - $dirname = dirname($reflection->getFileName()); + $dirname = $this->getBundlePath($bundle); if (is_file($file = $dirname.'/Resources/config/validation.xml')) { $files[0][] = realpath($file); @@ -923,8 +922,7 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder $bundles = $container->getParameter('kernel.bundles'); foreach ($bundles as $bundle) { - $reflection = new \ReflectionClass($bundle); - $dirname = dirname($reflection->getFileName()); + $dirname = $this->getBundlePath($bundle); if (is_file($file = $dirname.'/Resources/config/serialization.xml')) { $definition = new Definition('Symfony\Component\Serializer\Mapping\Loader\XmlFileLoader', array(realpath($file))); @@ -990,6 +988,28 @@ private function getKernelRootHash(ContainerBuilder $container) return $this->kernelRootHash; } + /** + * Gets the path of a given bundle using Bundle#getPath() when available. + * + * @param string $fqcn The bundle FQCN + * + * @return string + */ + private function getBundlePath($fqcn) + { + $reflection = new \ReflectionClass($fqcn); + $defaultPath = dirname($reflection->getFilename()); + + if (PHP_VERSION_ID < 50400) { + return $defaultPath; + } + + $reflectionInstance = $reflection->newInstanceWithoutConstructor(); + $reflectedPathGetter = new \ReflectionMethod($fqcn, 'getPath'); + + return $reflectedPathGetter->invoke($reflectionInstance) ?: $defaultPath; + } + /** * Returns the base path for the XSD files. * From f3f8841b12783cb461cc349100f978e5ce3f167b Mon Sep 17 00:00:00 2001 From: Robin Chalas Date: Wed, 18 May 2016 22:40:38 +0200 Subject: [PATCH 2/2] Fix class#method to class::method, add a warning in the description --- .../DependencyInjection/FrameworkExtension.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index aa89a2917ed28..d6b2a6676336a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -989,7 +989,11 @@ private function getKernelRootHash(ContainerBuilder $container) } /** - * Gets the path of a given bundle using Bundle#getPath() when available. + * Gets the path of a given bundle using Bundle::getPath() when usable. + * + * WARNING: + * Calling this may cause a fatal error if the getPath method contains call(s) + * of constructor argument's methods, e.g. Bundle::$foo->bar() called in Bundle::getPath() * * @param string $fqcn The bundle FQCN *