Skip to content

[EventDispatcher] Moves the logic of addSubscriberService to the compiler pass #12069

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 1 commit into from

Conversation

Nicofuma
Copy link
Contributor

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Currently when an event subscribers is registered as a to be lazy loaded service, the corresponding class (and so at least a file) is loaded and parsed even if this subscriber isn't used at all. And when you have a lot of events subscribers (with phpBB extensions we can easily have dozens of subscribers) it could represent a lot of files (due to the use statements we rarely have only one file to load) and on some systems (i.e if you don't have an opcode cache) it can be important on the performance side.

@fabpot
Copy link
Member

fabpot commented Sep 29, 2014

Looks good to me. 👍

foreach ($class::getSubscribedEvents() as $eventName => $params) {
if (is_string($params)) {
$definition->addMethodCall('addListenerService', array($eventName, array($id, $params), 0));
} elseif (is_string($params[0])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

You assume it's an array if it's not a string

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a great opportunity to make sure it will not break 👍

@stof
Copy link
Member

stof commented Sep 29, 2014

This would require adding the class source files (yeah, plural because it requires considering all parent classes and traits as well) as container resources. Otherwise updating the list of event listeners in the subscriber will not be respected in the service definition (this is precisely why it was implemented this way: #2021 (comment)). Having a listener not registered because of a cache not being reloaded would be a huge WTF in dev (especially if we introduce it in 2.6 while it works previously), so I'm 👎 on it.
And to be safe, we would also have to take into account any additional resource used in the method implementation (constants of static methods coming from other classes for instance). And this last part is impossible (unless you build a custom PHP runtime to execute the method while keeping track of them, but this is totally crazy).

and adding all listener classes as container resources is likely to have an impact in dev, as it means that the container will need to be rebuilt each time you change the source code of your listener.

due to the use statements we rarely have only one file to load

Use statements don't trigger any autoloading, so they don't trigger file loads either. The only case where it would trigger an additional file load is when the imported class is used inside getSubscribedEvents

@Nicofuma
Copy link
Contributor Author

I see the point for the cache...

@fabpot
Copy link
Member

fabpot commented Oct 1, 2014

I'm closing this PR now as @stof has good arguments against this change.

@Strate
Copy link
Contributor

Strate commented Oct 13, 2014

@stof, almost all of your arguments are about environment with debug enabled. But we can use different logic with and without debug option.

@stof
Copy link
Member

stof commented Oct 13, 2014

@Strate I know they are. My point is that this change is making it impossible to implement a working debug mode.

Having a totally different logic for the non-debug mode is possible, but it could lead to weird bugs appearing only in non-debug mode, so I'm not sure we want this. What do you think @fabpot ?

@Strate
Copy link
Contributor

Strate commented Oct 13, 2014

I think it can break BC, if developers checks some of global state in getSubscribedEvents method. i.e.

public static function getSubscribedEvents()
{
    if ($_COOKIE['blabla']) { // or getEnv(), or somethng else.
        return [...]
    }
}

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