3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\ServiceManager\Proxy
;
12 use ProxyManager\Factory\LazyLoadingValueHolderFactory
;
14 use ProxyManager\Proxy\LazyLoadingInterface
;
15 use Zend\ServiceManager\DelegatorFactoryInterface
;
16 use Zend\ServiceManager\Exception
;
17 use Zend\ServiceManager\ServiceLocatorInterface
;
20 * Delegator factory responsible of instantiating lazy loading value holder proxies of
21 * given services at runtime
23 * @link https://github.com/Ocramius/ProxyManager/blob/master/docs/lazy-loading-value-holder.md
25 class LazyServiceFactory
implements DelegatorFactoryInterface
28 * @var \ProxyManager\Factory\LazyLoadingValueHolderFactory
30 protected $proxyFactory;
33 * @var string[] map of service names to class names
35 protected $servicesMap;
38 * @param LazyLoadingValueHolderFactory $proxyFactory
39 * @param string[] $servicesMap a map of service names to class names of their
42 public function __construct(LazyLoadingValueHolderFactory
$proxyFactory, array $servicesMap)
44 $this->proxyFactory
= $proxyFactory;
45 $this->servicesMap
= $servicesMap;
51 * @return object|\ProxyManager\Proxy\LazyLoadingInterface|\ProxyManager\Proxy\ValueHolderInterface
53 public function createDelegatorWithName(ServiceLocatorInterface
$serviceLocator, $name, $requestedName, $callback)
55 $initializer = function (& $wrappedInstance, LazyLoadingInterface
$proxy) use ($callback) {
56 $proxy->setProxyInitializer(null);
58 $wrappedInstance = call_user_func($callback);
63 if (isset($this->servicesMap
[$requestedName])) {
64 return $this->proxyFactory
->createProxy($this->servicesMap
[$requestedName], $initializer);
65 } elseif (isset($this->servicesMap
[$name])) {
66 return $this->proxyFactory
->createProxy($this->servicesMap
[$name], $initializer);
69 throw new Exception\
InvalidServiceNameException(
70 sprintf('The requested service "%s" was not found in the provided services map', $requestedName)