Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
      • Traits
    • Reflection
    • Security
    • Tokenizer
    • Utils
  • Tracy
    • Bridges
      • Nette
  • none

Classes

  • Compiler
  • CompilerExtension
  • Container
  • ContainerBuilder
  • ContainerLoader
  • DependencyChecker
  • Helpers
  • PhpGenerator
  • PhpReflection
  • ServiceDefinition
  • Statement

Exceptions

  • MissingServiceException
  • ServiceCreationException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  • Other releases
  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:  * Definition used by ContainerBuilder.
 15:  *
 16:  * @property string|null $class
 17:  * @property Statement|null $factory
 18:  * @property Statement[] $setup
 19:  */
 20: class ServiceDefinition
 21: {
 22:     use Nette\SmartObject;
 23: 
 24:     const
 25:         IMPLEMENT_MODE_CREATE = 'create',
 26:         IMPLEMENT_MODE_GET = 'get';
 27: 
 28:     /** @var array */
 29:     public $parameters = [];
 30: 
 31:     /** @var string|null  class or interface name */
 32:     private $type;
 33: 
 34:     /** @var Statement|null */
 35:     private $factory;
 36: 
 37:     /** @var Statement[] */
 38:     private $setup = [];
 39: 
 40:     /** @var array */
 41:     private $tags = [];
 42: 
 43:     /** @var bool|string[] */
 44:     private $autowired = true;
 45: 
 46:     /** @var bool */
 47:     private $dynamic = false;
 48: 
 49:     /** @var string|null  interface name */
 50:     private $implement;
 51: 
 52:     /** @var string|null  create | get */
 53:     private $implementMode;
 54: 
 55:     /** @var callable  'pi' is noop */
 56:     private $notifier = 'pi';
 57: 
 58: 
 59:     /**
 60:      * @param  string|null
 61:      * @return static
 62:      * @deprecated Use setType() instead.
 63:      */
 64:     public function setClass($type, array $args = [])
 65:     {
 66:         call_user_func($this->notifier);
 67:         $this->type = $type;
 68:         if ($args) {
 69:             $this->setFactory($type, $args);
 70:         }
 71:         return $this;
 72:     }
 73: 
 74: 
 75:     /**
 76:      * @return string|null
 77:      * @deprecated Use getType() instead.
 78:      */
 79:     public function getClass()
 80:     {
 81:         return $this->type;
 82:     }
 83: 
 84: 
 85:     /**
 86:      * @param  string|null
 87:      * @return static
 88:      */
 89:     public function setType($type)
 90:     {
 91:         call_user_func($this->notifier);
 92:         $this->type = $type;
 93:         return $this;
 94:     }
 95: 
 96: 
 97:     /**
 98:      * @return string|null
 99:      */
100:     public function getType()
101:     {
102:         return $this->type;
103:     }
104: 
105: 
106:     /**
107:      * @return static
108:      */
109:     public function setFactory($factory, array $args = [])
110:     {
111:         call_user_func($this->notifier);
112:         $this->factory = $factory instanceof Statement ? $factory : new Statement($factory, $args);
113:         return $this;
114:     }
115: 
116: 
117:     /**
118:      * @return Statement|null
119:      */
120:     public function getFactory()
121:     {
122:         return $this->factory;
123:     }
124: 
125: 
126:     /**
127:      * @return string|array|ServiceDefinition|null
128:      */
129:     public function getEntity()
130:     {
131:         return $this->factory ? $this->factory->getEntity() : null;
132:     }
133: 
134: 
135:     /**
136:      * @return static
137:      */
138:     public function setArguments(array $args = [])
139:     {
140:         if (!$this->factory) {
141:             $this->factory = new Statement($this->type);
142:         }
143:         $this->factory->arguments = $args;
144:         return $this;
145:     }
146: 
147: 
148:     /**
149:      * @param  Statement[]
150:      * @return static
151:      */
152:     public function setSetup(array $setup)
153:     {
154:         foreach ($setup as $v) {
155:             if (!$v instanceof Statement) {
156:                 throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Statement[].');
157:             }
158:         }
159:         $this->setup = $setup;
160:         return $this;
161:     }
162: 
163: 
164:     /**
165:      * @return Statement[]
166:      */
167:     public function getSetup()
168:     {
169:         return $this->setup;
170:     }
171: 
172: 
173:     /**
174:      * @return static
175:      */
176:     public function addSetup($entity, array $args = [])
177:     {
178:         $this->setup[] = $entity instanceof Statement ? $entity : new Statement($entity, $args);
179:         return $this;
180:     }
181: 
182: 
183:     /**
184:      * @return static
185:      */
186:     public function setParameters(array $params)
187:     {
188:         $this->parameters = $params;
189:         return $this;
190:     }
191: 
192: 
193:     /**
194:      * @return array
195:      */
196:     public function getParameters()
197:     {
198:         return $this->parameters;
199:     }
200: 
201: 
202:     /**
203:      * @return static
204:      */
205:     public function setTags(array $tags)
206:     {
207:         $this->tags = $tags;
208:         return $this;
209:     }
210: 
211: 
212:     /**
213:      * @return array
214:      */
215:     public function getTags()
216:     {
217:         return $this->tags;
218:     }
219: 
220: 
221:     /**
222:      * @return static
223:      */
224:     public function addTag($tag, $attr = true)
225:     {
226:         $this->tags[$tag] = $attr;
227:         return $this;
228:     }
229: 
230: 
231:     /**
232:      * @return mixed
233:      */
234:     public function getTag($tag)
235:     {
236:         return isset($this->tags[$tag]) ? $this->tags[$tag] : null;
237:     }
238: 
239: 
240:     /**
241:      * @param  bool|string|string[]
242:      * @return static
243:      */
244:     public function setAutowired($state = true)
245:     {
246:         call_user_func($this->notifier);
247:         $this->autowired = is_string($state) || is_array($state) ? (array) $state : (bool) $state;
248:         return $this;
249:     }
250: 
251: 
252:     /**
253:      * @return bool|string[]
254:      */
255:     public function isAutowired()
256:     {
257:         return $this->autowired;
258:     }
259: 
260: 
261:     /**
262:      * @return bool|string[]
263:      */
264:     public function getAutowired()
265:     {
266:         return $this->autowired;
267:     }
268: 
269: 
270:     /**
271:      * @param  bool
272:      * @return static
273:      */
274:     public function setDynamic($state = true)
275:     {
276:         $this->dynamic = (bool) $state;
277:         return $this;
278:     }
279: 
280: 
281:     /**
282:      * @return bool
283:      */
284:     public function isDynamic()
285:     {
286:         return $this->dynamic;
287:     }
288: 
289: 
290:     /**
291:      * @param  string
292:      * @return static
293:      */
294:     public function setImplement($interface)
295:     {
296:         call_user_func($this->notifier);
297:         $this->implement = $interface;
298:         return $this;
299:     }
300: 
301: 
302:     /**
303:      * @return string|null
304:      */
305:     public function getImplement()
306:     {
307:         return $this->implement;
308:     }
309: 
310: 
311:     /**
312:      * @param  string
313:      * @return static
314:      */
315:     public function setImplementMode($mode)
316:     {
317:         if (!in_array($mode, [self::IMPLEMENT_MODE_CREATE, self::IMPLEMENT_MODE_GET], true)) {
318:             throw new Nette\InvalidArgumentException('Argument must be get|create.');
319:         }
320:         $this->implementMode = $mode;
321:         return $this;
322:     }
323: 
324: 
325:     /**
326:      * @return string|null
327:      */
328:     public function getImplementMode()
329:     {
330:         return $this->implementMode;
331:     }
332: 
333: 
334:     /** @deprecated */
335:     public function setImplementType($type)
336:     {
337:         trigger_error(__METHOD__ . '() is deprecated, use setImplementMode()', E_USER_DEPRECATED);
338:         return $this->setImplementMode($type);
339:     }
340: 
341: 
342:     /** @deprecated */
343:     public function getImplementType()
344:     {
345:         trigger_error(__METHOD__ . '() is deprecated, use getImplementMode()', E_USER_DEPRECATED);
346:         return $this->implementMode;
347:     }
348: 
349: 
350:     /** @return static */
351:     public function setInject($state = true)
352:     {
353:         //trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
354:         return $this->addTag(Extensions\InjectExtension::TAG_INJECT, $state);
355:     }
356: 
357: 
358:     /** @return bool|null */
359:     public function getInject()
360:     {
361:         //trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
362:         return $this->getTag(Extensions\InjectExtension::TAG_INJECT);
363:     }
364: 
365: 
366:     /**
367:      * @internal
368:      */
369:     public function setNotifier(callable $notifier)
370:     {
371:         $this->notifier = $notifier;
372:     }
373: 
374: 
375:     public function __clone()
376:     {
377:         $this->factory = unserialize(serialize($this->factory));
378:         $this->setup = unserialize(serialize($this->setup));
379:         $this->notifier = 'pi';
380:     }
381: }
382: 
Nette 2.4-20180918 API API documentation generated by ApiGen 2.8.0