Commit fc8bbb62 authored by Fabien Potencier's avatar Fabien Potencier

switched to Pimple 2.0

parent c2421fab
......@@ -34,11 +34,11 @@ Using it in a template is as easy as before:
If you need to implement some logic independently of the asset, define a
service instead::
$app['asset_path'] = $app->share(function () {
$app['asset_path'] = function () {
// implement whatever logic you need to determine the asset path
return 'http://assets.examples.com';
});
};
Usage is exactly the same as before:
......@@ -49,7 +49,7 @@ Usage is exactly the same as before:
If the asset location depends on the asset type or path, you will need more
abstraction; here is one way to do that with a Twig function::
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$app->extend('twig', function($twig, $app) {
$twig->addFunction(new \Twig_SimpleFunction('asset', function ($asset) {
// implement whatever logic you need to determine the asset path
......@@ -57,7 +57,7 @@ abstraction; here is one way to do that with a Twig function::
}));
return $twig;
}));
});
The ``asset`` function can then be used in your templates:
......
......@@ -18,9 +18,9 @@ using the bundled handler, but each with a different channel.
});
foreach (array('auth', 'payments', 'stats') as $channel) {
$app['monolog.'.$channel] = $app->share(function ($app) use ($channel) {
$app['monolog.'.$channel] = function ($app) use ($channel) {
return $app['monolog.factory']($channel);
});
};
}
As your application grows, or your logging needs for certain areas of the
......@@ -31,13 +31,13 @@ particular service separately, including your customizations.
use Monolog\Handler\StreamHandler;
$app['monolog.payments'] = $app->share(function ($app) {
$app['monolog.payments'] = function ($app) {
$log = new $app['monolog.logger.class']('payments');
$handler = new StreamHandler($app['monolog.payments.logfile'], $app['monolog.payment.level']);
$log->pushHandler($handler);
return $log;
});
};
Alternatively, you could attempt to make the factory more complicated, and rely
on some conventions, such as checking for an array of handlers registered with
......@@ -62,10 +62,8 @@ the container with the channel name, defaulting to the bundled handler.
return $log;
});
$app['monolog.payments.handlers'] = $app->share(function ($app) {
$app['monolog.payments.handlers'] = function ($app) {
return array(
new StreamHandler(__DIR__.'/../payments.log', Logger::DEBUG),
);
});
};
......@@ -35,21 +35,21 @@ With a dedicated PDO service
'db_time_col' => 'session_time',
);
$app['pdo'] = $app->share(function () use ($app) {
$app['pdo'] = function () use ($app) {
return new PDO(
$app['pdo.dsn'],
$app['pdo.user'],
$app['pdo.password']
);
});
};
$app['session.storage.handler'] = $app->share(function () use ($app) {
$app['session.storage.handler'] = function () use ($app) {
return new PdoSessionHandler(
$app['pdo'],
$app['session.db_options'],
$app['session.storage.options']
);
});
};
Using the DoctrineServiceProvider
---------------------------------
......@@ -70,13 +70,13 @@ have to make another database connection, simply pass the getWrappedConnection m
'db_time_col' => 'session_time',
);
$app['session.storage.handler'] = $app->share(function () use ($app) {
$app['session.storage.handler'] = function () use ($app) {
return new PdoSessionHandler(
$app['db']->getWrappedConnection(),
$app['session.db_options'],
$app['session.storage.options']
);
});
};
Database structure
------------------
......
......@@ -168,28 +168,28 @@ form by adding constraints on the fields::
You can register form extensions by extending ``form.extensions``::
$app['form.extensions'] = $app->share($app->extend('form.extensions', function ($extensions) use ($app) {
$app->extend('form.extensions', function ($extensions) use ($app) {
$extensions[] = new YourTopFormExtension();
return $extensions;
}));
});
You can register form type extensions by extending ``form.type.extensions``::
$app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function ($extensions) use ($app) {
$app->extend('form.type.extensions', function ($extensions) use ($app) {
$extensions[] = new YourFormTypeExtension();
return $extensions;
}));
});
You can register form type guessers by extending ``form.type.guessers``::
$app['form.type.guessers'] = $app->share($app->extend('form.type.guessers', function ($guessers) use ($app) {
$app->extend('form.type.guessers', function ($guessers) use ($app) {
$guessers[] = new YourFormTypeGuesser();
return $guessers;
}));
});
Traits
------
......
......@@ -81,11 +81,11 @@ Customization
You can configure Monolog (like adding or changing the handlers) before using
it by extending the ``monolog`` service::
$app['monolog'] = $app->share($app->extend('monolog', function($monolog, $app) {
$app->extend('monolog', function($monolog, $app) {
$monolog->pushHandler(...);
return $monolog;
}));
});
By default, all requests, responses and errors are logged by an event listener
registered as a service called `monolog.listener`. You can replace or remove
......
......@@ -432,9 +432,9 @@ The ``users`` setting can be defined as a service that returns an instance of
`UserProviderInterface
<http://api.symfony.com/master/Symfony/Component/Security/Core/User/UserProviderInterface.html>`_::
'users' => $app->share(function () use ($app) {
'users' => function () use ($app) {
return new UserProvider($app['db']);
}),
},
Here is a simple example of a user provider, where Doctrine DBAL is used to
store the users::
......@@ -532,12 +532,12 @@ service::
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
$app['security.encoder.digest'] = $app->share(function ($app) {
$app['security.encoder.digest'] = function ($app) {
// use the sha1 algorithm
// don't base64 encode the password
// use only 1 iteration
return new MessageDigestPasswordEncoder('sha1', false, 1);
});
};
Defining a custom Authentication Provider
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -550,14 +550,14 @@ use in your configuration::
$app['security.authentication_listener.factory.wsse'] = $app->protect(function ($name, $options) use ($app) {
// define the authentication provider object
$app['security.authentication_provider.'.$name.'.wsse'] = $app->share(function () use ($app) {
$app['security.authentication_provider.'.$name.'.wsse'] = function () use ($app) {
return new WsseProvider($app['security.user_provider.default'], __DIR__.'/security_cache');
});
};
// define the authentication listener object
$app['security.authentication_listener.'.$name.'.wsse'] = $app->share(function () use ($app) {
$app['security.authentication_listener.'.$name.'.wsse'] = function () use ($app) {
return new WsseListener($app['security'], $app['security.authentication_manager']);
});
};
return array(
// the authentication provider id
......
......@@ -58,9 +58,9 @@ In this slightly contrived example of a blog API, we're going to change the
$app = new Application();
$app['posts.repository'] = $app->share(function() {
$app['posts.repository'] = function() {
return new PostRepository;
});
};
$app->get('/posts.json', function() use ($app) {
return $app->json($app['posts.repository']->findAll());
......@@ -109,8 +109,8 @@ followed by a single colon (:), followed by the method name.
.. code-block:: php
$app['posts.controller'] = $app->share(function() use ($app) {
$app['posts.controller'] = function() use ($app) {
return new PostController($app['posts.repository']);
});
};
$app->get('/posts.json', "posts.controller:indexJsonAction");
......@@ -144,7 +144,7 @@ translation files::
use Symfony\Component\Translation\Loader\YamlFileLoader;
$app['translator'] = $app->share($app->extend('translator', function($translator, $app) {
$app->extend('translator', function($translator, $app) {
$translator->addLoader('yaml', new YamlFileLoader());
$translator->addResource('yaml', __DIR__.'/locales/en.yml', 'en');
......@@ -152,7 +152,7 @@ translation files::
$translator->addResource('yaml', __DIR__.'/locales/fr.yml', 'fr');
return $translator;
}));
});
XLIFF-based language files
~~~~~~~~~~~~~~~~~~~~~~~~~~
......
......@@ -156,12 +156,12 @@ Customization
You can configure the Twig environment before using it by extending the
``twig`` service::
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$app->extend('twig', function($twig, $app) {
$twig->addGlobal('pi', 3.14);
$twig->addFilter('levenshtein', new \Twig_Filter_Function('levenshtein'));
return $twig;
}));
});
For more information, check out the `official Twig documentation
<http://twig.sensiolabs.org>`_.
......@@ -107,19 +107,6 @@ And to retrieve the service, use::
Every time you call ``$app['some_service']``, a new instance of the service is
created.
Shared services
~~~~~~~~~~~~~~~
You may want to use the same instance of a service across all of your code. In
order to do that you can make a *shared* service::
$app['some_service'] = $app->share(function () {
return new Service();
});
This will create the service on first invocation, and then return the existing
instance on any subsequent access.
Access container from closure
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
......@@ -139,17 +126,13 @@ options. The dependency is only created when ``some_service`` is accessed, and
it is possible to replace either of the dependencies by simply overriding
those definitions.
.. note::
This also works for shared services.
Going back to our initial example, here's how we could use the container
to manage its dependencies::
$app['user.persist_path'] = '/tmp/users';
$app['user.persister'] = $app->share(function ($app) {
$app['user.persister'] = function ($app) {
return new JsonUserPersister($app['user.persist_path']);
});
};
Protected closures
......@@ -230,10 +213,6 @@ don't want to mess with most of them.
the ``MonologServiceProvider`` or define your own ``logger`` service that
conforms to the PSR logger interface.
.. note::
All of these Silex core services are shared.
Core parameters
---------------
......
......@@ -375,9 +375,9 @@ converter based on Doctrine ObjectManager::
The service will now be registered in the application, and the
convert method will be used as converter::
$app['converter.user'] = $app->share(function () {
$app['converter.user'] = function () {
return new UserConverter();
});
};
$app->get('/user/{user}', function (User $user) {
// ...
......
......@@ -65,29 +65,29 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
$app = $this;
$this['routes'] = $this->share(function () {
$this['routes'] = function () {
return new RouteCollection();
});
};
$this['controllers'] = $this->share(function () use ($app) {
$this['controllers'] = function () use ($app) {
return $app['controllers_factory'];
});
};
$this['controllers_factory'] = function () use ($app) {
$this['controllers_factory'] = $this->factory(function () use ($app) {
return new ControllerCollection($app['route_factory']);
};
});
$this['route_class'] = 'Silex\\Route';
$this['route_factory'] = function () use ($app) {
return new $app['route_class']();
};
$this['exception_handler'] = $this->share(function () use ($app) {
$this['exception_handler'] = function () use ($app) {
return new ExceptionHandler($app['debug']);
});
};
$this['dispatcher_class'] = 'Symfony\\Component\\EventDispatcher\\EventDispatcher';
$this['dispatcher'] = $this->share(function () use ($app) {
$this['dispatcher'] = function () use ($app) {
$dispatcher = new $app['dispatcher_class']();
if (isset($app['exception_handler'])) {
......@@ -99,23 +99,23 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
$dispatcher->addSubscriber(new StringToResponseListener());
return $dispatcher;
});
};
$this['callback_resolver'] = $this->share(function () use ($app) {
$this['callback_resolver'] = function () use ($app) {
return new CallbackResolver($app);
});
};
$this['resolver'] = $this->share(function () use ($app) {
$this['resolver'] = function () use ($app) {
return new ControllerResolver($app, $app['logger']);
});
};
$this['kernel'] = $this->share(function () use ($app) {
$this['kernel'] = function () use ($app) {
return new HttpKernel($app['dispatcher'], $app['resolver'], $app['request_stack']);
});
};
$this['request_stack'] = $this->share(function () use ($app) {
$this['request_stack'] = function () use ($app) {
return new RequestStack();
});
};
$this['request.http_port'] = 80;
$this['request.https_port'] = 443;
......@@ -270,11 +270,11 @@ class Application extends \Pimple implements HttpKernelInterface, TerminableInte
return;
}
$this['dispatcher'] = $this->share($this->extend('dispatcher', function ($dispatcher, $app) use ($callback, $priority, $eventName) {
$this->extend('dispatcher', function ($dispatcher, $app) use ($callback, $priority, $eventName) {
$dispatcher->addListener($eventName, $app['callback_resolver']->resolveCallback($callback), $priority);
return $dispatcher;
}));
});
}
/**
......
......@@ -59,7 +59,7 @@ class DoctrineServiceProvider implements ServiceProviderInterface
$app['dbs.options'] = $tmp;
});
$app['dbs'] = $app->share(function ($app) {
$app['dbs'] = function ($app) {
$app['dbs.options.initializer']();
$dbs = new \Pimple();
......@@ -73,15 +73,15 @@ class DoctrineServiceProvider implements ServiceProviderInterface
$manager = $app['dbs.event_manager'][$name];
}
$dbs[$name] = $dbs->share(function ($dbs) use ($options, $config, $manager) {
$dbs[$name] = function ($dbs) use ($options, $config, $manager) {
return DriverManager::getConnection($options, $config, $manager);
});
};
}
return $dbs;
});
};
$app['dbs.config'] = $app->share(function ($app) {
$app['dbs.config'] = function ($app) {
$app['dbs.options.initializer']();
$configs = new \Pimple();
......@@ -94,9 +94,9 @@ class DoctrineServiceProvider implements ServiceProviderInterface
}
return $configs;
});
};
$app['dbs.event_manager'] = $app->share(function ($app) {
$app['dbs.event_manager'] = function ($app) {
$app['dbs.options.initializer']();
$managers = new \Pimple();
......@@ -105,25 +105,25 @@ class DoctrineServiceProvider implements ServiceProviderInterface
}
return $managers;
});
};
// shortcuts for the "first" DB
$app['db'] = $app->share(function ($app) {
$app['db'] = function ($app) {
$dbs = $app['dbs'];
return $dbs[$app['dbs.default']];
});
};
$app['db.config'] = $app->share(function ($app) {
$app['db.config'] = function ($app) {
$dbs = $app['dbs.config'];
return $dbs[$app['dbs.default']];
});
};
$app['db.event_manager'] = $app->share(function ($app) {
$app['db.event_manager'] = function ($app) {
$dbs = $app['dbs.event_manager'];
return $dbs[$app['dbs.default']];
});
};
}
}
......@@ -46,23 +46,23 @@ class FormServiceProvider implements ServiceProviderInterface
$app['form.secret'] = md5(__DIR__);
$app['form.type.extensions'] = $app->share(function ($app) {
$app['form.type.extensions'] = function ($app) {
return array();
});
};
$app['form.type.guessers'] = $app->share(function ($app) {
$app['form.type.guessers'] = function ($app) {
return array();
});
};
$app['form.extension.csrf'] = $app->share(function ($app) {
$app['form.extension.csrf'] = function ($app) {
if (isset($app['translator'])) {
return new CsrfExtension($app['form.csrf_provider'], $app['translator']);
}
return new CsrfExtension($app['form.csrf_provider']);
});
};
$app['form.extensions'] = $app->share(function ($app) {
$app['form.extensions'] = function ($app) {
$extensions = array(
$app['form.extension.csrf'],
new HttpFoundationExtension(),
......@@ -78,9 +78,9 @@ class FormServiceProvider implements ServiceProviderInterface
}
return $extensions;
});
};
$app['form.factory'] = $app->share(function ($app) {
$app['form.factory'] = function ($app) {
return Forms::createFormFactoryBuilder()
->addExtensions($app['form.extensions'])
->addTypeExtensions($app['form.type.extensions'])
......@@ -88,18 +88,18 @@ class FormServiceProvider implements ServiceProviderInterface
->setResolvedTypeFactory($app['form.resolved_type_factory'])
->getFormFactory()
;
});
};
$app['form.resolved_type_factory'] = $app->share(function ($app) {
$app['form.resolved_type_factory'] = function ($app) {
return new ResolvedFormTypeFactory();
});
};
$app['form.csrf_provider'] = $app->share(function ($app) {
$app['form.csrf_provider'] = function ($app) {
if (isset($app['session'])) {
return new SessionCsrfProvider($app['session'], $app['form.secret']);
}
return new DefaultCsrfProvider($app['form.secret']);
});
};
}
}
......@@ -28,7 +28,7 @@ class HttpCacheServiceProvider implements ServiceProviderInterface, EventListene
{
public function register(\Pimple $app)
{
$app['http_cache'] = $app->share(function ($app) {
$app['http_cache'] = function ($app) {
$app['http_cache.options'] = array_replace(
array(
'debug' => isset($app['debug']) ? $app['debug'] : false,
......@@ -36,19 +36,19 @@ class HttpCacheServiceProvider implements ServiceProviderInterface, EventListene
);
return new HttpCache($app, $app['http_cache.store'], $app['http_cache.esi'], $app['http_cache.options']);
});
};
$app['http_cache.esi'] = $app->share(function ($app) {
$app['http_cache.esi'] = function ($app) {
return new Esi();
});
};
$app['http_cache.store'] = $app->share(function ($app) {
$app['http_cache.store'] = function ($app) {
return new Store($app['http_cache.cache_dir']);
});
};
$app['http_cache.esi_listener'] = $app->share(function ($app) {
$app['http_cache.esi_listener'] = function ($app) {
return new EsiListener($app['http_cache.esi']);
});
};
$app['http_cache.options'] = array();
}
......
......@@ -30,43 +30,43 @@ class HttpFragmentServiceProvider implements ServiceProviderInterface, EventList
{
public function register(\Pimple $app)
{
$app['fragment.handler'] = $app->share(function ($app) {
$app['fragment.handler'] = function ($app) {
return new FragmentHandler($app['fragment.renderers'], isset($app['debug']) ? $app['debug'] : false, $app['request_stack']);
});
};
$app['fragment.renderer.inline'] = $app->share(function ($app) {
$app['fragment.renderer.inline'] = function ($app) {
$renderer = new InlineFragmentRenderer($app['kernel'], $app['dispatcher']);
$renderer->setFragmentPath($app['fragment.path']);
return $renderer;
});
};
$app['fragment.renderer.hinclude'] = $app->share(function ($app) {
$app['fragment.renderer.hinclude'] = function ($app) {
$renderer = new HIncludeFragmentRenderer(null, $app['uri_signer'], $app['fragment.renderer.hinclude.global_template'], isset($app['charset']) ? $app['charset'] : 'UTF-8');
$renderer->setFragmentPath($app['fragment.path']);
return $renderer;
});
};
$app['fragment.renderer.esi'] = $app->share(function ($app) {
$app['fragment.renderer.esi'] = function ($app) {
$renderer = new EsiFragmentRenderer($app['http_cache.esi'], $app['fragment.renderer.inline']);
$renderer->setFragmentPath($app['fragment.path']);
return $renderer;
});
};
$app['fragment.listener'] = $app->share(function ($app) {
$app['fragment.listener'] = function ($app) {
return new FragmentListener($app['uri_signer'], $app['fragment.path']);
});
};
$app['uri_signer'] = $app->share(function ($app) {
$app['uri_signer'] = function ($app) {
return new UriSigner($app['uri_signer.secret']);
});
};
$app['uri_signer.secret'] = md5(__DIR__);
$app['fragment.path'] = '/_fragment';
$app['fragment.renderer.hinclude.global_template'] = null;
$app['fragment.renderers'] = $app->share(function ($app) {
$app['fragment.renderers'] = function ($app) {
$renderers = array($app['fragment.renderer.inline'], $app['fragment.renderer.hinclude']);
if (isset($app['http_cache.esi'])) {
......@@ -74,7 +74,7 @@ class HttpFragmentServiceProvider implements ServiceProviderInterface, EventList
}
return $renderers;
});
};
}
public function subscribe(\Pimple $app, EventDispatcherInterface $dispatcher)
......
......@@ -26,7 +26,7 @@ class LocaleServiceProvider implements ServiceProviderInterface, EventListenerPr
{
public function register(\Pimple $app)
{
$app['locale.listener'] = $app->share(function ($app) {
$app['locale.listener'] = function ($app) {
$urlMatcher = null;
if (isset($app['url_matcher'])) {
$urlMatcher = new LazyUrlMatcher(function () use ($app) {
......@@ -35,7 +35,7 @@ class LocaleServiceProvider implements ServiceProviderInterface, EventListenerPr
}
return new LocaleListener($app, $urlMatcher, $app['request_stack']);
});
};
$app['locale'] = 'en';
}
......
......@@ -45,7 +45,7 @@ class MonologServiceProvider implements ServiceProviderInterface, BootableProvid
$app['monolog.logger.class'] = $bridge ? 'Symfony\Bridge\Monolog\Logger' : 'Monolog\Logger';
$app['monolog'] = $app->share(function ($app) {
$app['monolog'] = function ($app) {
$log = new $app['monolog.logger.class']($app['monolog.name']);
$log->pushHandler($app['monolog.handler']);
......@@ -55,7 +55,7 @@ class MonologServiceProvider implements ServiceProviderInterface, BootableProvid
}
return $log;
});
};
$app['monolog.handler'] = function () use ($app) {
$level = MonologServiceProvider::translateLevel($app['monolog.level']);
......@@ -67,9 +67,9 @@ class MonologServiceProvider implements ServiceProviderInterface, BootableProvid
return Logger::DEBUG;
};
$app['monolog.listener'] = $app->share(function () use ($app) {
$app['monolog.listener'] = function () use ($app) {
return new LogListener($app['logger']);
});
};
$app['monolog.name'] = 'myapp';
}
......
......@@ -28,13 +28,13 @@ class RememberMeServiceProvider implements ServiceProviderInterface, EventListen
{
public function register(\Pimple $app)
{
$app['security.remember_me.response_listener'] = $app->share(function ($app) {
$app['security.remember_me.response_listener'] = function ($app) {
if (!isset($app['security'])) {
throw new \LogicException('You must register the SecurityServiceProvider to use the RememberMeServiceProvider');
}
return new ResponseListener();
});
};
$app['security.authentication_listener.factory.remember_me'] = $app->protect(function ($name, $options) use ($app) {
if (empty($options['key'])) {
......@@ -62,7 +62,7 @@ class RememberMeServiceProvider implements ServiceProviderInterface, EventListen
});
$app['security.remember_me.service._proto'] = $app->protect(function ($providerKey, $options) use ($app) {
return $app->share(function () use ($providerKey, $options, $app) {
return function () use ($providerKey, $options, $app) {
$options = array_replace(array(
'name' => 'REMEMBERME',
'lifetime' => 31536000,
......@@ -75,11 +75,11 @@ class RememberMeServiceProvider implements ServiceProviderInterface, EventListen
), $options);
return new TokenBasedRememberMeServices(array($app['security.user_provider.'.$providerKey]), $options['key'], $providerKey, $options, isset($app['logger']) ? $app['logger'] : null);
});
};
});
$app['security.authentication_listener.remember_me._proto'] = $app->protect(function ($providerKey) use ($app) {
return $app->share(function () use ($app, $providerKey) {
return function () use ($app, $providerKey) {
$listener = new RememberMeListener(
$app['security'],
$app['security.remember_me.service.'.$providerKey],
......@@ -88,13 +88,13 @@ class RememberMeServiceProvider implements ServiceProviderInterface, EventListen
);
return $listener;
});
};
});
$app['security.authentication_provider.remember_me._proto'] = $app->protect(function ($name, $options) use ($app) {
return $app->share(function () use ($app, $name, $options) {
return function () use ($app, $name, $options) {
return new RememberMeAuthenticationProvider($app['security.user_checker'], $options['key'], $name);
});
};
});
}
......
......@@ -29,30 +29,30 @@ class RoutingServiceProvider implements ServiceProviderInterface, EventListenerP
{
public function register(\Pimple $app)
{
$app['url_generator'] = $app->share(function ($app) {
$app['url_generator'] = function ($app) {
return new UrlGenerator($app['routes'], $app['request_context']);
});
};
$app['url_matcher'] = $app->share(function () use ($app) {
$app['url_matcher'] = function () use ($app) {
return new RedirectableUrlMatcher($app['routes'], $app['request_context']);
});
};
$app['request_context'] = $app->share(function () use ($app) {
$app['request_context'] = function () use ($app) {
$context = new RequestContext();
$context->setHttpPort(isset($app['request.http_port']) ? $app['request.http_port'] : 80);
$context->setHttpsPort(isset($app['request.https_port']) ? $app['request.https_port'] : 443);
return $context;
});
};
$app['routing.listener'] = $app->share(function () use ($app) {
$app['routing.listener'] = function () use ($app) {
$urlMatcher = new LazyUrlMatcher(function () use ($app) {
return $app['url_matcher'];
});
return new RouterListener($urlMatcher, $app['request_context'], isset($app['logger']) ? $app['logger'] : null, $app['request_stack']);
});
};
}
public function subscribe(\Pimple $app, EventDispatcherInterface $dispatcher)
......
......@@ -36,16 +36,16 @@ class SerializerServiceProvider implements ServiceProviderInterface
*/
public function register(\Pimple $app)
{
$app['serializer'] = $app->share(function () use ($app) {
$app['serializer'] = function () use ($app) {
return new Serializer($app['serializer.normalizers'], $app['serializer.encoders']);
});
};
$app['serializer.encoders'] = $app->share(function () {
$app['serializer.encoders'] = function () {
return array(new JsonEncoder(), new XmlEncoder());
});
};
$app['serializer.normalizers'] = $app->share(function () {
$app['serializer.normalizers'] = function () {
return array(new CustomNormalizer(), new GetSetMethodNormalizer());
});
};
}
}
......@@ -18,8 +18,8 @@ class ServiceControllerServiceProvider implements ServiceProviderInterface
{
public function register(\Pimple $app)
{
$app['resolver'] = $app->share($app->extend('resolver', function ($resolver, $app) {
$app->extend('resolver', function ($resolver, $app) {
return new ServiceControllerResolver($resolver, $app['callback_resolver']);
}));
});
}
}
......@@ -36,7 +36,7 @@ class SessionServiceProvider implements ServiceProviderInterface, EventListenerP
$app['session.test'] = false;
$app['session'] = $app->share(function ($app) {
$app['session'] = function ($app) {
if (!isset($app['session.storage'])) {
if ($app['session.test']) {
$app['session.storage'] = $app['session.storage.test'];
......@@ -46,30 +46,30 @@ class SessionServiceProvider implements ServiceProviderInterface, EventListenerP
}
return new Session($app['session.storage']);
});
};
$app['session.storage.handler'] = $app->share(function ($app) {
$app['session.storage.handler'] = function ($app) {
return new NativeFileSessionHandler($app['session.storage.save_path']);
});
};
$app['session.storage.native'] = $app->share(function ($app) {
$app['session.storage.native'] = function ($app) {
return new NativeSessionStorage(
$app['session.storage.options'],
$app['session.storage.handler']
);
});
};
$app['session.listener'] = $app->share(function ($app) {
$app['session.listener'] = function ($app) {
return new SessionListener($app);
});
};
$app['session.storage.test'] = $app->share(function () {
$app['session.storage.test'] = function () {
return new MockFileSessionStorage();
});
};
$app['session.listener.test'] = $app->share(function ($app) {
$app['session.listener.test'] = function ($app) {
return new TestSessionListener($app);
});
};
$app['session.storage.options'] = array();
$app['session.default_locale'] = 'en';
......
......@@ -30,21 +30,21 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface, EventListe
$app['mailer.initialized'] = false;
$app['mailer'] = $app->share(function ($app) {
$app['mailer'] = function ($app) {
$app['mailer.initialized'] = true;
return new \Swift_Mailer($app['swiftmailer.spooltransport']);
});
};
$app['swiftmailer.spooltransport'] = $app->share(function ($app) {
$app['swiftmailer.spooltransport'] = function ($app) {
return new \Swift_SpoolTransport($app['swiftmailer.spool']);
});
};
$app['swiftmailer.spool'] = $app->share(function ($app) {
$app['swiftmailer.spool'] = function ($app) {
return new \Swift_MemorySpool();
});
};
$app['swiftmailer.transport'] = $app->share(function ($app) {
$app['swiftmailer.transport'] = function ($app) {
$transport = new \Swift_Transport_EsmtpTransport(
$app['swiftmailer.transport.buffer'],
array($app['swiftmailer.transport.authhandler']),
......@@ -68,23 +68,23 @@ class SwiftmailerServiceProvider implements ServiceProviderInterface, EventListe
$transport->setAuthMode($options['auth_mode']);
return $transport;
});
};
$app['swiftmailer.transport.buffer'] = $app->share(function () {
$app['swiftmailer.transport.buffer'] = function () {
return new \Swift_Transport_StreamBuffer(new \Swift_StreamFilters_StringReplacementFilterFactory());
});
};
$app['swiftmailer.transport.authhandler'] = $app->share(function () {
$app['swiftmailer.transport.authhandler'] = function () {
return new \Swift_Transport_Esmtp_AuthHandler(array(
new \Swift_Transport_Esmtp_Auth_CramMd5Authenticator(),
new \Swift_Transport_Esmtp_Auth_LoginAuthenticator(),
new \Swift_Transport_Esmtp_Auth_PlainAuthenticator(),
));
});
};
$app['swiftmailer.transport.eventdispatcher'] = $app->share(function () {
$app['swiftmailer.transport.eventdispatcher'] = function () {
return new \Swift_Events_SimpleEventDispatcher();
});
};
}
public function subscribe(\Pimple $app, EventDispatcherInterface $dispatcher)
......
......@@ -26,7 +26,7 @@ class TranslationServiceProvider implements ServiceProviderInterface
{
public function register(\Pimple $app)
{
$app['translator'] = $app->share(function ($app) {
$app['translator'] = function ($app) {
if (!isset($app['locale'])) {
throw new \LogicException('You must register the LocaleServiceProvider to use the TranslationServiceProvider');
}
......@@ -43,11 +43,11 @@ class TranslationServiceProvider implements ServiceProviderInterface
}
return $translator;
});
};
$app['translator.message_selector'] = $app->share(function () {
$app['translator.message_selector'] = function () {
return new MessageSelector();
});
};
$app['translator.domains'] = array();
$app['locale_fallbacks'] = array('en');
......
......@@ -34,7 +34,7 @@ class TwigServiceProvider implements ServiceProviderInterface
$app['twig.path'] = array();
$app['twig.templates'] = array();
$app['twig'] = $app->share(function ($app) {
$app['twig'] = function ($app) {
$app['twig.options'] = array_replace(
array(
'charset' => isset($app['charset']) ? $app['charset'] : 'UTF-8',
......@@ -70,13 +70,13 @@ class TwigServiceProvider implements ServiceProviderInterface
}
if (isset($app['form.factory'])) {
$app['twig.form.engine'] = $app->share(function ($app) {
$app['twig.form.engine'] = function ($app) {
return new TwigRendererEngine($app['twig.form.templates']);
});
};
$app['twig.form.renderer'] = $app->share(function ($app) {
$app['twig.form.renderer'] = function ($app) {
return new TwigRenderer($app['twig.form.engine'], $app['form.csrf_provider']);
});
};
$twig->addExtension(new FormExtension($app['twig.form.renderer']));
......@@ -88,21 +88,21 @@ class TwigServiceProvider implements ServiceProviderInterface
}
return $twig;
});
};
$app['twig.loader.filesystem'] = $app->share(function ($app) {
$app['twig.loader.filesystem'] = function ($app) {
return new \Twig_Loader_Filesystem($app['twig.path']);
});
};
$app['twig.loader.array'] = $app->share(function ($app) {
$app['twig.loader.array'] = function ($app) {
return new \Twig_Loader_Array($app['twig.templates']);
});
};
$app['twig.loader'] = $app->share(function ($app) {
$app['twig.loader'] = function ($app) {
return new \Twig_Loader_Chain(array(
$app['twig.loader.array'],
$app['twig.loader.filesystem'],
));
});
};
}
}
......@@ -27,7 +27,7 @@ class ValidatorServiceProvider implements ServiceProviderInterface
{
public function register(\Pimple $app)
{
$app['validator'] = $app->share(function ($app) {
$app['validator'] = function ($app) {
$r = new \ReflectionClass('Symfony\Component\Validator\Validator');
if (isset($app['translator'])) {
......@@ -41,20 +41,20 @@ class ValidatorServiceProvider implements ServiceProviderInterface
'validators',
$app['validator.object_initializers']
);
});
};
$app['validator.mapping.class_metadata_factory'] = $app->share(function ($app) {
$app['validator.mapping.class_metadata_factory'] = function ($app) {
return new ClassMetadataFactory(new StaticMethodLoader());
});
};
$app['validator.validator_factory'] = $app->share(function () use ($app) {
$app['validator.validator_factory'] = function () use ($app) {
$validators = isset($app['validator.validator_service_ids']) ? $app['validator.validator_service_ids'] : array();
return new ConstraintValidatorFactory($app, $validators);
});
};
$app['validator.object_initializers'] = $app->share(function ($app) {
$app['validator.object_initializers'] = function ($app) {
return array();
});
};
}
}
......@@ -39,9 +39,9 @@ class MonologTraitTest extends \PHPUnit_Framework_TestCase
{
$app = new MonologApplication();
$app->register(new MonologServiceProvider(), array(
'monolog.handler' => $app->share(function () use ($app) {
'monolog.handler' => function () use ($app) {
return new TestHandler($app['monolog.level']);
}),
},
));
return $app;
......
......@@ -28,9 +28,9 @@ class CallbackServicesTest extends \PHPUnit_Framework_TestCase
{
$app = new Application();
$app['service'] = $app->share(function () {
$app['service'] = function () {
return new self();
});
};
$app->before('service:beforeApp');
$app->after('service:afterApp');
......
......@@ -22,11 +22,11 @@ class LazyDispatcherTest extends \PHPUnit_Framework_TestCase
$dispatcherCreated = false;
$app = new Application();
$app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, $app) use (&$dispatcherCreated) {
$app->extend('dispatcher', function ($dispatcher, $app) use (&$dispatcherCreated) {
$dispatcherCreated = true;
return $dispatcher;
}));
});
$app->before(function () {});
......
......@@ -35,11 +35,11 @@ class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new FormServiceProvider());
$app['form.type.extensions'] = $app->share($app->extend('form.type.extensions', function($extensions) {
$app->extend('form.type.extensions', function($extensions) {
$extensions[] = new DummyFormTypeExtension();
return $extensions;
}));
});
$form = $app['form.factory']->createBuilder('form', array())
->add('file', 'file', array('image_path' => 'webPath'))
......@@ -54,11 +54,11 @@ class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new FormServiceProvider());
$app['form.type.guessers'] = $app->share($app->extend('form.type.guessers', function($guessers) {
$app->extend('form.type.guessers', function($guessers) {
$guessers[] = new FormTypeGuesserChain(array());
return $guessers;
}));
});
$this->assertInstanceOf('Symfony\Component\Form\FormFactory', $app['form.factory']);
}
......@@ -78,9 +78,9 @@ class FormServiceProviderTest extends \PHPUnit_Framework_TestCase
);
$app['locale'] = 'de';
$app['form.csrf_provider'] = $app->share(function () {
$app['form.csrf_provider'] = function () {
return new FakeCsrfProvider();
});
};
$form = $app['form.factory']->createBuilder('form', array())
->getForm();
......
......@@ -185,11 +185,11 @@ class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new MonologServiceProvider());
$app['monolog.handler'] = $app->share(function () use ($app) {
$app['monolog.handler'] = function () use ($app) {
$level = MonologServiceProvider::translateLevel($app['monolog.level']);
return new TestHandler($level);
});
};
return $app;
}
......
......@@ -34,9 +34,9 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new SwiftmailerServiceProvider());
$app->boot();
$app['swiftmailer.spool'] = $app->share(function () {
$app['swiftmailer.spool'] = function () {
return new SpoolStub();
});
};
$app->get('/', function() use ($app) {
$app['mailer']->send(\Swift_Message::newInstance());
......@@ -60,9 +60,9 @@ class SwiftmailerServiceProviderTest extends \PHPUnit_Framework_TestCase
$app->register(new SwiftmailerServiceProvider());
$app->boot();
$app['swiftmailer.spool'] = $app->share(function () {
$app['swiftmailer.spool'] = function () {
return new SpoolStub();
});
};
$app->get('/', function() use ($app) { });
......
......@@ -47,9 +47,9 @@ class TwigServiceProviderTest extends \PHPUnit_Framework_TestCase
));
$loader = $this->getMock('\Twig_LoaderInterface');
$loader->expects($this->never())->method('getSource');
$app['twig.loader.filesystem'] = $app->share(function ($app) use ($loader) {
$app['twig.loader.filesystem'] = function ($app) use ($loader) {
return $loader;
});
};
$this->assertEquals('foo', $app['twig.loader']->getSource('foo'));
}
}
......@@ -28,8 +28,8 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
public function testRegister()
{
$app = new Application();
$app->register(new ValidatorServiceProvider());
$app->register(new FormServiceProvider());
return $app;
}
......@@ -38,9 +38,9 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
{
$app = new Application();
$app['custom.validator'] = $app->share(function() {
$app['custom.validator'] = function() {
return new CustomValidator();
});
};
$app->register(new ValidatorServiceProvider(), array(
'validator.validator_service_ids' => array(
......@@ -76,9 +76,6 @@ class ValidatorServiceProviderTest extends \PHPUnit_Framework_TestCase
*/
public function testValidatorConstraint($email, $isValid, $nbGlobalError, $nbEmailError, $app)
{
$app->register(new ValidatorServiceProvider());
$app->register(new FormServiceProvider());
$constraints = new Assert\Collection(array(
'email' => array(
new Assert\NotBlank(),
......
......@@ -23,6 +23,7 @@ class ServiceControllerResolverRouterTest extends RouterTest
public function testServiceNameControllerSyntax()
{
$app = new Application();
$app->register(new ServiceControllerServiceProvider());
$app['service_name'] = function () {
return new MyController();
......@@ -35,8 +36,6 @@ class ServiceControllerResolverRouterTest extends RouterTest
protected function checkRouteResponse(Application $app, $path, $expectedContent, $method = 'get', $message = null)
{
$app->register(new ServiceControllerServiceProvider());
$request = Request::create($path, $method);
$response = $app->handle($request);
$this->assertEquals($expectedContent, $response->getContent(), $message);
......
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