/usr/share/php/Nette/DI/Container.php is in php-nette 2.4-20160731-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 339 340 341 | <?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
namespace Nette\DI;
use Nette;
/**
* The dependency injection container default implementation.
*/
class Container
{
use Nette\SmartObject;
const TAGS = 'tags';
const TYPES = 'types';
const SERVICES = 'services';
const ALIASES = 'aliases';
/** @var array user parameters */
/*private*/public $parameters = [];
/** @var object[] storage for shared objects */
private $registry = [];
/** @var array[] */
protected $meta = [];
/** @var array circular reference detector */
private $creating;
public function __construct(array $params = [])
{
$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 (!is_string($name) || !$name) {
throw new Nette\InvalidArgumentException(sprintf('Service name must be a non-empty string, %s given.', gettype($name)));
}
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
if (isset($this->registry[$name])) {
throw new Nette\InvalidStateException("Service '$name' already exists.");
} elseif (!is_object($service)) {
throw new Nette\InvalidArgumentException(sprintf("Service '%s' must be a object, %s given.", $name, gettype($service)));
} elseif (isset($this->meta[self::SERVICES][$name]) && !$service instanceof $this->meta[self::SERVICES][$name]) {
throw new Nette\InvalidArgumentException(sprintf("Service '%s' must be instance of %s, %s given.", $name, $this->meta[self::SERVICES][$name], get_class($service)));
}
$this->registry[$name] = $service;
return $this;
}
/**
* Removes the service from the container.
* @param string
* @return void
*/
public function removeService($name)
{
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $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])) {
if (isset($this->meta[self::ALIASES][$name])) {
return $this->getService($this->meta[self::ALIASES][$name]);
}
$this->registry[$name] = $this->createService($name);
}
return $this->registry[$name];
}
/**
* Gets the service type by name.
* @param string
* @return string
* @throws MissingServiceException
*/
public function getServiceType($name)
{
if (isset($this->meta[self::ALIASES][$name])) {
return $this->getServiceType($this->meta[self::ALIASES][$name]);
} elseif (isset($this->meta[self::SERVICES][$name])) {
return $this->meta[self::SERVICES][$name];
} else {
throw new MissingServiceException("Service '$name' not found.");
}
}
/**
* Does the service exist?
* @param string service name
* @return bool
*/
public function hasService($name)
{
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
return isset($this->registry[$name])
|| (method_exists($this, $method = self::getMethodName($name))
&& (new \ReflectionMethod($this, $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.");
}
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
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 = [])
{
$name = isset($this->meta[self::ALIASES][$name]) ? $this->meta[self::ALIASES][$name] : $name;
$method = self::getMethodName($name);
if (isset($this->creating[$name])) {
throw new Nette\InvalidStateException(sprintf('Circular reference detected for services: %s.', implode(', ', array_keys($this->creating))));
} elseif (!method_exists($this, $method) || (new \ReflectionMethod($this, $method))->getName() !== $method) {
throw new MissingServiceException("Service '$name' not found.");
}
try {
$this->creating[$name] = TRUE;
$service = $this->$method(...$args);
} finally {
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)
{
$class = ltrim($class, '\\');
if (!empty($this->meta[self::TYPES][$class][TRUE])) {
if (count($names = $this->meta[self::TYPES][$class][TRUE]) === 1) {
return $this->getService($names[0]);
}
throw new MissingServiceException("Multiple services of type $class found: " . implode(', ', $names) . '.');
} elseif ($need) {
throw new MissingServiceException("Service of type $class not found.");
}
}
/**
* Gets the service names of the specified type.
* @param string
* @return string[]
*/
public function findByType($class)
{
$class = ltrim($class, '\\');
return empty($this->meta[self::TYPES][$class])
? []
: array_merge(...array_values($this->meta[self::TYPES][$class]));
}
/**
* 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] : [];
}
/********************* 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 = [])
{
$rc = new \ReflectionClass($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)
{
Extensions\InjectExtension::callInjects($this, $service);
}
/**
* Calls method using autowiring.
* @return mixed
*/
public function callMethod(callable $function, array $args = [])
{
return $function(...Helpers::autowireArguments(Nette\Utils\Callback::toReflection($function), $args, $this));
}
/********************* shortcuts ****************d*g**/
/**
* Expands %placeholders%.
* @param mixed
* @return mixed
* @deprecated
*/
public function expand($s)
{
return Helpers::expand($s, $this->parameters);
}
/** @deprecated */
public function &__get($name)
{
trigger_error(__METHOD__ . ' is deprecated; use getService().', E_USER_DEPRECATED);
$tmp = $this->getService($name);
return $tmp;
}
/** @deprecated */
public function __set($name, $service)
{
trigger_error(__METHOD__ . ' is deprecated; use addService().', E_USER_DEPRECATED);
$this->addService($name, $service);
}
/** @deprecated */
public function __isset($name)
{
trigger_error(__METHOD__ . ' is deprecated; use hasService().', E_USER_DEPRECATED);
return $this->hasService($name);
}
/** @deprecated */
public function __unset($name)
{
trigger_error(__METHOD__ . ' is deprecated; use removeService().', E_USER_DEPRECATED);
$this->removeService($name);
}
public static function getMethodName($name)
{
$uname = ucfirst($name);
return 'createService' . ((string) $name === $uname ? '__' : '') . str_replace('.', '__', $uname);
}
}
|