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