Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: fabpot/Silex
...
head fork: fabpot/Silex
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 17 files changed
  • 0 commit comments
  • 1 contributor
Commits on May 26, 2012
@fabpot added the ServiceProviderInterface::boot() method c753641
@fabpot made monolog logger as the default logger when the monolog bridge is …
…installed
c5e7a0b
Commits on May 27, 2012
@fabpot merged branch fabpot/boot (PR #330)
Commits
-------

c753641 added the ServiceProviderInterface::boot() method

Discussion
----------

added the ServiceProviderInterface::boot() method

The `register` method for the service providers should only be about configuring services and parameters. But some service providers (monolog, swiftmailer, and session) also register some event listeners which means that the `dispatcher` service is created.

This makes some things impossible like replacing the `dispatcher` service (except if you make sure to override it *before* the other providers). Also, if you want to use Monolog for the logger, it won't work as there is a chicken and egg problem.

So, I propose to introduce a new `boot()` method on `ServiceProviderInterface` that must be called after all the providers are registered.

The `boot()` method is automatically called by the `handle()` method if not already called by the developer.

TODO: update the doc

---------------------------------------------------------------------------

by fabpot at 2012-05-26T09:59:48Z

We can avoid the BC break by creating a new `BootableServiceProviderInterface` but it seems overkill (especially because we are not stable yet).

---------------------------------------------------------------------------

by stof at 2012-05-26T10:36:05Z

:+1: And I agree that adding a new interface is overkill

---------------------------------------------------------------------------

by igorw at 2012-05-27T18:14:06Z

I'm not so happy with the BC break and that an additional method is required, but I can't think of any better way to solve this cleanly. So I guess let's go for it.
14cbd6c
@fabpot merged branch fabpot/monolog-as-logger (PR #331)
Commits
-------

c5e7a0b made monolog logger as the default logger when the monolog bridge is installed

Discussion
----------

Use Monolog as the default logger when the Symfony2 Monolog bridge is installed

#330 is a pre-requisite for this PR to be merged.
410c1dd
View
1  composer.json
@@ -32,6 +32,7 @@
"monolog/monolog": ">=1.0.0",
"symfony/translation": "2.1.*",
"symfony/twig-bridge": "2.1.*",
+ "symfony/monolog-bridge": "2.1.*",
"symfony/validator": "2.1.*",
"twig/twig": ">=1.2.0",
"doctrine/dbal": "2.1.*"
View
69 composer.lock
@@ -1,5 +1,5 @@
{
- "hash": "d22b382f0e20509713b06e6991c82e07",
+ "hash": "142455bfe45755d318953c04880d43f2",
"packages": [
{
"package": "pimple/pimple",
@@ -28,11 +28,6 @@
{
"package": "symfony/class-loader",
"version": "dev-master",
- "source-reference": "c57e62c886899f8d88264efad23c857eb198dc09"
- },
- {
- "package": "symfony/class-loader",
- "version": "dev-master",
"source-reference": "c57e62c886899f8d88264efad23c857eb198dc09",
"alias-pretty-version": "2.1.x-dev",
"alias-version": "2.1.9999999.9999999-dev"
@@ -88,14 +83,14 @@
{
"package": "symfony/http-foundation",
"version": "dev-master",
- "source-reference": "5b1581418381f46679604fd19efc8b518f2390ae",
- "alias-pretty-version": "2.1.x-dev",
- "alias-version": "2.1.9999999.9999999-dev"
+ "source-reference": "5b1581418381f46679604fd19efc8b518f2390ae"
},
{
"package": "symfony/http-foundation",
"version": "dev-master",
- "source-reference": "5b1581418381f46679604fd19efc8b518f2390ae"
+ "source-reference": "5b1581418381f46679604fd19efc8b518f2390ae",
+ "alias-pretty-version": "2.1.x-dev",
+ "alias-version": "2.1.9999999.9999999-dev"
},
{
"package": "symfony/http-kernel",
@@ -124,21 +119,28 @@
{
"package": "symfony/routing",
"version": "dev-master",
- "source-reference": "aec133671d79d530d09aa0b5ca4cea9e4ee4b274"
+ "source-reference": "aec133671d79d530d09aa0b5ca4cea9e4ee4b274",
+ "alias-pretty-version": "2.1.x-dev",
+ "alias-version": "2.1.9999999.9999999-dev"
},
{
"package": "symfony/routing",
"version": "dev-master",
- "source-reference": "aec133671d79d530d09aa0b5ca4cea9e4ee4b274",
- "alias-pretty-version": "2.1.x-dev",
- "alias-version": "2.1.9999999.9999999-dev"
+ "source-reference": "aec133671d79d530d09aa0b5ca4cea9e4ee4b274"
}
],
"packages-dev": [
{
"package": "doctrine/common",
- "version": "2.2.x-dev",
- "source-reference": "1e0aa60d109c630d19543d999f12e2852ef8f932"
+ "version": "dev-master",
+ "source-reference": "8b403cde97eaede30bd79acab4f18895fd5bdf27",
+ "alias-pretty-version": "2.3.x-dev",
+ "alias-version": "2.3.9999999.9999999-dev"
+ },
+ {
+ "package": "doctrine/common",
+ "version": "dev-master",
+ "source-reference": "8b403cde97eaede30bd79acab4f18895fd5bdf27"
},
{
"package": "doctrine/dbal",
@@ -147,20 +149,19 @@
},
{
"package": "monolog/monolog",
- "version": "dev-master",
- "source-reference": "2eb0c0978d290a1c45346a1955188929cb4e5db7"
+ "version": "1.1.0"
},
{
"package": "symfony/form",
"version": "dev-master",
- "source-reference": "f410a9e3440b1248f8941d332d9e65b80e573ec5",
- "alias-pretty-version": "2.1.x-dev",
- "alias-version": "2.1.9999999.9999999-dev"
+ "source-reference": "f410a9e3440b1248f8941d332d9e65b80e573ec5"
},
{
"package": "symfony/form",
"version": "dev-master",
- "source-reference": "f410a9e3440b1248f8941d332d9e65b80e573ec5"
+ "source-reference": "f410a9e3440b1248f8941d332d9e65b80e573ec5",
+ "alias-pretty-version": "2.1.x-dev",
+ "alias-version": "2.1.9999999.9999999-dev"
},
{
"package": "symfony/locale",
@@ -175,9 +176,14 @@
"source-reference": "6a9e36fef0eedcd52b0dde361faab38f4615fe48"
},
{
- "package": "symfony/options-resolver",
+ "package": "symfony/monolog-bridge",
"version": "dev-master",
- "source-reference": "92c19fb262283a348093bbd32b5c5dfef8f00612",
+ "source-reference": "7ef27aa6b88447989fe5da551c2d2746d4cf60aa"
+ },
+ {
+ "package": "symfony/monolog-bridge",
+ "version": "dev-master",
+ "source-reference": "7ef27aa6b88447989fe5da551c2d2746d4cf60aa",
"alias-pretty-version": "2.1.x-dev",
"alias-version": "2.1.9999999.9999999-dev"
},
@@ -187,9 +193,11 @@
"source-reference": "92c19fb262283a348093bbd32b5c5dfef8f00612"
},
{
- "package": "symfony/translation",
+ "package": "symfony/options-resolver",
"version": "dev-master",
- "source-reference": "f89af1b91bbb9ee9151e3516aeaf1eda7992a369"
+ "source-reference": "92c19fb262283a348093bbd32b5c5dfef8f00612",
+ "alias-pretty-version": "2.1.x-dev",
+ "alias-version": "2.1.9999999.9999999-dev"
},
{
"package": "symfony/translation",
@@ -199,6 +207,11 @@
"alias-version": "2.1.9999999.9999999-dev"
},
{
+ "package": "symfony/translation",
+ "version": "dev-master",
+ "source-reference": "f89af1b91bbb9ee9151e3516aeaf1eda7992a369"
+ },
+ {
"package": "symfony/twig-bridge",
"version": "dev-master",
"source-reference": "a97473481d33d7d3d320bd4e4e95dd62a9b31891",
@@ -225,7 +238,7 @@
{
"package": "twig/twig",
"version": "dev-master",
- "source-reference": "e28663efd83d76ee36bf9e8a790f75bd880abbf2",
+ "source-reference": "45d0f6fe67e2199d1148cf1c7e832674af4e8e93",
"alias-pretty-version": "1.8.x-dev",
"alias-version": "1.8.9999999.9999999-dev"
},
@@ -239,7 +252,7 @@
{
"package": "twig/twig",
"version": "dev-master",
- "source-reference": "e28663efd83d76ee36bf9e8a790f75bd880abbf2"
+ "source-reference": "45d0f6fe67e2199d1148cf1c7e832674af4e8e93"
}
],
"aliases": [
View
2  doc/changelog.rst
@@ -3,6 +3,8 @@ Changelog
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 the ``translator.messages`` parameter (use
View
2  doc/services.rst
@@ -218,6 +218,8 @@ don't want to mess with most of them.
* **logger**: A
`http://api.symfony.com/master/Symfony/Component/HttpKernel/Log/LoggerInterface.html`_
instance. By default, logging is disabled as the value is set to `null`.
+ When the Symfony2 Monolog bridge is installed, Monolog is automatically used
+ as the default logger.
.. note::
View
20 src/Silex/Application.php
@@ -48,6 +48,9 @@ class Application extends \Pimple implements HttpKernelInterface, EventSubscribe
{
const VERSION = '@package_version@';
+ private $providers = array();
+ private $booted = false;
+
/**
* Constructor.
*/
@@ -147,9 +150,22 @@ public function register(ServiceProviderInterface $provider, array $values = arr
$this[$key] = $value;
}
+ $this->providers[] = $provider;
+
$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.
*
@@ -439,6 +455,10 @@ public function run(Request $request = null)
*/
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
+ if (!$this->booted) {
+ $this->boot();
+ }
+
$this->beforeDispatched = false;
$current = HttpKernelInterface::SUB_REQUEST === $type ? $this['request'] : $this['request_error'];
View
4 src/Silex/Provider/DoctrineServiceProvider.php
@@ -119,4 +119,8 @@ public function register(Application $app)
return $dbs[$app['dbs.default']];
});
}
+
+ public function boot(Application $app)
+ {
+ }
}
View
4 src/Silex/Provider/FormServiceProvider.php
@@ -52,4 +52,8 @@ public function register(Application $app)
return new DefaultCsrfProvider($app['form.secret']);
});
}
+
+ public function boot(Application $app)
+ {
+ }
}
View
4 src/Silex/Provider/HttpCacheServiceProvider.php
@@ -42,4 +42,8 @@ public function register(Application $app)
$app['http_cache.options'] = array();
}
}
+
+ public function boot(Application $app)
+ {
+ }
}
View
19 src/Silex/Provider/MonologServiceProvider.php
@@ -13,10 +13,8 @@
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
-
use Silex\Application;
use Silex\ServiceProviderInterface;
-
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
@@ -29,8 +27,16 @@ class MonologServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
- $app['monolog'] = $app->share(function () use ($app) {
- $log = new Logger(isset($app['monolog.name']) ? $app['monolog.name'] : 'myapp');
+ if ($bridge = class_exists('Symfony\Bridge\Monolog\Logger')) {
+ $app['logger'] = function () use ($app) {
+ return $app['monolog'];
+ };
+ }
+
+ $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');
$app['monolog.configure']($log);
@@ -50,14 +56,17 @@ public function register(Application $app)
return Logger::DEBUG;
};
}
+ }
+ public function boot(Application $app)
+ {
$app->before(function (Request $request) use ($app) {
$app['monolog']->addInfo('> '.$request->getMethod().' '.$request->getRequestUri());
});
$app->error(function (\Exception $e) use ($app) {
$app['monolog']->addError($e->getMessage());
- });
+ }, 255);
$app->after(function (Request $request, Response $response) use ($app) {
$app['monolog']->addInfo('< '.$response->getStatusCode());
View
7 src/Silex/Provider/SessionServiceProvider.php
@@ -49,8 +49,6 @@ public function register(Application $app)
);
});
- $app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), 128);
-
if (!isset($app['session.storage.options'])) {
$app['session.storage.options'] = array();
}
@@ -70,4 +68,9 @@ public function onKernelRequest($event)
$request->getSession()->start();
}
}
+
+ public function boot(Application $app)
+ {
+ $app['dispatcher']->addListener(KernelEvents::REQUEST, array($this, 'onKernelRequest'), 128);
+ }
}
View
11 src/Silex/Provider/SwiftmailerServiceProvider.php
@@ -76,14 +76,17 @@ public function register(Application $app)
return new \Swift_Events_SimpleEventDispatcher();
});
- $app->finish(function () use ($app) {
- $app['swiftmailer.spooltransport']->getSpool()->flushQueue($app['swiftmailer.transport']);
- });
-
if (isset($app['swiftmailer.class_path'])) {
require_once $app['swiftmailer.class_path'].'/Swift.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']);
+ });
+ }
}
View
4 src/Silex/Provider/TranslationServiceProvider.php
@@ -53,4 +53,8 @@ public function register(Application $app)
return new MessageSelector();
});
}
+
+ public function boot(Application $app)
+ {
+ }
}
View
4 src/Silex/Provider/TwigServiceProvider.php
@@ -90,4 +90,8 @@ public function register(Application $app)
));
});
}
+
+ public function boot(Application $app)
+ {
+ }
}
View
4 src/Silex/Provider/UrlGeneratorServiceProvider.php
@@ -31,4 +31,8 @@ public function register(Application $app)
return new UrlGenerator($app['routes'], $app['request_context']);
});
}
+
+ public function boot(Application $app)
+ {
+ }
}
View
4 src/Silex/Provider/ValidatorServiceProvider.php
@@ -43,4 +43,8 @@ public function register(Application $app)
return new ConstraintValidatorFactory();
});
}
+
+ public function boot(Application $app)
+ {
+ }
}
View
12 src/Silex/ServiceProviderInterface.php
@@ -21,7 +21,19 @@
/**
* 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
*/
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);
}
View
55 tests/Silex/Tests/Provider/MonologServiceProviderTest.php
@@ -12,10 +12,8 @@
namespace Silex\Tests\Provider;
use Monolog\Handler\TestHandler;
-
use Silex\Application;
use Silex\Provider\MonologServiceProvider;
-
use Symfony\Component\HttpFoundation\Request;
/**
@@ -32,24 +30,10 @@ public function setUp()
}
}
- public function testRegister()
+ public function testRequestLogging()
{
- $app = new Application();
-
- $app->register(new MonologServiceProvider());
-
- $app['monolog.handler'] = $app->share(function () use ($app) {
- return new TestHandler($app['monolog.level']);
- });
+ $app = $this->getApplication();
- return $app;
- }
-
- /**
- * @depends testRegister
- */
- public function testRequestLogging($app)
- {
$app->get('/foo', function () use ($app) {
return 'foo';
});
@@ -59,14 +43,15 @@ public function testRequestLogging($app)
$request = Request::create('/foo');
$app->handle($request);
- $this->assertTrue($app['monolog.handler']->hasInfoRecords());
+ $this->assertTrue($app['monolog.handler']->hasInfo('> GET /foo'));
+ $this->assertTrue($app['monolog.handler']->hasInfo('< 200'));
+ $this->assertTrue($app['monolog.handler']->hasInfo('Matched route "GET_foo" (parameters: "_controller": "{}", "_route": "GET_foo")'));
}
- /**
- * @depends testRegister
- */
- public function testManualLogging($app)
+ public function testManualLogging()
{
+ $app = $this->getApplication();
+
$app->get('/log', function () use ($app) {
$app['monolog']->addDebug('logging a message');
});
@@ -76,14 +61,13 @@ public function testManualLogging($app)
$request = Request::create('/log');
$app->handle($request);
- $this->assertTrue($app['monolog.handler']->hasDebugRecords());
+ $this->assertTrue($app['monolog.handler']->hasDebug('logging a message'));
}
- /**
- * @depends testRegister
- */
- public function testErrorLogging($app)
+ public function testErrorLogging()
{
+ $app = $this->getApplication();
+
$app->get('/error', function () {
throw new \RuntimeException('very bad error');
});
@@ -97,6 +81,19 @@ public function testErrorLogging($app)
$request = Request::create('/error');
$app->handle($request);
- $this->assertTrue($app['monolog.handler']->hasErrorRecords());
+ $this->assertTrue($app['monolog.handler']->hasError('very bad error'));
+ }
+
+ protected function getApplication()
+ {
+ $app = new Application();
+
+ $app->register(new MonologServiceProvider());
+
+ $app['monolog.handler'] = $app->share(function () use ($app) {
+ return new TestHandler($app['monolog.level']);
+ });
+
+ return $app;
}
}

No commit comments for this range

Something went wrong with that request. Please try again.