-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from jdreesen/silex-2
Compatibility with Silex 2
- Loading branch information
Showing
4 changed files
with
111 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace DI\Bridge\Silex\Provider; | ||
|
||
use DI\Bridge\Silex\CallbackInvoker; | ||
use DI\Bridge\Silex\CallbackResolver; | ||
use DI\Bridge\Silex\Controller\ControllerResolver; | ||
use DI\Bridge\Silex\ConverterListener; | ||
use DI\Bridge\Silex\MiddlewareListener; | ||
use Interop\Container\ContainerInterface; | ||
use Invoker\ParameterResolver\AssociativeArrayResolver; | ||
use Invoker\ParameterResolver\Container\TypeHintContainerResolver; | ||
use Invoker\ParameterResolver\ResolverChain; | ||
use Pimple\ServiceProviderInterface; | ||
use Silex\Api\EventListenerProviderInterface; | ||
use Symfony\Component\EventDispatcher\EventDispatcherInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* @author Jacob Dreesen <jacob.dreesen@gmail.com> | ||
*/ | ||
class HttpKernelServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface | ||
{ | ||
/** | ||
* @var ContainerInterface | ||
*/ | ||
private $container; | ||
|
||
/** | ||
* @var CallbackInvoker | ||
*/ | ||
private $callbackInvoker; | ||
|
||
/** | ||
* @param ContainerInterface $container | ||
* @param CallbackInvoker $callbackInvoker | ||
*/ | ||
public function __construct(ContainerInterface $container, CallbackInvoker $callbackInvoker) | ||
{ | ||
$this->container = $container; | ||
$this->callbackInvoker = $callbackInvoker; | ||
} | ||
|
||
public function register(\Pimple\Container $app) | ||
{ | ||
// Override the controller resolver with ours. | ||
$app['resolver'] = function ($app) { | ||
return new ControllerResolver( | ||
$app['phpdi.callable_resolver'], | ||
new ResolverChain([ | ||
new AssociativeArrayResolver, | ||
new TypeHintContainerResolver($this->container), | ||
]) | ||
); | ||
}; | ||
|
||
// Override the callback resolver with ours. | ||
$app['callback_resolver'] = function ($app) { | ||
return new CallbackResolver( | ||
$app, | ||
$app['phpdi.callable_resolver'] | ||
); | ||
}; | ||
} | ||
|
||
public function subscribe(\Pimple\Container $app, EventDispatcherInterface $dispatcher) | ||
{ | ||
// Remove the Silex listeners first | ||
$this->removeSubscriber($dispatcher, \Silex\EventListener\MiddlewareListener::class); | ||
$this->removeSubscriber($dispatcher, \Silex\EventListener\ConverterListener::class); | ||
|
||
// And register ours instead | ||
$dispatcher->addSubscriber(new MiddlewareListener($app, $this->callbackInvoker)); | ||
$dispatcher->addSubscriber(new ConverterListener($app['routes'], $app['callback_resolver'], $this->callbackInvoker)); | ||
} | ||
|
||
/** | ||
* Removes an event subscriber by its class name. | ||
* | ||
* @param EventDispatcherInterface $dispatcher | ||
* @param string $subscriberClass | ||
*/ | ||
private function removeSubscriber(EventDispatcherInterface $dispatcher, $subscriberClass) | ||
{ | ||
if (!is_subclass_of($subscriberClass, EventSubscriberInterface::class, true)) { | ||
throw new \InvalidArgumentException('$subscriberClass must implement ' . EventSubscriberInterface::class); | ||
} | ||
|
||
/** @var EventSubscriberInterface $subscriberClass */ | ||
foreach ($subscriberClass::getSubscribedEvents() as $eventName => $params) { | ||
foreach ($dispatcher->getListeners($eventName) as $listener) { | ||
if (is_array($listener) && is_a($listener[0], $subscriberClass, true)) { | ||
$dispatcher->removeListener($eventName, $listener); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters