/usr/share/php/Nette/DI/Container.php is in php-nette 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | <?php
/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/
namespace Nette\DI;
use Nette;
/**
* The dependency injection container default implementation.
*
* @author David Grudl
*/
class Container extends Nette\Object
{
const TAGS = 'tags';
const TYPES = 'types';
/** @var array user parameters */
/*private*/public $parameters = array();
/** @var array storage for shared objects */
private $registry = array();
/** @var array[] */
protected $meta = array();
/** @var array circular reference detector */
private $creating;
public function __construct(array $params = array())
{
$this->parameters = $params + $this->parameters;
}
/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}
/**
* Adds the service to the container.
* @param string
* @param object
* @return self
*/
public function addService($name, $service)
{
if (func_num_args() > 2) {
throw new Nette\DeprecatedException('Parameter $meta has been removed.');
} elseif (!is_string($name) || !$name) {
throw new Nette\InvalidArgumentException('Service name must be a non-empty string, ' . gettype($name) . ' given.');
} elseif (isset($this->registry[$name])) {
throw new Nette\InvalidStateException("Service '$name' already exists.");
} elseif (is_string($service) || is_array($service) || $service instanceof \Closure || $service instanceof Nette\Callback) {
trigger_error('Passing factories to ' . __METHOD__ . '() is deprecated; pass the object itself.', E_USER_DEPRECATED);
$service = is_string($service) && !preg_match('#\x00|:#', $service) ? new $service : call_user_func($service, $this);
}
if (!is_object($service)) {
throw new Nette\InvalidArgumentException('Service must be a object, ' . gettype($service) . ' given.');
}
$this->registry[$name] = $service;
return $this;
}
/**
* Removes the service from the container.
* @param string
* @return void
*/
public function removeService($name)
{
unset($this->registry[$name]);
}
/**
* Gets the service object by name.
* @param string
* @return object
* @throws MissingServiceException
*/
public function getService($name)
{
if (!isset($this->registry[$name])) {
$this->registry[$name] = $this->createService($name);
}
return $this->registry[$name];
}
/**
* Does the service exist?
* @param string service name
* @return bool
*/
public function hasService($name)
{
return isset($this->registry[$name])
|| method_exists($this, $method = Container::getMethodName($name)) && $this->getReflection()->getMethod($method)->getName() === $method;
}
/**
* Is the service created?
* @param string service name
* @return bool
*/
public function isCreated($name)
{
if (!$this->hasService($name)) {
throw new MissingServiceException("Service '$name' not found.");
}
return isset($this->registry[$name]);
}
/**
* Creates new instance of the service.
* @param string service name
* @return object
* @throws MissingServiceException
*/
public function createService($name, array $args = array())
{
$method = Container::getMethodName($name);
if (isset($this->creating[$name])) {
throw new Nette\InvalidStateException("Circular reference detected for services: "
. implode(', ', array_keys($this->creating)) . ".");
} elseif (!method_exists($this, $method) || $this->getReflection()->getMethod($method)->getName() !== $method) {
throw new MissingServiceException("Service '$name' not found.");
}
$this->creating[$name] = TRUE;
try {
$service = call_user_func_array(array($this, $method), $args);
} catch (\Exception $e) {
unset($this->creating[$name]);
throw $e;
}
unset($this->creating[$name]);
if (!is_object($service)) {
throw new Nette\UnexpectedValueException("Unable to create service '$name', value returned by method $method() is not object.");
}
return $service;
}
/**
* Resolves service by type.
* @param string class or interface
* @param bool throw exception if service doesn't exist?
* @return object service or NULL
* @throws MissingServiceException
*/
public function getByType($class, $need = TRUE)
{
$names = $this->findByType($class);
if (!$names) {
if ($need) {
throw new MissingServiceException("Service of type $class not found.");
}
} elseif (count($names) > 1) {
throw new MissingServiceException("Multiple services of type $class found: " . implode(', ', $names) . '.');
} else {
return $this->getService($names[0]);
}
}
/**
* Gets the service names of the specified type.
* @param string
* @return string[]
*/
public function findByType($class)
{
$class = ltrim(strtolower($class), '\\');
return isset($this->meta[self::TYPES][$class]) ? $this->meta[self::TYPES][$class] : array();
}
/**
* Gets the service names of the specified tag.
* @param string
* @return array of [service name => tag attributes]
*/
public function findByTag($tag)
{
return isset($this->meta[self::TAGS][$tag]) ? $this->meta[self::TAGS][$tag] : array();
}
/********************* autowiring ****************d*g**/
/**
* Creates new instance using autowiring.
* @param string class
* @param array arguments
* @return object
* @throws Nette\InvalidArgumentException
*/
public function createInstance($class, array $args = array())
{
$rc = Nette\Reflection\ClassType::from($class);
if (!$rc->isInstantiable()) {
throw new ServiceCreationException("Class $class is not instantiable.");
} elseif ($constructor = $rc->getConstructor()) {
return $rc->newInstanceArgs(Helpers::autowireArguments($constructor, $args, $this));
} elseif ($args) {
throw new ServiceCreationException("Unable to pass arguments, class $class has no constructor.");
}
return new $class;
}
/**
* Calls all methods starting with with "inject" using autowiring.
* @param object
* @return void
*/
public function callInjects($service)
{
if (!is_object($service)) {
throw new Nette\InvalidArgumentException('Service must be object, ' . gettype($service) . ' given.');
}
foreach (array_reverse(get_class_methods($service)) as $method) {
if (substr($method, 0, 6) === 'inject') {
$this->callMethod(array($service, $method));
}
}
foreach (Helpers::getInjectProperties(Nette\Reflection\ClassType::from($service)) as $property => $type) {
$service->$property = $this->getByType($type);
}
}
/**
* Calls method using autowiring.
* @param mixed class, object, function, callable
* @param array arguments
* @return mixed
*/
public function callMethod($function, array $args = array())
{
return call_user_func_array(
$function,
Helpers::autowireArguments(Nette\Utils\Callback::toReflection($function), $args, $this)
);
}
/********************* shortcuts ****************d*g**/
/**
* Expands %placeholders%.
* @param mixed
* @return mixed
*/
public function expand($s)
{
return Helpers::expand($s, $this->parameters);
}
/** @deprecated */
public function &__get($name)
{
$this->error(__METHOD__, 'getService');
$tmp = $this->getService($name);
return $tmp;
}
/** @deprecated */
public function __set($name, $service)
{
$this->error(__METHOD__, 'addService');
$this->addService($name, $service);
}
/** @deprecated */
public function __isset($name)
{
$this->error(__METHOD__, 'hasService');
return $this->hasService($name);
}
/** @deprecated */
public function __unset($name)
{
$this->error(__METHOD__, 'removeService');
$this->removeService($name);
}
private function error($oldName, $newName)
{
if (empty($this->parameters['container']['accessors'])) {
trigger_error("$oldName() is deprecated; use $newName() or enable nette.container.accessors in configuration.", E_USER_DEPRECATED);
}
}
public static function getMethodName($name)
{
$uname = ucfirst($name);
return 'createService' . ((string) $name === $uname ? '__' : '') . str_replace('.', '__', $uname);
}
}
|