|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of DivineNii opensource projects. |
|
7
|
|
|
* |
|
8
|
|
|
* PHP version 7.4 and above required |
|
9
|
|
|
* |
|
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
|
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
|
13
|
|
|
* |
|
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
15
|
|
|
* file that was distributed with this source code. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
namespace Rade\DI\Definitions\Traits; |
|
19
|
|
|
|
|
20
|
|
|
use Rade\DI\AbstractContainer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* DefinitionAware trait. |
|
24
|
|
|
* |
|
25
|
|
|
* This trait is currently under re-thinking process, and can potentially changed to |
|
26
|
|
|
* be (deprecated) for a more stable approach in attaching container to definition class. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
|
29
|
|
|
*/ |
|
30
|
|
|
trait DefinitionAwareTrait |
|
31
|
|
|
{ |
|
32
|
|
|
protected AbstractContainer $container; |
|
33
|
|
|
|
|
34
|
|
|
protected ?string $innerId = null; |
|
35
|
|
|
|
|
36
|
|
|
/** @var array<int|string,mixed> */ |
|
37
|
|
|
private array $tags; |
|
38
|
|
|
|
|
39
|
|
|
public function bindWith(string $id, AbstractContainer $container): void |
|
40
|
|
|
{ |
|
41
|
|
|
$this->innerId = $id; |
|
42
|
|
|
$this->container = $container; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Adds a tag for this definition. |
|
47
|
|
|
* |
|
48
|
|
|
* @param mixed $value |
|
49
|
|
|
* |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
|
|
public function tag(string $name, $value = true) |
|
53
|
|
|
{ |
|
54
|
|
|
if (null !== $this->innerId) { |
|
55
|
|
|
$this->tags[] = $name; |
|
56
|
|
|
$this->container->tag($this->innerId, $name, $value); |
|
57
|
|
|
} else { |
|
58
|
|
|
$this->tags[$name] = $value; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Sets tags for this definition. |
|
66
|
|
|
* |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function tags(array $tags) |
|
70
|
|
|
{ |
|
71
|
|
|
foreach ($tags as $tag => $value) { |
|
72
|
|
|
$this->tag(\is_int($tag) ? $value : $tag, \is_int($tag) ? true : $value); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* If definition has tag, a value will be returned else null. |
|
80
|
|
|
* |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
public function tagged(string $name) |
|
84
|
|
|
{ |
|
85
|
|
|
if (null !== $this->innerId) { |
|
86
|
|
|
return $this->container->tagged($name, $this->innerId); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
return $this->tags[$name] ?? null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Whether tags exists in definition. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function hasTags(): bool |
|
96
|
|
|
{ |
|
97
|
|
|
return !empty($this->tags); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns all tags. |
|
102
|
|
|
* |
|
103
|
|
|
* @return array<string,mixed> |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getTags(): array |
|
106
|
|
|
{ |
|
107
|
|
|
$tagged = []; |
|
108
|
|
|
|
|
109
|
|
|
foreach ($this->tags as $offset => $tag) { |
|
110
|
|
|
if (!\is_numeric($offset)) { |
|
111
|
|
|
$tagged[$offset] = $tag; |
|
112
|
|
|
|
|
113
|
|
|
continue; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$tagged[$tag] = $this->tagged($tag); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return $tagged; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|