From 9766faf0616a67cd0ff7437950d5453deebac9c4 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 15 Jun 2012 12:33:28 +0200 Subject: [PATCH] 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. --- src/Silex/Application.php | 8 ++--- .../Provider/HttpCacheServiceProvider.php | 4 +-- src/Silex/Provider/MonologServiceProvider.php | 12 ++++---- .../Provider/SecurityServiceProvider.php | 11 ++----- src/Silex/Provider/SessionServiceProvider.php | 18 ++++------- .../Provider/SwiftmailerServiceProvider.php | 30 ++++++++++--------- .../Provider/TranslationServiceProvider.php | 4 ++- src/Silex/Provider/TwigServiceProvider.php | 16 +++++----- 8 files changed, 46 insertions(+), 57 deletions(-) diff --git a/src/Silex/Application.php b/src/Silex/Application.php index 16033e12a..a68c610db 100644 --- a/src/Silex/Application.php +++ b/src/Silex/Application.php @@ -169,13 +169,13 @@ public function __construct() */ public function register(ServiceProviderInterface $provider, array $values = array()) { - foreach ($values as $key => $value) { - $this[$key] = $value; - } - $this->providers[] = $provider; $provider->register($this); + + foreach ($values as $key => $value) { + $this[$key] = $value; + } } /** diff --git a/src/Silex/Provider/HttpCacheServiceProvider.php b/src/Silex/Provider/HttpCacheServiceProvider.php index eaa972271..c649fe932 100644 --- a/src/Silex/Provider/HttpCacheServiceProvider.php +++ b/src/Silex/Provider/HttpCacheServiceProvider.php @@ -38,9 +38,7 @@ public function register(Application $app) 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) diff --git a/src/Silex/Provider/MonologServiceProvider.php b/src/Silex/Provider/MonologServiceProvider.php index de28ef63a..c6865355d 100644 --- a/src/Silex/Provider/MonologServiceProvider.php +++ b/src/Silex/Provider/MonologServiceProvider.php @@ -36,7 +36,7 @@ public function register(Application $app) $app['monolog'] = $app->share(function () use ($app, $bridge) { $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); @@ -51,11 +51,11 @@ public function register(Application $app) return new StreamHandler($app['monolog.logfile'], $app['monolog.level']); }; - if (!isset($app['monolog.level'])) { - $app['monolog.level'] = function () { - return Logger::DEBUG; - }; - } + $app['monolog.level'] = function () { + return Logger::DEBUG; + }; + + $app['monolog.name'] = 'myapp'; } public function boot(Application $app) diff --git a/src/Silex/Provider/SecurityServiceProvider.php b/src/Silex/Provider/SecurityServiceProvider.php index 4a0c64003..f6bd5b0a6 100644 --- a/src/Silex/Provider/SecurityServiceProvider.php +++ b/src/Silex/Provider/SecurityServiceProvider.php @@ -65,6 +65,9 @@ public function register(Application $app) $that = $this; + $app['security.role_hierarchy'] = array(); + $app['security.access_rules'] = array(); + $app['security.context'] = $app->share(function () use ($app) { return new SecurityContext($app['security.authentication_manager'], $app['security.access_manager']); }); @@ -92,10 +95,6 @@ public function register(Application $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( new RoleHierarchyVoter(new RoleHierarchy($app['security.role_hierarchy'])), new AuthenticatedVoter($app['security.trust_resolver']), @@ -223,10 +222,6 @@ public function register(Application $app) $app['security.access_map'] = $app->share(function () use ($app) { $map = new AccessMap(); - if (!isset($app['security.access_rules'])) { - $app['security.access_rules'] = array(); - } - foreach ($app['security.access_rules'] as $rule) { if (is_string($rule[0])) { $rule[0] = new RequestMatcher($rule[0]); diff --git a/src/Silex/Provider/SessionServiceProvider.php b/src/Silex/Provider/SessionServiceProvider.php index 55a3db595..a2a5cdc9a 100644 --- a/src/Silex/Provider/SessionServiceProvider.php +++ b/src/Silex/Provider/SessionServiceProvider.php @@ -37,9 +37,7 @@ public function register(Application $app) { $this->app = $app; - if (!isset($app['session.test'])) { - $app['session.test'] = false; - } + $app['session.test'] = false; $app['session'] = $app->share(function () use ($app) { if (!isset($app['session.storage'])) { @@ -54,9 +52,7 @@ public function register(Application $app) }); $app['session.storage.handler'] = $app->share(function () use ($app) { - return new FileSessionHandler( - isset($app['session.storage.save_path']) ? $app['session.storage.save_path'] : null - ); + return new FileSessionHandler($app['session.storage.save_path']); }); $app['session.storage.native'] = $app->share(function () use ($app) { @@ -70,13 +66,9 @@ public function register(Application $app) return new MockFileSessionStorage(); }); - if (!isset($app['session.storage.options'])) { - $app['session.storage.options'] = array(); - } - - if (!isset($app['session.default_locale'])) { - $app['session.default_locale'] = 'en'; - } + $app['session.storage.options'] = array(); + $app['session.default_locale'] = 'en'; + $app['session.storage.save_path'] = null; } public function onEarlyKernelRequest(GetResponseEvent $event) diff --git a/src/Silex/Provider/SwiftmailerServiceProvider.php b/src/Silex/Provider/SwiftmailerServiceProvider.php index 63a1eac6a..aaa20f0bf 100644 --- a/src/Silex/Provider/SwiftmailerServiceProvider.php +++ b/src/Silex/Provider/SwiftmailerServiceProvider.php @@ -23,14 +23,7 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface { public function register(Application $app) { - $app['swiftmailer.options'] = array_replace(array( - 'host' => 'localhost', - 'port' => 25, - 'username' => '', - 'password' => '', - 'encryption' => null, - 'auth_mode' => null, - ), isset($app['swiftmailer.options']) ? $app['swiftmailer.options'] : array()); + $app['swiftmailer.options'] = array(); $app['mailer'] = $app->share(function () use ($app) { $r = new \ReflectionClass('Swift_Mailer'); @@ -54,12 +47,21 @@ public function register(Application $app) $app['swiftmailer.transport.eventdispatcher'] ); - $transport->setHost($app['swiftmailer.options']['host']); - $transport->setPort($app['swiftmailer.options']['port']); - $transport->setEncryption($app['swiftmailer.options']['encryption']); - $transport->setUsername($app['swiftmailer.options']['username']); - $transport->setPassword($app['swiftmailer.options']['password']); - $transport->setAuthMode($app['swiftmailer.options']['auth_mode']); + $options = $app['swiftmailer.options'] = array_replace(array( + 'host' => 'localhost', + 'port' => 25, + 'username' => '', + 'password' => '', + '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; }); diff --git a/src/Silex/Provider/TranslationServiceProvider.php b/src/Silex/Provider/TranslationServiceProvider.php index 703ecc601..2e2752d54 100644 --- a/src/Silex/Provider/TranslationServiceProvider.php +++ b/src/Silex/Provider/TranslationServiceProvider.php @@ -27,8 +27,10 @@ class TranslationServiceProvider implements ServiceProviderInterface { public function register(Application $app) { + $app['locale'] = 'en'; + $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'])) { $translator->setFallbackLocale($app['locale_fallback']); diff --git a/src/Silex/Provider/TwigServiceProvider.php b/src/Silex/Provider/TwigServiceProvider.php index ec633575a..093805930 100644 --- a/src/Silex/Provider/TwigServiceProvider.php +++ b/src/Silex/Provider/TwigServiceProvider.php @@ -28,14 +28,18 @@ class TwigServiceProvider implements ServiceProviderInterface { 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.options'] = array_replace( array( 'charset' => $app['charset'], 'debug' => $app['debug'], 'strict_variables' => $app['debug'], - ), - isset($app['twig.options']) ? $app['twig.options'] : array() + ), $app['twig.options'] ); $twig = new \Twig_Environment($app['twig.loader'], $app['twig.options']); @@ -60,10 +64,6 @@ public function register(Application $app) } 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'])); // add loader for Symfony built-in form templates @@ -81,11 +81,11 @@ public function register(Application $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) { - 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) {