Commit c7536412 authored by Fabien Potencier's avatar Fabien Potencier

added the ServiceProviderInterface::boot() method

parent 95393c5b
...@@ -3,6 +3,8 @@ Changelog ...@@ -3,6 +3,8 @@ Changelog
This changelog references all backward incompatibilities as we introduce them: This changelog references all backward incompatibilities as we introduce them:
* **2012-05-26**: added ``boot()`` to ``ServiceProviderInterface``
* **2012-05-26**: Removed ``SymfonyBridgesServiceProvider`` * **2012-05-26**: Removed ``SymfonyBridgesServiceProvider``
* **2012-05-26**: Removed the ``translator.messages`` parameter (use * **2012-05-26**: Removed the ``translator.messages`` parameter (use
......
...@@ -48,6 +48,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -48,6 +48,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{ {
const VERSION = '@package_version@'; const VERSION = '@package_version@';
private $providers = array();
private $booted = false;
/** /**
* Constructor. * Constructor.
*/ */
...@@ -147,9 +150,22 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -147,9 +150,22 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
$this[$key] = $value; $this[$key] = $value;
} }
$this->providers[] = $provider;
$provider->register($this); $provider->register($this);
} }
public function boot()
{
if (!$this->booted) {
foreach ($this->providers as $provider) {
$provider->boot($this);
}
$this->booted = true;
}
}
/** /**
* Maps a pattern to a callable. * Maps a pattern to a callable.
* *
...@@ -439,6 +455,10 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -439,6 +455,10 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/ */
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true) public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{ {
if (!$this->booted) {
$this->boot();
}
$this->beforeDispatched = false; $this->beforeDispatched = false;
$current = HttpKernelInterface::SUB_REQUEST === $type ? $this['request'] : $this['request_error']; $current = HttpKernelInterface::SUB_REQUEST === $type ? $this['request'] : $this['request_error'];
......
...@@ -119,4 +119,8 @@ class DoctrineServiceProvider implements ServiceProviderInterface ...@@ -119,4 +119,8 @@ class DoctrineServiceProvider implements ServiceProviderInterface
return $dbs[$app['dbs.default']]; return $dbs[$app['dbs.default']];
}); });
} }
public function boot(Application $app)
{
}
} }
...@@ -52,4 +52,8 @@ class FormServiceProvider implements ServiceProviderInterface ...@@ -52,4 +52,8 @@ class FormServiceProvider implements ServiceProviderInterface
return new DefaultCsrfProvider($app['form.secret']); return new DefaultCsrfProvider($app['form.secret']);
}); });
} }
public function boot(Application $app)
{
}
} }
...@@ -42,4 +42,8 @@ class HttpCacheServiceProvider implements ServiceProviderInterface ...@@ -42,4 +42,8 @@ class HttpCacheServiceProvider implements ServiceProviderInterface
$app['http_cache.options'] = array(); $app['http_cache.options'] = array();
} }
} }
public function boot(Application $app)
{
}
} }
...@@ -50,7 +50,10 @@ class MonologServiceProvider implements ServiceProviderInterface ...@@ -50,7 +50,10 @@ class MonologServiceProvider implements ServiceProviderInterface
return Logger::DEBUG; return Logger::DEBUG;
}; };
} }
}
public function boot(Application $app)
{
$app->before(function (Request $request) use ($app) { $app->before(function (Request $request) use ($app) {
$app['monolog']->addInfo('> '.$request->getMethod().' '.$request->getRequestUri()); $app['monolog']->addInfo('> '.$request->getMethod().' '.$request->getRequestUri());
}); });
......
...@@ -49,8 +49,6 @@ class SessionServiceProvider implements ServiceProviderInterface ...@@ -49,8 +49,6 @@ class SessionServiceProvider implements ServiceProviderInterface
); );
}); });
$app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), 128);
if (!isset($app['session.storage.options'])) { if (!isset($app['session.storage.options'])) {
$app['session.storage.options'] = array(); $app['session.storage.options'] = array();
} }
...@@ -70,4 +68,9 @@ class SessionServiceProvider implements ServiceProviderInterface ...@@ -70,4 +68,9 @@ class SessionServiceProvider implements ServiceProviderInterface
$request->getSession()->start(); $request->getSession()->start();
} }
} }
public function boot(Application $app)
{
$app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), 128);
}
} }
...@@ -76,14 +76,17 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface ...@@ -76,14 +76,17 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface
return new \Swift_Events_SimpleEventDispatcher(); return new \Swift_Events_SimpleEventDispatcher();
}); });
$app->finish(function () use ($app) {
$app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
});
if (isset($app['swiftmailer.class_path'])) { if (isset($app['swiftmailer.class_path'])) {
require_once $app['swiftmailer.class_path'].'/Swift.php'; require_once $app['swiftmailer.class_path'].'/Swift.php';
\Swift::registerAutoload($app['swiftmailer.class_path'].'/../swift_init.php'); \Swift::registerAutoload($app['swiftmailer.class_path'].'/../swift_init.php');
} }
} }
public function boot(Application $app)
{
$app->finish(function () use ($app) {
$app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
});
}
} }
...@@ -53,4 +53,8 @@ class TranslationServiceProvider implements ServiceProviderInterface ...@@ -53,4 +53,8 @@ class TranslationServiceProvider implements ServiceProviderInterface
return new MessageSelector(); return new MessageSelector();
}); });
} }
public function boot(Application $app)
{
}
} }
...@@ -90,4 +90,8 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -90,4 +90,8 @@ class TwigServiceProvider implements ServiceProviderInterface
)); ));
}); });
} }
public function boot(Application $app)
{
}
} }
...@@ -31,4 +31,8 @@ class UrlGeneratorServiceProvider implements ServiceProviderInterface ...@@ -31,4 +31,8 @@ class UrlGeneratorServiceProvider implements ServiceProviderInterface
return new UrlGenerator($app['routes'], $app['request_context']); return new UrlGenerator($app['routes'], $app['request_context']);
}); });
} }
public function boot(Application $app)
{
}
} }
...@@ -43,4 +43,8 @@ class ValidatorServiceProvider implements ServiceProviderInterface ...@@ -43,4 +43,8 @@ class ValidatorServiceProvider implements ServiceProviderInterface
return new ConstraintValidatorFactory(); return new ConstraintValidatorFactory();
}); });
} }
public function boot(Application $app)
{
}
} }
...@@ -21,7 +21,19 @@ interface ServiceProviderInterface ...@@ -21,7 +21,19 @@ interface ServiceProviderInterface
/** /**
* Registers services on the given app. * Registers services on the given app.
* *
* This method should only be used to configure services and parameters.
* It should not get services.
*
* @param Application $app An Application instance * @param Application $app An Application instance
*/ */
function register(Application $app); function register(Application $app);
/**
* Bootstraps the application.
*
* This method is called after all services are registers
* and should be used for "dynamic" configuration (whenever
* a service must be requested).
*/
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