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