Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Diagnostics
      • Extensions
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
      • Diagnostics
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • PhpGenerator
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
  • NetteModule
  • none

Classes

  • Compiler
  • CompilerExtension
  • Container
  • ContainerBuilder
  • ServiceDefinition
  • Statement

Exceptions

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