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: 88: 89:
90: public function getEntity()
91: {
92: return $this->factory ? $this->factory->getEntity() : NULL;
93: }
94:
95:
96: 97: 98:
99: public function setArguments(array $args = array())
100: {
101: if (!$this->factory) {
102: $this->factory = new Statement($this->class);
103: }
104: $this->factory->arguments = $args;
105: return $this;
106: }
107:
108:
109: 110: 111: 112:
113: public function setSetup(array $setup)
114: {
115: foreach ($setup as $v) {
116: if (!$v instanceof Statement) {
117: throw new Nette\InvalidArgumentException('Argument must be Nette\DI\Statement[].');
118: }
119: }
120: $this->setup = $setup;
121: return $this;
122: }
123:
124:
125: 126: 127:
128: public function getSetup()
129: {
130: return $this->setup;
131: }
132:
133:
134: 135: 136:
137: public function addSetup($entity, array $args = array())
138: {
139: $this->setup[] = $entity instanceof Statement ? $entity : new Statement($entity, $args);
140: return $this;
141: }
142:
143:
144: 145: 146:
147: public function setParameters(array $params)
148: {
149: $this->parameters = $params;
150: return $this;
151: }
152:
153:
154: 155: 156:
157: public function getParameters()
158: {
159: return $this->parameters;
160: }
161:
162:
163: 164: 165:
166: public function setTags(array $tags)
167: {
168: $this->tags = $tags;
169: return $this;
170: }
171:
172:
173: 174: 175:
176: public function getTags()
177: {
178: return $this->tags;
179: }
180:
181:
182: 183: 184:
185: public function addTag($tag, $attr = TRUE)
186: {
187: $this->tags[$tag] = $attr;
188: return $this;
189: }
190:
191:
192: 193: 194:
195: public function getTag($tag)
196: {
197: return isset($this->tags[$tag]) ? $this->tags[$tag] : NULL;
198: }
199:
200:
201: 202: 203: 204:
205: public function setAutowired($state = TRUE)
206: {
207: $this->autowired = (bool) $state;
208: return $this;
209: }
210:
211:
212: 213: 214:
215: public function isAutowired()
216: {
217: return $this->autowired;
218: }
219:
220:
221: 222: 223: 224:
225: public function setDynamic($state = TRUE)
226: {
227: $this->dynamic = (bool) $state;
228: return $this;
229: }
230:
231:
232: 233: 234:
235: public function isDynamic()
236: {
237: return $this->dynamic;
238: }
239:
240:
241: 242: 243: 244:
245: public function setImplement($interface)
246: {
247: $this->implement = ltrim($interface, '\\');
248: return $this;
249: }
250:
251:
252: 253: 254:
255: public function getImplement()
256: {
257: return $this->implement;
258: }
259:
260:
261: 262: 263: 264:
265: public function setImplementType($type)
266: {
267: if (!in_array($type, array('get', 'create'), TRUE)) {
268: throw new Nette\InvalidArgumentException('Argument must be get|create.');
269: }
270: $this->implementType = $type;
271: return $this;
272: }
273:
274:
275: 276: 277:
278: public function getImplementType()
279: {
280: return $this->implementType;
281: }
282:
283:
284:
285: public function setShared($on)
286: {
287: trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
288: $this->autowired = $on ? $this->autowired : FALSE;
289: return $this;
290: }
291:
292:
293:
294: public function isShared()
295: {
296: trigger_error(__METHOD__ . '() is deprecated.', E_USER_DEPRECATED);
297: }
298:
299:
300:
301: public function setInject($state = TRUE)
302: {
303:
304: return $this->addTag(Extensions\InjectExtension::TAG_INJECT, $state);
305: }
306:
307:
308:
309: public function getInject()
310: {
311:
312: return $this->getTag(Extensions\InjectExtension::TAG_INJECT);
313: }
314:
315: }
316: