1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\DI;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17: 18: 19:
20: class ServiceDefinition
21: {
22: use Nette\SmartObject;
23:
24: const
25: IMPLEMENT_MODE_CREATE = 'create',
26: IMPLEMENT_MODE_GET = 'get';
27:
28:
29: public $parameters = [];
30:
31:
32: private $type;
33:
34:
35: private $factory;
36:
37:
38: private $setup = [];
39:
40:
41: private $tags = [];
42:
43:
44: private $autowired = true;
45:
46:
47: private $dynamic = false;
48:
49:
50: private $implement;
51:
52:
53: private $implementMode;
54:
55:
56: private $notifier = 'pi';
57:
58:
59: 60: 61: 62: 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: 77: 78:
79: public function getClass()
80: {
81: return $this->type;
82: }
83:
84:
85: 86: 87: 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: 99:
100: public function getType()
101: {
102: return $this->type;
103: }
104:
105:
106: 107: 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: 119:
120: public function getFactory()
121: {
122: return $this->factory;
123: }
124:
125:
126: 127: 128:
129: public function getEntity()
130: {
131: return $this->factory ? $this->factory->getEntity() : null;
132: }
133:
134:
135: 136: 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: 150: 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: 166:
167: public function getSetup()
168: {
169: return $this->setup;
170: }
171:
172:
173: 174: 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: 185:
186: public function setParameters(array $params)
187: {
188: $this->parameters = $params;
189: return $this;
190: }
191:
192:
193: 194: 195:
196: public function getParameters()
197: {
198: return $this->parameters;
199: }
200:
201:
202: 203: 204:
205: public function setTags(array $tags)
206: {
207: $this->tags = $tags;
208: return $this;
209: }
210:
211:
212: 213: 214:
215: public function getTags()
216: {
217: return $this->tags;
218: }
219:
220:
221: 222: 223:
224: public function addTag($tag, $attr = true)
225: {
226: $this->tags[$tag] = $attr;
227: return $this;
228: }
229:
230:
231: 232: 233:
234: public function getTag($tag)
235: {
236: return isset($this->tags[$tag]) ? $this->tags[$tag] : null;
237: }
238:
239:
240: 241: 242: 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: 254:
255: public function isAutowired()
256: {
257: return $this->autowired;
258: }
259:
260:
261: 262: 263:
264: public function getAutowired()
265: {
266: return $this->autowired;
267: }
268:
269:
270: 271: 272: 273:
274: public function setDynamic($state = true)
275: {
276: $this->dynamic = (bool) $state;
277: return $this;
278: }
279:
280:
281: 282: 283:
284: public function isDynamic()
285: {
286: return $this->dynamic;
287: }
288:
289:
290: 291: 292: 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: 304:
305: public function getImplement()
306: {
307: return $this->implement;
308: }
309:
310:
311: 312: 313: 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: 327:
328: public function getImplementMode()
329: {
330: return $this->implementMode;
331: }
332:
333:
334:
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:
343: public function getImplementType()
344: {
345: trigger_error(__METHOD__ . '() is deprecated, use getImplementMode()', E_USER_DEPRECATED);
346: return $this->implementMode;
347: }
348:
349:
350:
351: public function setInject($state = true)
352: {
353:
354: return $this->addTag(Extensions\InjectExtension::TAG_INJECT, $state);
355: }
356:
357:
358:
359: public function getInject()
360: {
361:
362: return $this->getTag(Extensions\InjectExtension::TAG_INJECT);
363: }
364:
365:
366: 367: 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: