Commit 7bee6bfd authored by Fabien Potencier's avatar Fabien Potencier

adding a new interface for providers willing to register event listeners

parent 613c4bfa
......@@ -9,6 +9,7 @@ Changelog
if you want Silex to manage your locale (must also be registered for the translation service provider)
1.2.0 (2014-03-29)
------------------
* Allowed disabling the boot logic of MonologServiceProvider
* Reverted "convert attributes on the request that actually exist"
......
......@@ -179,11 +179,15 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
public function boot()
{
if (!$this->booted) {
$this->booted = true;
foreach ($this->providers as $provider) {
$provider->boot($this);
if ($provider instanceof EventListenerProviderInterface) {
$provider->subscribe($this, $this['dispatcher']);
}
$this->booted = true;
$provider->boot($this);
}
}
}
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Silex;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Interface for event listener providers.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
interface EventListenerProviderInterface
{
public function subscribe(Application $app, EventDispatcherInterface $dispatcher);
}
......@@ -12,8 +12,10 @@
namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\HttpCache;
use Silex\ServiceProviderInterface;
use Silex\EventListenerProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\HttpCache\Esi;
use Symfony\Component\HttpKernel\HttpCache\Store;
use Symfony\Component\HttpKernel\EventListener\EsiListener;
......@@ -23,7 +25,7 @@ use Symfony\Component\HttpKernel\EventListener\EsiListener;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class HttpCacheServiceProvider implements ServiceProviderInterface
class HttpCacheServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
public function register(Application $app)
{
......@@ -52,8 +54,12 @@ class HttpCacheServiceProvider implements ServiceProviderInterface
$app['http_cache.options'] = array();
}
public function subscribe(Application $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['http_cache.esi_listener']);
}
public function boot(Application $app)
{
$app['dispatcher']->addSubscriber($app['http_cache.esi_listener']);
}
}
......@@ -13,6 +13,8 @@ namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\EventListenerProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Fragment\FragmentHandler;
use Symfony\Component\HttpKernel\Fragment\InlineFragmentRenderer;
use Symfony\Component\HttpKernel\Fragment\EsiFragmentRenderer;
......@@ -25,7 +27,7 @@ use Symfony\Component\HttpKernel\UriSigner;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class HttpFragmentServiceProvider implements ServiceProviderInterface
class HttpFragmentServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
public function register(Application $app)
{
......@@ -76,8 +78,12 @@ class HttpFragmentServiceProvider implements ServiceProviderInterface
});
}
public function subscribe(Application $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['fragment.listener']);
}
public function boot(Application $app)
{
$app['dispatcher']->addSubscriber($app['fragment.listener']);
}
}
......@@ -14,14 +14,16 @@ namespace Silex\Provider;
use Silex\Application;
use Silex\LazyUrlMatcher;
use Silex\ServiceProviderInterface;
use Silex\EventListenerProviderInterface;
use Silex\EventListener\LocaleListener;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Locale Provider.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class LocaleServiceProvider implements ServiceProviderInterface
class LocaleServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
public function register(Application $app)
{
......@@ -39,8 +41,12 @@ class LocaleServiceProvider implements ServiceProviderInterface
$app['locale'] = 'en';
}
public function subscribe(Application $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['locale.listener']);
}
public function boot(Application $app)
{
$app['dispatcher']->addSubscriber($app['locale.listener']);
}
}
......@@ -13,6 +13,8 @@ namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\EventListenerProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Security\Core\Authentication\Provider\RememberMeAuthenticationProvider;
use Symfony\Component\Security\Http\Firewall\RememberMeListener;
use Symfony\Component\Security\Http\RememberMe\TokenBasedRememberMeServices;
......@@ -23,7 +25,7 @@ use Symfony\Component\Security\Http\RememberMe\ResponseListener;
*
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class RememberMeServiceProvider implements ServiceProviderInterface
class RememberMeServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
public function register(Application $app)
{
......@@ -93,12 +95,15 @@ class RememberMeServiceProvider implements ServiceProviderInterface
});
}
public function subscribe(Application $app, EventDispatcherInterface $dispatcher)
{
$dispatcher->addSubscriber($app['security.remember_me.response_listener']);
}
public function boot(Application $app)
{
if (!isset($app['security'])) {
throw new \LogicException('You must register the SecurityServiceProvider to use the RememberMeServiceProvider');
}
$app['dispatcher']->addSubscriber($app['security.remember_me.response_listener']);
}
}
......@@ -13,6 +13,8 @@ namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\EventListenerProviderInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\SecurityContext;
......@@ -57,7 +59,7 @@ use Symfony\Component\Security\Http\HttpUtils;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SecurityServiceProvider implements ServiceProviderInterface
class SecurityServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
protected $fakeRoutes;
......@@ -535,10 +537,13 @@ class SecurityServiceProvider implements ServiceProviderInterface
}
}
public function boot(Application $app)
public function subscribe(Application $app, EventDispatcherInterface $dispatcher)
{
$app['dispatcher']->addSubscriber($app['security.firewall']);
$dispatcher->addSubscriber($app['security.firewall']);
}
public function boot(Application $app)
{
foreach ($this->fakeRoutes as $route) {
list($method, $pattern, $name) = $route;
......
......@@ -13,8 +13,10 @@ namespace Silex\Provider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Silex\EventListenerProviderInterface;
use Silex\EventListener\SessionListener;
use Silex\EventListener\TestSessionListener;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\NativeFileSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session\Storage\MockFileSessionStorage;
......@@ -25,7 +27,7 @@ use Symfony\Component\HttpFoundation\Session\Session;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SessionServiceProvider implements ServiceProviderInterface
class SessionServiceProvider implements ServiceProviderInterface, EventListenerProviderInterface
{
private $app;
......@@ -75,12 +77,16 @@ class SessionServiceProvider implements ServiceProviderInterface
$app['session.storage.save_path'] = null;
}
public function boot(Application $app)
public function subscribe(Application $app, EventDispatcherInterface $dispatcher)
{
$app['dispatcher']->addSubscriber($app['session.listener']);
$dispatcher->addSubscriber($app['session.listener']);
if ($app['session.test']) {
$app['dispatcher']->addSubscriber($app['session.listener.test']);
}
}
public function boot(Application $app)
{
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment