Skip to content
This repository has been archived by the owner on Jul 4, 2018. It is now read-only.

Commit

Permalink
switched to Pimple 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Apr 29, 2014
1 parent c2421fa commit fc8bbb6
Show file tree
Hide file tree
Showing 38 changed files with 282 additions and 309 deletions.
2 changes: 1 addition & 1 deletion composer.json
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": ">=5.3.3",
"pimple/pimple": "~1.0",
"pimple/pimple": "~2.0",
"symfony/event-dispatcher": ">=2.4,<2.6-dev",
"symfony/http-foundation": ">=2.4,<2.6-dev",
"symfony/http-kernel": ">=2.4,<2.6-dev",
Expand Down
8 changes: 4 additions & 4 deletions doc/cookbook/assets.rst
Expand Up @@ -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:

Expand All @@ -49,15 +49,15 @@ 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

return sprintf('http://assets.examples.com/%s', ltrim($asset, '/'));
}));

return $twig;
}));
});

The ``asset`` function can then be used in your templates:

Expand Down
14 changes: 6 additions & 8 deletions doc/cookbook/multiple_loggers.rst
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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),
);
});
};
12 changes: 6 additions & 6 deletions doc/cookbook/session_storage.rst
Expand Up @@ -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
---------------------------------
Expand All @@ -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
------------------
Expand Down
12 changes: 6 additions & 6 deletions doc/providers/form.rst
Expand Up @@ -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
------
Expand Down
4 changes: 2 additions & 2 deletions doc/providers/monolog.rst
Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions doc/providers/security.rst
Expand Up @@ -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::
Expand Down Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions doc/providers/service_controller.rst
Expand Up @@ -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());
Expand Down Expand Up @@ -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");
4 changes: 2 additions & 2 deletions doc/providers/translation.rst
Expand Up @@ -144,15 +144,15 @@ 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');
$translator->addResource('yaml', __DIR__.'/locales/de.yml', 'de');
$translator->addResource('yaml', __DIR__.'/locales/fr.yml', 'fr');

return $translator;
}));
});

XLIFF-based language files
~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions doc/providers/twig.rst
Expand Up @@ -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>`_.
25 changes: 2 additions & 23 deletions doc/services.rst
Expand Up @@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand All @@ -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
Expand Down Expand Up @@ -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
---------------

Expand Down
4 changes: 2 additions & 2 deletions doc/usage.rst
Expand Up @@ -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) {
// ...
Expand Down

0 comments on commit fc8bbb6

Please sign in to comment.