Skip to content

[FrameworkBundle] Use Bundle#getPath() to load config files #18579

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 2 commits into from
Closed
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 @@ -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))) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -990,6 +988,32 @@ private function getKernelRootHash(ContainerBuilder $container)
return $this->kernelRootHash;
}

/**
* 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
*
* @return string
*/
private function getBundlePath($fqcn)
{
$reflection = new \ReflectionClass($fqcn);
$defaultPath = dirname($reflection->getFilename());

if (PHP_VERSION_ID < 50400) {
return $defaultPath;
}

$reflectionInstance = $reflection->newInstanceWithoutConstructor();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof I replaced the instance by this one, but I'm not sure if it can be a solution?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's a solution, the bundle could use on of it's constructor arguments to determine it's path.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jvasseur Yes this is what I mean. Any suggestion?

Copy link
Contributor

@jvasseur jvasseur Apr 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do anything without the actual bundle instances, maybe we can update the Kernel class to set the kernel service into the ContainerBuilder ?

Copy link
Member Author

@chalasr chalasr Apr 19, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ATM the only working way I found is to pass the Kernel instance as argument to the FrameworkBundle when register it (e.g. new FrameworkBundle($this) in the AppKernel), then pass the kernel as argument to a compiler pass (from the FrameworkBundle class) and use it to get the bundle instances. It works but I think that adding an argument to the FrameworkBundle is really not an alternative :/

$reflectedPathGetter = new \ReflectionMethod($fqcn, 'getPath');

return $reflectedPathGetter->invoke($reflectionInstance) ?: $defaultPath;
}

/**
* Returns the base path for the XSD files.
*
Expand Down