Commit 9766faf0 authored by Fabien Potencier's avatar Fabien Potencier

moved the setting of provider values after the registration to avoid having...

moved the setting of provider values after the registration to avoid having isset calls everywhere in the provider code

Before, the behavior was not consistent. Some values could be overridden
by passing an array as a second argument to the register() method, but
for most of them it did not work (we would have to wrap all definitions
with an isset()). Now, this is more consistent as you can override
everything that is defined in the provider.
parent 93d49671
...@@ -169,13 +169,13 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe ...@@ -169,13 +169,13 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
*/ */
public function register(ServiceProviderInterface $provider, array $values = array()) public function register(ServiceProviderInterface $provider, array $values = array())
{ {
foreach ($values as $key => $value) {
$this[$key] = $value;
}
$this->providers[] = $provider; $this->providers[] = $provider;
$provider->register($this); $provider->register($this);
foreach ($values as $key => $value) {
$this[$key] = $value;
}
} }
/** /**
......
...@@ -38,9 +38,7 @@ class HttpCacheServiceProvider implements ServiceProviderInterface ...@@ -38,9 +38,7 @@ class HttpCacheServiceProvider implements ServiceProviderInterface
return new Store($app['http_cache.cache_dir']); return new Store($app['http_cache.cache_dir']);
}); });
if (!isset($app['http_cache.options'])) { $app['http_cache.options'] = array();
$app['http_cache.options'] = array();
}
} }
public function boot(Application $app) public function boot(Application $app)
......
...@@ -36,7 +36,7 @@ class MonologServiceProvider implements ServiceProviderInterface ...@@ -36,7 +36,7 @@ class MonologServiceProvider implements ServiceProviderInterface
$app['monolog'] = $app->share(function () use ($app, $bridge) { $app['monolog'] = $app->share(function () use ($app, $bridge) {
$class = $bridge ? 'Symfony\Bridge\Monolog\Logger' : 'Monolog\Logger'; $class = $bridge ? 'Symfony\Bridge\Monolog\Logger' : 'Monolog\Logger';
$log = new $class(isset($app['monolog.name']) ? $app['monolog.name'] : 'myapp'); $log = new $class($app['monolog.name']);
$app['monolog.configure']($log); $app['monolog.configure']($log);
...@@ -51,11 +51,11 @@ class MonologServiceProvider implements ServiceProviderInterface ...@@ -51,11 +51,11 @@ class MonologServiceProvider implements ServiceProviderInterface
return new StreamHandler($app['monolog.logfile'], $app['monolog.level']); return new StreamHandler($app['monolog.logfile'], $app['monolog.level']);
}; };
if (!isset($app['monolog.level'])) { $app['monolog.level'] = function () {
$app['monolog.level'] = function () { return Logger::DEBUG;
return Logger::DEBUG; };
};
} $app['monolog.name'] = 'myapp';
} }
public function boot(Application $app) public function boot(Application $app)
......
...@@ -65,6 +65,9 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -65,6 +65,9 @@ class SecurityServiceProvider implements ServiceProviderInterface
$that = $this; $that = $this;
$app['security.role_hierarchy'] = array();
$app['security.access_rules'] = array();
$app['security.context'] = $app->share(function () use ($app) { $app['security.context'] = $app->share(function () use ($app) {
return new SecurityContext($app['security.authentication_manager'], $app['security.access_manager']); return new SecurityContext($app['security.authentication_manager'], $app['security.access_manager']);
}); });
...@@ -92,10 +95,6 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -92,10 +95,6 @@ class SecurityServiceProvider implements ServiceProviderInterface
}); });
$app['security.access_manager'] = $app->share(function () use ($app) { $app['security.access_manager'] = $app->share(function () use ($app) {
if (!isset($app['security.role_hierarchy'])) {
$app['security.role_hierarchy'] = array();
}
return new AccessDecisionManager(array( return new AccessDecisionManager(array(
new RoleHierarchyVoter(new RoleHierarchy($app['security.role_hierarchy'])), new RoleHierarchyVoter(new RoleHierarchy($app['security.role_hierarchy'])),
new AuthenticatedVoter($app['security.trust_resolver']), new AuthenticatedVoter($app['security.trust_resolver']),
...@@ -223,10 +222,6 @@ class SecurityServiceProvider implements ServiceProviderInterface ...@@ -223,10 +222,6 @@ class SecurityServiceProvider implements ServiceProviderInterface
$app['security.access_map'] = $app->share(function () use ($app) { $app['security.access_map'] = $app->share(function () use ($app) {
$map = new AccessMap(); $map = new AccessMap();
if (!isset($app['security.access_rules'])) {
$app['security.access_rules'] = array();
}
foreach ($app['security.access_rules'] as $rule) { foreach ($app['security.access_rules'] as $rule) {
if (is_string($rule[0])) { if (is_string($rule[0])) {
$rule[0] = new RequestMatcher($rule[0]); $rule[0] = new RequestMatcher($rule[0]);
......
...@@ -37,9 +37,7 @@ class SessionServiceProvider implements ServiceProviderInterface ...@@ -37,9 +37,7 @@ class SessionServiceProvider implements ServiceProviderInterface
{ {
$this->app = $app; $this->app = $app;
if (!isset($app['session.test'])) { $app['session.test'] = false;
$app['session.test'] = false;
}
$app['session'] = $app->share(function () use ($app) { $app['session'] = $app->share(function () use ($app) {
if (!isset($app['session.storage'])) { if (!isset($app['session.storage'])) {
...@@ -54,9 +52,7 @@ class SessionServiceProvider implements ServiceProviderInterface ...@@ -54,9 +52,7 @@ class SessionServiceProvider implements ServiceProviderInterface
}); });
$app['session.storage.handler'] = $app->share(function () use ($app) { $app['session.storage.handler'] = $app->share(function () use ($app) {
return new FileSessionHandler( return new FileSessionHandler($app['session.storage.save_path']);
isset($app['session.storage.save_path']) ? $app['session.storage.save_path'] : null
);
}); });
$app['session.storage.native'] = $app->share(function () use ($app) { $app['session.storage.native'] = $app->share(function () use ($app) {
...@@ -70,13 +66,9 @@ class SessionServiceProvider implements ServiceProviderInterface ...@@ -70,13 +66,9 @@ class SessionServiceProvider implements ServiceProviderInterface
return new MockFileSessionStorage(); return new MockFileSessionStorage();
}); });
if (!isset($app['session.storage.options'])) { $app['session.storage.options'] = array();
$app['session.storage.options'] = array(); $app['session.default_locale'] = 'en';
} $app['session.storage.save_path'] = null;
if (!isset($app['session.default_locale'])) {
$app['session.default_locale'] = 'en';
}
} }
public function onEarlyKernelRequest(GetResponseEvent $event) public function onEarlyKernelRequest(GetResponseEvent $event)
......
...@@ -23,14 +23,7 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface ...@@ -23,14 +23,7 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface
{ {
public function register(Application $app) public function register(Application $app)
{ {
$app['swiftmailer.options'] = array_replace(array( $app['swiftmailer.options'] = array();
'host' => 'localhost',
'port' => 25,
'username' => '',
'password' => '',
'encryption' => null,
'auth_mode' => null,
), isset($app['swiftmailer.options']) ? $app['swiftmailer.options'] : array());
$app['mailer'] = $app->share(function () use ($app) { $app['mailer'] = $app->share(function () use ($app) {
$r = new \ReflectionClass('Swift_Mailer'); $r = new \ReflectionClass('Swift_Mailer');
...@@ -54,12 +47,21 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface ...@@ -54,12 +47,21 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface
$app['swiftmailer.transport.eventdispatcher'] $app['swiftmailer.transport.eventdispatcher']
); );
$transport->setHost($app['swiftmailer.options']['host']); $options = $app['swiftmailer.options'] = array_replace(array(
$transport->setPort($app['swiftmailer.options']['port']); 'host' => 'localhost',
$transport->setEncryption($app['swiftmailer.options']['encryption']); 'port' => 25,
$transport->setUsername($app['swiftmailer.options']['username']); 'username' => '',
$transport->setPassword($app['swiftmailer.options']['password']); 'password' => '',
$transport->setAuthMode($app['swiftmailer.options']['auth_mode']); 'encryption' => null,
'auth_mode' => null,
), $app['swiftmailer.options']);
$transport->setHost($options['host']);
$transport->setPort($options['port']);
$transport->setEncryption($options['encryption']);
$transport->setUsername($options['username']);
$transport->setPassword($options['password']);
$transport->setAuthMode($options['auth_mode']);
return $transport; return $transport;
}); });
......
...@@ -27,8 +27,10 @@ class TranslationServiceProvider implements ServiceProviderInterface ...@@ -27,8 +27,10 @@ class TranslationServiceProvider implements ServiceProviderInterface
{ {
public function register(Application $app) public function register(Application $app)
{ {
$app['locale'] = 'en';
$app['translator'] = $app->share(function () use ($app) { $app['translator'] = $app->share(function () use ($app) {
$translator = new Translator(isset($app['locale']) ? $app['locale'] : 'en', $app['translator.message_selector']); $translator = new Translator($app['locale'], $app['translator.message_selector']);
if (isset($app['locale_fallback'])) { if (isset($app['locale_fallback'])) {
$translator->setFallbackLocale($app['locale_fallback']); $translator->setFallbackLocale($app['locale_fallback']);
......
...@@ -28,14 +28,18 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -28,14 +28,18 @@ class TwigServiceProvider implements ServiceProviderInterface
{ {
public function register(Application $app) public function register(Application $app)
{ {
$app['twig.options'] = array();
$app['twig.form.templates'] = array('form_div_layout.html.twig');
$app['twig.path'] = array();
$app['twig.templates'] = array();
$app['twig'] = $app->share(function () use ($app) { $app['twig'] = $app->share(function () use ($app) {
$app['twig.options'] = array_replace( $app['twig.options'] = array_replace(
array( array(
'charset' => $app['charset'], 'charset' => $app['charset'],
'debug' => $app['debug'], 'debug' => $app['debug'],
'strict_variables' => $app['debug'], 'strict_variables' => $app['debug'],
), ), $app['twig.options']
isset($app['twig.options']) ? $app['twig.options'] : array()
); );
$twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']); $twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']);
...@@ -60,10 +64,6 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -60,10 +64,6 @@ class TwigServiceProvider implements ServiceProviderInterface
} }
if (isset($app['form.factory'])) { if (isset($app['form.factory'])) {
if (!isset($app['twig.form.templates'])) {
$app['twig.form.templates'] = array('form_div_layout.html.twig');
}
$twig->addExtension(new FormExtension($app['form.csrf_provider'], $app['twig.form.templates'])); $twig->addExtension(new FormExtension($app['form.csrf_provider'], $app['twig.form.templates']));
// add loader for Symfony built-in form templates // add loader for Symfony built-in form templates
...@@ -81,11 +81,11 @@ class TwigServiceProvider implements ServiceProviderInterface ...@@ -81,11 +81,11 @@ class TwigServiceProvider implements ServiceProviderInterface
}); });
$app['twig.loader.filesystem'] = $app->share(function () use ($app) { $app['twig.loader.filesystem'] = $app->share(function () use ($app) {
return new \Twig_Loader_Filesystem(isset($app['twig.path']) ? $app['twig.path'] : array()); return new \Twig_Loader_Filesystem($app['twig.path']);
}); });
$app['twig.loader.array'] = $app->share(function () use ($app) { $app['twig.loader.array'] = $app->share(function () use ($app) {
return new \Twig_Loader_Array(isset($app['twig.templates']) ? $app['twig.templates'] : array()); return new \Twig_Loader_Array($app['twig.templates']);
}); });
$app['twig.loader'] = $app->share(function () use ($app) { $app['twig.loader'] = $app->share(function () use ($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