Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save graste/fe0acc35bfdbf105535ccd62d7f3d4ed to your computer and use it in GitHub Desktop.

Select an option

Save graste/fe0acc35bfdbf105535ccd62d7f3d4ed to your computer and use it in GitHub Desktop.
<?php
// This code is what the Symfony Dependency Injection container performs
// under the hood when registering a listener.
$eventDispatcher->addListener(
KernelEvents::REQUEST,
function ($event) {
// only when here, MyListener and ExpensiveDependency trigger autoloading
// and the time of calling their constructor is spent.
(new MyListener(new ExpensiveDependency))->onKernelRequest($event);
},
);
// dispatch "unwraps" the listener by calling the closure, only then MyListener
// and the ExpensiveDependency are created.
$eventDispatcher->dispatch(
new RequestEvent(/** ... */), KernelEvents::REQUEST)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment