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