Source for file ServiceLocator.php
Documentation is available at ServiceLocator.php
6: * Copyright (c) 2004, 2009 David Grudl (http://davidgrudl.com)
8: * This source file is subject to the "Nette license" that is bundled
9: * with this package in the file license.txt.
11: * For more information please see http://nettephp.com
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license http://nettephp.com/license Nette license
15: * @link http://nettephp.com
22: require_once dirname(__FILE__) .
'/IServiceLocator.php';
24: require_once dirname(__FILE__) .
'/Object.php';
29: * Service locator pattern implementation.
31: * @author David Grudl
32: * @copyright Copyright (c) 2004, 2009 David Grudl
37: /** @var IServiceLocator */
40: /** @var array storage for shared objects */
41: private $registry =
array();
43: /** @var array storage for shared objects */
44: private $factories =
array();
49: * @param IServiceLocator
53: $this->parent =
$parent;
59: * Adds the specified service to the service container.
60: * @param mixed object, class name or service factory callback
61: * @param string optional service name (for factories is not optional)
62: * @param bool promote to higher level?
64: * @throws InvalidArgumentException, AmbiguousServiceException
66: public function addService($service, $name =
NULL, $promote =
FALSE)
72: if ($name ===
NULL) $name =
$service;
76: throw new InvalidArgumentException('When factory callback is given, service name must be specified.');
80: throw new InvalidArgumentException('Service must be name, object or factory callback.');
84: if (isset($this->registry[$lower])) {
89: $this->registry[$lower] =
$service;
92: $this->factories[$lower] =
$service;
95: if ($promote &&
$this->parent !==
NULL) {
103: * Removes the specified service type from the service container.
104: * @param bool promote to higher level?
110: throw new InvalidArgumentException("Service name must be a non-empty string, " .
gettype($name) .
" given.");
114: unset($this->registry[$lower], $this->factories[$lower]);
116: if ($promote &&
$this->parent !==
NULL) {
124: * Gets the service object of the specified type.
125: * @param string service name
126: * @param bool throw exception if service doesn't exist?
132: throw new InvalidArgumentException("Service name must be a non-empty string, " .
gettype($name) .
" given.");
137: if (isset($this->registry[$lower])) {
138: return $this->registry[$lower];
140: } elseif (isset($this->factories[$lower])) {
141: $service =
$this->factories[$lower];
145: // trick to pass callback as string
154: return $this->registry[$lower] =
new $service;
161: throw new AmbiguousServiceException("Cannot instantiate service '$name', handler '$textual' is not " .
($able ?
'callable.' :
'valid PHP callback.'));
167: if ($this->parent !==
NULL) {
181: * Exists the service?
182: * @param string service name
183: * @param bool must be created yet?
189: throw new InvalidArgumentException("Service name must be a non-empty string, " .
gettype($name) .
" given.");
193: return isset($this->registry[$lower]) ||
(!$created &&
isset($this->factories[$lower])) ||
($this->parent !==
NULL &&
$this->parent->hasService($name, $created));
199: * Returns the parent container if any.
200: * @return IServiceLocator|NULL
204: return $this->parent;
212: * Ambiguous service resolution exception.
214: * @author David Grudl
215: * @copyright Copyright (c) 2004, 2009 David Grudl