Completed
Push — master ( 351c32...271583 )
by Taosikai
19:55 queued 04:54
created

Definition.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the slince/di package.
5
 *
6
 * (c) Slince <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Slince\Di;
13
14
class Definition
15
{
16
    /**
17
     * @var mixed
18
     */
19
    protected $concrete;
20
21
    /**
22
     * @var string
23
     */
24
    protected $class;
25
26
    /**
27
     * Array of arguments.
28
     *
29
     * @var array
30
     */
31
    protected $arguments = [];
32
33
    /**
34
     * Array of setters.
35
     *
36
     * @var array
37
     */
38
    protected $calls = [];
39
40
    /**
41
     * Array of properties.
42
     *
43
     * @var array
44
     */
45
    protected $properties = [];
46
47
    /**
48
     * ['@Foo\Bar', 'createBaz']
49
     * or
50
     * ['Foo\Bar', 'createBaz'].
51
     *
52
     * @var \callable
53
     */
54
    protected $factory;
55
56
    /**
57
     * @var array
58
     */
59
    protected $tags;
60
61
    /**
62
     * @var boolean
63
     */
64
    protected $autowired;
65
66
    /**
67
     * @var boolean
68
     */
69
    protected $shared;
70
71
    /**
72
     * @var object
73
     */
74
    protected $resolved;
75
76
    public function __construct($concrete)
77
    {
78
        $this->concrete = $concrete;
79
    }
80
81
    /**
82
     * Set the concrete of the definition.
83
     *
84
     * @param mixed $concrete
85
     */
86
    public function setConcrete($concrete)
87
    {
88
        $this->concrete = $concrete;
89
    }
90
91
    /**
92
     * Get the concrete of the definition.
93
     *
94
     * @return mixed
95
     */
96
    public function getConcrete()
97
    {
98
        return $this->concrete;
99
    }
100
101
    /**
102
     * Set class for the definition.
103
     *
104
     * @param string $class
105
     *
106
     * @return $this
107
     */
108
    public function setClass($class)
109
    {
110
        $this->class = $class;
111
112
        return $this;
113
    }
114
115
    /**
116
     * Gets the class.
117
     *
118
     * @return string
119
     */
120
    public function getClass()
121
    {
122
        return $this->class;
123
    }
124
125
    /**
126
     * @param callable $factory
127
     *
128
     * @return $this
129
     */
130
    public function setFactory($factory)
131
    {
132
        $this->factory = $factory;
0 ignored issues
show
Documentation Bug introduced by
It seems like $factory of type callable is incompatible with the declared type object<callable> of property $factory.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
133
134
        return $this;
135
    }
136
137
    /**
138
     * @return callable
139
     */
140
    public function getFactory()
141
    {
142
        return $this->factory;
143
    }
144
145
    /**
146
     * @param array $properties
147
     */
148
    public function setProperties($properties)
149
    {
150
        $this->properties = $properties;
151
    }
152
153
    /**
154
     * Gets all properties.
155
     *
156
     * @return array
157
     */
158
    public function getProperties()
159
    {
160
        return $this->properties;
161
    }
162
163
    /**
164
     * Adds a property.
165
     *
166
     * @param int|string $name
167
     * @param mixed      $value
168
     *
169
     * @return $this
170
     */
171
    public function setProperty($name, $value)
172
    {
173
        $this->properties[$name] = $value;
174
175
        return $this;
176
    }
177
178
    /**
179
     * Gets the property by given name.
180
     *
181
     * @param string $name
182
     *
183
     * @return mixed
184
     */
185
    public function getProperty($name)
186
    {
187
        return isset($this->properties[$name]) ? $this->properties[$name] : null;
188
    }
189
190
    /**
191
     * add an argument.
192
     *
193
     * @param mixed $value
194
     *
195
     * @return $this
196
     */
197
    public function addArgument($value)
198
    {
199
        $this->arguments[] = $value;
200
201
        return $this;
202
    }
203
204
    /**
205
     * Sets a specific argument.
206
     *
207
     * @param string $key
208
     * @param mixed  $value
209
     *
210
     * @return $this
211
     */
212
    public function setArgument($key, $value)
213
    {
214
        $this->arguments[$key] = $value;
215
216
        return $this;
217
    }
218
219
    /**
220
     * Sets the arguments to pass to the service constructor/factory method.
221
     *
222
     * @param array $arguments
223
     *
224
     * @return $this
225
     */
226
    public function setArguments(array $arguments)
227
    {
228
        $this->arguments = $arguments;
229
230
        return $this;
231
    }
232
233
    /**
234
     * Gets all arguments of constructor.
235
     *
236
     * @return array
237
     */
238
    public function getArguments()
239
    {
240
        return $this->arguments;
241
    }
242
243
    /**
244
     * Gets the argument at the specified position of constructor.
245
     *
246
     * @param int|string $index
247
     *
248
     * @return mixed
249
     */
250
    public function getArgument($index)
251
    {
252
        return isset($this->arguments[$index]) ? $this->arguments[$index] : null;
253
    }
254
255
    /**
256
     * Adds a method.
257
     *
258
     * @param string       $method
259
     * @param string|array $arguments
260
     *
261
     * @return $this
262
     */
263
    public function addMethodCall($method, $arguments)
264
    {
265
        $this->calls[] = [
266
            $method,
267
            (array) $arguments,
268
        ];
269
270
        return $this;
271
    }
272
273
    /**
274
     * Sets the methods to call after service initialization.
275
     *
276
     * @param array methods
277
     *
278
     * @return $this
279
     */
280
    public function setMethodCalls(array $methods)
281
    {
282
        $this->calls = array();
283
        foreach ($methods as $call) {
284
            $this->addMethodCall($call[0], $call[1]);
285
        }
286
287
        return $this;
288
    }
289
290
    /**
291
     * Gets all methods.
292
     *
293
     * @return array
294
     */
295
    public function getMethodCalls()
296
    {
297
        return $this->calls;
298
    }
299
300
    /**
301
     * Check if the current definition has a given method to call after service initialization.
302
     *
303
     * @param string $method The method name to search for
304
     *
305
     * @return bool
306
     */
307
    public function hasMethodCall($method)
308
    {
309
        foreach ($this->calls as $call) {
310
            if ($call[0] === $method) {
311
                return true;
312
            }
313
        }
314
315
        return false;
316
    }
317
318
    /**
319
     * Sets tags for this definition.
320
     *
321
     * @param array $tags
322
     *
323
     * @return $this
324
     */
325
    public function setTags(array $tags)
326
    {
327
        $this->tags = $tags;
328
329
        return $this;
330
    }
331
332
    /**
333
     * Returns all tags.
334
     *
335
     * @return array An array of tags
336
     */
337
    public function getTags()
338
    {
339
        return $this->tags;
340
    }
341
342
    /**
343
     * Gets a tag by name.
344
     *
345
     * @param string $name The tag name
346
     *
347
     * @return array An array of attributes
348
     */
349
    public function getTag($name)
350
    {
351
        return isset($this->tags[$name]) ? $this->tags[$name] : array();
352
    }
353
354
    /**
355
     * Adds a tag for this definition.
356
     *
357
     * @param string $name       The tag name
358
     * @param array  $attributes An array of attributes
359
     *
360
     * @return $this
361
     */
362
    public function addTag($name, array $attributes = array())
363
    {
364
        $this->tags[$name][] = $attributes;
365
366
        return $this;
367
    }
368
369
    /**
370
     * Whether this definition has a tag with the given name.
371
     *
372
     * @param string $name
373
     *
374
     * @return bool
375
     */
376
    public function hasTag($name)
377
    {
378
        return isset($this->tags[$name]);
379
    }
380
381
    /**
382
     * Clears all tags for a given name.
383
     *
384
     * @param string $name The tag name
385
     *
386
     * @return $this
387
     */
388
    public function clearTag($name)
389
    {
390
        unset($this->tags[$name]);
391
392
        return $this;
393
    }
394
395
    /**
396
     * Clears the tags for this definition.
397
     *
398
     * @return $this
399
     */
400
    public function clearTags()
401
    {
402
        $this->tags = array();
403
404
        return $this;
405
    }
406
407
    /**
408
     * Is the definition autowired?
409
     *
410
     * @return bool
411
     */
412
    public function isAutowired()
413
    {
414
        return $this->autowired;
415
    }
416
417
    /**
418
     * Enables/disables autowiring.
419
     *
420
     * @param bool $autowired
421
     *
422
     * @return $this
423
     */
424
    public function setAutowired($autowired)
425
    {
426
        $this->autowired = (bool) $autowired;
427
428
        return $this;
429
    }
430
431
    /**
432
     * Sets if the service must be shared or not.
433
     *
434
     * @param bool $shared Whether the service must be shared or not
435
     *
436
     * @return $this
437
     */
438
    public function setShared($shared)
439
    {
440
        $this->shared = (bool) $shared;
441
442
        return $this;
443
    }
444
445
    /**
446
     * Whether this service is shared.
447
     *
448
     * @return bool
449
     */
450
    public function isShared()
451
    {
452
        return $this->shared;
453
    }
454
455
    /**
456
     * Get resolved instance of the definition.
457
     *
458
     * @return object
459
     */
460
    public function getResolved()
461
    {
462
        return $this->resolved;
463
    }
464
465
    /**
466
     * Set the resolved instance for the definition.
467
     *
468
     * @param object $resolved
469
     *
470
     * @return $this
471
     */
472
    public function setResolved($resolved)
473
    {
474
        $this->resolved = $resolved;
475
476
        return $this;
477
    }
478
}
479