1: <?php
2:
3: 4: 5: 6:
7:
8: namespace Nette\DI;
9:
10: use Nette;
11:
12:
13: 14: 15: 16: 17:
18: class ServiceDefinition extends Nette\Object
19: {
20:
21: private $class;
22:
23:
24: private $factory;
25:
26:
27: private $setup = array();
28:
29:
30: public $parameters = array();
31:
32:
33: private $tags = array();
34:
35:
36: private $autowired = TRUE;
37:
38:
39: private $dynamic = FALSE;
40:
41:
42: private $implement;
43:
44:
45: private $implementType;
46:
47:
48: 49: 50:
51: public function setClass($class, array $args = array())
52: {
53: $this->class = ltrim($class, '\\');
54: if ($args) {
55: $this->setFactory($class, $args);
56: }
57: return $this;
58: }
59:
60:
61: 62: 63:
64: public function getClass()
65: {
66: return $this->class;
67: }
68:
69:
70: 71: 72:
73: public function setFactory($factory, array $args = array())
74: {
75: $this->factory = $factory instanceof Statement ? $factory : new Statement($factory, $args);
76: return $this;
77: }
78:
79:
80: 81: 82:
83: public function getFactory()
84: {
85: return $this->factory;
86: }
87:
88:
89: public function getEntity()
90: {
91: return $this->factory ? $this->factory->getEntity() : NULL;
92: }
93:
94:
95: 96: 97:
98: public function setArguments(array $args = array())
99: {
100: if (!$this->factory) {
101: $this->factory = new Statement($this->class);
102: }
103: $this->factory->arguments = $args;
104: return $this;
105: }
106:
107:
108: 109: 110: 111:
112: public function setSetup(array $setup)
113: {
114: foreach ($setup as $v) {
115: if (!$v instanceof Statement) {
116: throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Statement[].');
117: }
118: }
119: $this->setup = $setup;
120: return $this;
121: }
122:
123:
124: 125: 126:
127: public function getSetup()
128: {
129: return $this->setup;
130: }
131:
132:
133: 134: 135:
136: public function addSetup($entity, array $args = array())
137: {
138: $this->setup[] = $entity instanceof Statement ? $entity : new Statement($entity, $args);
139: return $this;
140: }
141:
142:
143: 144: 145:
146: public function setParameters(array $params)
147: {
148: $this->parameters = $params;
149: return $this;
150: }
151:
152:
153: 154: 155:
156: public function getParameters()
157: {
158: return $this->parameters;
159: }
160:
161:
162: 163: 164:
165: public function setTags(array $tags)
166: {
167: $this->tags = $tags;
168: return $this;
169: }
170:
171:
172: 173: 174:
175: public function getTags()
176: {
177: return $this->tags;
178: }
179:
180:
181: 182: 183:
184: public function addTag($tag, $attr = TRUE)
185: {
186: $this->tags[$tag] = $attr;
187: return $this;
188: }
189:
190:
191: 192: 193:
194: public function getTag($tag)
195: {
196: return isset($this->tags[$tag]) ? $this->tags[$tag] : NULL;
197: }
198:
199:
200: 201: 202: 203:
204: public function setAutowired($state = TRUE)
205: {
206: $this->autowired = (bool) $state;
207: return $this;
208: }
209:
210:
211: 212: 213:
214: public function isAutowired()
215: {
216: return $this->autowired;
217: }
218:
219:
220: 221: 222: 223:
224: public function setDynamic($state = TRUE)
225: {
226: $this->dynamic = (bool) $state;
227: return $this;
228: }
229:
230:
231: 232: 233:
234: public function isDynamic()
235: {
236: return $this->dynamic;
237: }
238:
239:
240: 241: 242: 243:
244: public function setImplement($interface)
245: {
246: $this->implement = ltrim($interface, '\\');
247: return $this;
248: }
249:
250:
251: 252: 253:
254: public function getImplement()
255: {
256: return $this->implement;
257: }
258:
259:
260: 261: 262: 263:
264: public function setImplementType($type)
265: {
266: if (!in_array($type, array('get', 'create'), TRUE)) {
267: throw new Nette\InvalidArgumentException('Argument must be get|create.');
268: }
269: $this->implementType = $type;
270: return $this;
271: }
272:
273:
274: 275: 276:
277: public function getImplementType()
278: {
279: return $this->implementType;
280: }
281:
282:
283:
284: public function setShared($on)
285: {
286: trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
287: $this->autowired = $on ? $this->autowired : FALSE;
288: return $this;
289: }
290:
291:
292:
293: public function isShared()
294: {
295: trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
296: }
297:
298:
299:
300: public function setInject($state = TRUE)
301: {
302:
303: return $this->addTag(Extensions\InjectExtension::TAG_INJECT, $state);
304: }
305:
306:
307:
308: public function getInject()
309: {
310:
311: return $this->getTag(Extensions\InjectExtension::TAG_INJECT);
312: }
313:
314: }
315: