-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[EventDispatcher] Add Drupal EventDispatcher #12521
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
Conversation
Note that #12131 would also help a lot here, regardless of whether this PR will be accepted or not. |
Why you don't patch existing |
Because that breaks BC and thus cannot be accepted in Symfony, see #12019 |
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Drupal\Core\DependencyInjection\Compiler; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this namespace be changed to namespace Symfony\Component\EventDispatcher\DependencyInjection;
?
Seems like it should be possible to add an optional constructor argument to the current EventDispatcher instead of doing a new class. |
* </dd> | ||
* </dl> | ||
*/ | ||
class ContainerAwareEventDispatcher implements EventDispatcherInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class name does not correspond with the filename
The idea is great, but this lacks tests and have clearly not been run after the move here, as classname and filenames does not correspond. |
$definition['callable'] = array($this->container->get($definition['service'][0]), $definition['service'][1]); | ||
} | ||
|
||
$definition['callable']($event, $event_name, $this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I'm right, this dereferencing syntax is only available in PHP 5.4+, so we couldn't use it for Symfony.
Indeed this was just a copy paste with some cleanup which I did mainly for testing the waters. Regarding tests, see #12131. |
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function dispatch($event_name, Event $event = NULL) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know it's a minor thing, but in Symfony we usually put null
in lowercase.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that the PHP-CS-Fixer tool will fix this automatically
@znerol thanks for your PR. As you may now, in Symfony we have a bot that checks if the submitted code complies with the code syntax standards used in the project. Apparently, your PR needs some minor tweaks to comply with those standards. You can find a patch to fix them all at http://fabbot.io/report/symfony/symfony/12521/61864b082c705f55501bd7ef33e457d2df80656e Please tell me if you need any help with that. |
We actually wondered about that also, but it seems that this has been introduced in 5.3. The following test passes on 3v4l with the relevant versions:
|
@znerol you are right! I'm sorry about my comment because the syntax is indeed valid for PHP 5.3. |
So, the question now is: can this class replace the existing one in Symfony 3.0? If yes, we then need to deprecate the current one in favor of this one. |
There is a subtle change in behavior when using this class. This has been pointed out by @stof in #12069:
With the |
@znerol I don't think there is a practical use case of this drawback. So, even if I can see the BC break here, I don't think it makes sense to support it. IMO, if the "limitation" is well documented, that's more than enough. |
Ok, then the following things need to be done:
Regarding the compiler pass, is it okay to simply add the array of listeners to the list of constructor arguments? |
/** | ||
* Whether listeners need to be sorted prior to dispatch, keyed by event name. | ||
* | ||
* @var TRUE[] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type doc should be @var array
The compiler pass to initialize the dispatcher should be provided as well (otherwise we won't be able to use it by default). And I don't see how you will handle the lazy loading of event subscriber in your listener. There is no logic to handle this in your code. |
and registering subscribers in a non-lazy way is not an option either given that all Symfony listeners are defined as subcribers to be easier to reuse them in other frameworks like Silex |
It is there
If you mean
The intention of this PR is to provide an alternative implementation, not to replace the existing |
6237aeb
to
b91caf1
Compare
Travis failures for PHP 5.3 are the same as for the current 2.7 branch, no test failures in the event dispatcher component. |
You have my permission for this. I am definitely not happy with MIT licensing my code but let's do it. |
cd1d0bf
to
9a9b31c
Compare
Symfony 2.7 is about to enter feature-freeze. We should decide soon on whether or not this gets in. |
I'd be in favor of closing this one and work on #20875 instead. |
More generic solution posted in #20953, based on closure injection + compile time resolution of subscribers. |
Closed in favor of #20953 |
…t type (nicolas-grekas) This PR was merged into the 3.3-dev branch. Discussion ---------- [DI][EventDispatcher] Add & wire closure-proxy argument type | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12521, #12007, #20039 | License | MIT | Doc PR | - By resolving event subscribers at compile time, then wrapping listeners in a closure, we get laziness almost for free. This should solve/replaced all the above linked issues. (WIP because tests are missing) Commits ------- ecdf857 [DI][EventDispatcher] Add & wire closure-proxy argument type
Drupal forked the
EventDispatcher
for performance reasons a while ago because the construction of the event dispatcher service turned out to be an order of magnitude faster when listeners are injected into the constructor instead of adding them one-by-one with method calls (see Drupal issue 1972300).This PR contains that version with Symfony coding-standards applied and short-array syntax removed. Obviously the class name hardly should be
DrupalEventDispatcher
but I cannot figure out a good name atm. Let's first see whether Symfony is willing to adopt it.