Skip to content

Commit

Permalink
moved the setting of provider values after the registration to avoid …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
fabpot committed Jun 15, 2012
1 parent 93d4967 commit 9766faf
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 57 deletions.
8 changes: 4 additions & 4 deletions src/Silex/Application.php
Expand Up @@ -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;
}
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Silex/Provider/HttpCacheServiceProvider.php
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions src/Silex/Provider/MonologServiceProvider.php
Expand Up @@ -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);

Expand All @@ -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)
Expand Down
11 changes: 3 additions & 8 deletions src/Silex/Provider/SecurityServiceProvider.php
Expand Up @@ -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']);
});
Expand Down Expand Up @@ -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']),
Expand Down Expand Up @@ -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]);
Expand Down
18 changes: 5 additions & 13 deletions src/Silex/Provider/SessionServiceProvider.php
Expand Up @@ -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'])) {
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
30 changes: 16 additions & 14 deletions src/Silex/Provider/SwiftmailerServiceProvider.php
Expand Up @@ -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');
Expand All @@ -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;
});
Expand Down
4 changes: 3 additions & 1 deletion src/Silex/Provider/TranslationServiceProvider.php
Expand Up @@ -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']);
Expand Down
16 changes: 8 additions & 8 deletions src/Silex/Provider/TwigServiceProvider.php
Expand Up @@ -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']);
Expand All @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 9766faf

Please sign in to comment.