Permalink
Browse files

merged branch davedevelopment/shared-extend-docs (PR #33)

Commits
-------

2a3eee1 Attempt to clarify the need to re-share extended services
1cecce2 Fixes #32 - Better practice in twig extend example

Discussion
----------

Fixes #32 - Better practice in twig extend example

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

by stof at 2012-04-16T11:21:03Z

IMO, it should be a separate note telling it needs to be wrapped in ``share`` again after extending. But the first explanation about ``extend`` should not do it to keep things simple.

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

by igorw at 2012-04-16T11:26:13Z

What happens if twig already was shared?

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

by davedevelopment at 2012-04-16T11:31:59Z

@stof agreed, will come up with a simple example and rewrite the explanation for the twig

@igorw The initial twig instantiation is shared as expected, but the new Closure created by ``extend`` will call the Closure passed to it on each subsequent request

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

by stof at 2012-04-16T11:34:07Z

@igorw extend will allow you to extend it, however forgetting to share it again may lead to unexpected behaviors: the twig object will be shared (because of the inner closure sharing it) but the extra logic would be executed each time, meaning you would register your Twig extension each time you call Twig

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

by igorw at 2012-04-16T12:05:57Z

Re-sharing kind of fixes the problem, but it introduces two layers of sharing, I'm not sure this is a good thing.

share(extend(extendCallable(share(serviceDefinitionCallable))))

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

by stof at 2012-04-16T12:30:42Z

@igorw it is not really an issue as the outer sharing will take care to call the inner code only once. so the inner sharing is useless (and adds some memory overhead) but does not break things. And if you want to avoid this, it requires changing the way the sharing is done because there is no way to access the original closure inside the shared one.
  • Loading branch information...
fabpot committed Apr 20, 2012
2 parents 4e5fc6a + 2a3eee1 commit f593ea9beabd16bfe1d877a193ff4d0b9a909a5a
Showing with 19 additions and 6 deletions.
  1. +19 −6 README.rst
View
@@ -85,19 +85,32 @@ In some cases you may want to modify a service definition after it has been
defined. You can use the ``extend()`` method to add define additional code to
be run on your service just after it is created::
- $c['twig'] = $c->share(function ($c) {
- return new Twig_Environment($c['twig.loader'], $c['twig.options']);
- });
+ $container['mail'] = function ($c) {
+ return new \Zend_Mail();
+ };
- $c['twig'] = $c->extend('twig', function ($twig, $c) {
- $twig->addExtension(new MyTwigExtension());
- return $twig;
+ $container['mail'] = $c->extend('mail', function($mail, $c) {
+ $mail->setFrom($c['mail.default_from']);
+ return $mail;
});
The first argument is the name of the object, the second is a function that
gets access to the object instance and the container. The return value is
a service definition, so you need to re-assign it on the container.
+If the service you plan to extend is already shared, it's recommended that you
+re-wrap your extended service with the ``shared`` method, otherwise your extension
+code will be called every time you access the service::
+
+ $c['twig'] = $c->share(function ($c) {
+ return new Twig_Environment($c['twig.loader'], $c['twig.options']);
+ });
+
+ $c['twig'] = $c->share($c->extend('twig', function ($twig, $c) {
+ $twig->addExtension(new MyTwigExtension());
+ return $twig;
+ }));
+
Fetching the service creation function
--------------------------------------

0 comments on commit f593ea9

Please sign in to comment.