8000 [FrameworkBundle] Use Bundle#getPath() to load config files by chalasr · Pull Request #18579 · symfony/symfony · GitHub
[go: up one dir, main page]

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

Conversation

chalasr
Copy link
Member
@chalasr chalasr commented Apr 18, 2016
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 is the better workaround I found for now.
As proposed by @wouterj, it would be cleaner to move the whole logic in a compiler pass and use the bundle classes instance directly, but $container->get('kernel')->getBundles() is not accessible because the service is synthetic (tried within the SerializerPass for instance), same problem in the extension.
I would like to have your suggestions if you think that would be even better to use a compiler pass.

I've kept the reflection and used it to invoke the getPath() method of each bundle, rather than calling dirname($reflection->getFilename()) and so totally ignore the getPath() method.

$reflection = new \ReflectionClass($fqcn);
$getPathReflection = new \ReflectionMethod($fqcn, 'getPath');

return $getPathReflection->invoke(new $fqcn()) ?: dirname($reflection->getFilename());
Copy link
Member

Choose a reason for hiding this comment

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

this is broken in case the class has some mandatory controller arguments (a few bundles do)

Copy link
Member Author
@chalasr chalasr Apr 18, 2016

Choose a reason for hiding this comment

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

@stof Of course ... Do you know how a way to access the bundles instances directly from a compiler pass? @wouterj ?

{
$reflection = new \ReflectionClass($fqcn);
$getPathReflection = new \ReflectionMethod($fqcn, 'getPath');
$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 :/

@wouterj
Copy link
Member
wouterj commented Apr 19, 2016

Is there an actual use-case to define the bundle path based on constructor arguments? I don't see any reason to do that.

What about making the method static? Won't that be an easy solution?

@chalasr
Copy link
Member Author
chalasr commented Apr 19, 2016

@wouterj I don't see any reason to use the constructor args inside the method, but in this case it leads to a fatal error.
Making the method static would easily solve the issue.

EDIT

@wouterj Making the method static gives unexpected side effects as the method uses $reflected->getFilename() as default path and the base Bundle is abstract.

@jvasseur
Copy link
Contributor

@wouterj the use case is see is to have a "virtual" bundle class that you can add multiple times, specifying the path in the constructor.

@chalasr
Copy link
Member Author
chalasr commented Apr 19, 2016

@jvasseur In case of setting the path in getPath depending on constructor args, call getPath from an instance without constructor would result in the default value (that is dirname($reflected->getFilename()), called by Symfony\Component\HttpKernel\Bundle\Bundle#getPath), or at worst null.

In case of getPath returning null, the value would be the default too (dirname($reflection->getFilename()) called from FrameworkExtension#getBundlePath()).

ATM I see a real gain into use the bundle#getPath if available (it will for most bundles), and a real loss if we don't try to use it at all.
Plus, I never saw a bundle that make such logic in its getPath (maybe you have an example?).

@jvasseur
Copy link
Contributor
jvasseur commented Apr 19, 2016

@chalasr the example i had in mind is https://github.com/WouterJ/Bundleless/blob/master/VirtualBundle.php

The problem with creating a bundle with newInstanceWithoutConstructor is that you don't get a properly instantiated class that could error in the getPath method (for example calling a method on one of its properties).

@chalasr
Copy link
Member Author
chalasr commented Apr 19, 2016

@jvasseur As the properties are declared but not set (constructor not called), calling getPath of the example you given would return null, and so, because of the condition in the return statement of FrameworkExtension#getBundlePath, the reflection file directory would be used (i.e. return $getPathReflection->invoke($reflectionInstance) ?: dirname($reflection->getFilename());).

As I understand, you're right, this fixes the issue for most cases but not for this particular (where it would behave as actually), but, while members are properly declared, I don't see how it could lead to errors?

@jvasseur
Copy link
Contributor

@chalasr It could lead to errors if you do something like this

class Bundle
{
    public function __construct($someProperty)
    {
        $this->someProperty = $someProperty;
    }

    public function getPath()
    {
        return $this->someProperty->someMethod();
    }
}

@chalasr
Copy link
Member Author
chalasr commented Apr 21, 2016

As said, the proposed fix would cover most of cases (all bundles classes that don't use any constructor argument into the getPath method). Also it removes duplicated code (getting the reflection file directory was repeated in each method before) and finally, I don't see any other alternative that doesn't involve to add/update/move lots of code (and give BC breaks).

chalasr added 2 commits May 18, 2016 22:41
Replace breaking new instance by reflection

Prevent unsupported method call for php < 5.4
@chalasr
Copy link
Member Author
chalasr commented Jun 13, 2016

I close this simply because it doesn't solve the problem entirely. Cover it partially would not be a good thing. Hope we soon find a real solution for this blocking issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
0