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 https://nette.org
13: * @copyright Copyright (c) 2004, 2009 David Grudl
14: * @license https://nette.org/license Nette license
15: * @link https://nette.org
23: require_once dirname(__FILE__) .
'/IServiceLocator.php';
25: require_once dirname(__FILE__) .
'/Object.php';
30: * Service locator pattern implementation.
32: * @author David Grudl
33: * @copyright Copyright (c) 2004, 2009 David Grudl
38: /** @var IServiceLocator */
41: /** @var array storage for shared objects */
42: private $registry =
array();
44: /** @var array storage for shared objects */
45: private $factories =
array();
50: * @param IServiceLocator
54: $this->parent =
$parent;
60: * Adds the specified service to the service container.
61: * @param mixed object, class name or service factory callback
62: * @param string optional service name (for factories is not optional)
63: * @param bool promote to higher level?
65: * @throws InvalidArgumentException, AmbiguousServiceException
67: public function addService($service, $name =
NULL, $promote =
FALSE)
73: if ($name ===
NULL) $name =
$service;
77: throw new InvalidArgumentException('When factory callback is given, service name must be specified.');
81: throw new InvalidArgumentException('Service must be name, object or factory callback.');
85: if (isset($this->registry[$lower])) {
90: $this->registry[$lower] =
$service;
93: $this->factories[$lower] =
$service;
96: if ($promote &&
$this->parent !==
NULL) {
104: * Removes the specified service type from the service container.
105: * @param bool promote to higher level?
111: throw new InvalidArgumentException("Service name must be a non-empty string, " .
gettype($name) .
" given.");
115: unset($this->registry[$lower], $this->factories[$lower]);
117: if ($promote &&
$this->parent !==
NULL) {
125: * Gets the service object of the specified type.
126: * @param string service name
127: * @param bool throw exception if service doesn't exist?
133: throw new InvalidArgumentException("Service name must be a non-empty string, " .
gettype($name) .
" given.");
138: if (isset($this->registry[$lower])) {
139: return $this->registry[$lower];
141: } elseif (isset($this->factories[$lower])) {
142: $service =
$this->factories[$lower];
146: // trick to pass callback as string
155: return $this->registry[$lower] =
new $service;
162: throw new AmbiguousServiceException("Cannot instantiate service '$name', handler '$textual' is not " .
($able ?
'callable.' :
'valid PHP callback.'));
168: if ($this->parent !==
NULL) {
182: * Returns the parent container if any.
183: * @return IServiceLocator|NULL
187: return $this->parent;
195: * Ambiguous service resolution exception.
197: * @author David Grudl
198: * @copyright Copyright (c) 2004, 2009 David Grudl