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 PhpParser\BuilderFactory; |
21
|
|
|
use PhpParser\Node\Expr; |
22
|
|
|
use PhpParser\Node\Expr\{ArrayDimFetch, Assign}; |
23
|
|
|
use PhpParser\Node\Scalar\String_; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This trait adds visibility functionality to the service definition. |
27
|
|
|
* |
28
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
trait VisibilityTrait |
31
|
|
|
{ |
32
|
|
|
private bool $shared = true; |
33
|
|
|
|
34
|
|
|
private bool $public = true; |
35
|
|
|
|
36
|
|
|
private bool $lazy = false; |
37
|
|
|
|
38
|
|
|
private bool $abstract = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
public function shared(bool $boolean = true) |
44
|
|
|
{ |
45
|
|
|
$this->shared = $boolean; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function public(bool $boolean = true) |
54
|
|
|
{ |
55
|
|
|
$this->public = $boolean; |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function lazy(bool $boolean = true) |
64
|
|
|
{ |
65
|
|
|
$this->lazy = $boolean; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function abstract(bool $boolean = true) |
74
|
|
|
{ |
75
|
|
|
$this->abstract = $boolean; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function isPublic(): bool |
84
|
|
|
{ |
85
|
|
|
return $this->public; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function isShared(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->shared; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
public function isLazy(): bool |
100
|
|
|
{ |
101
|
|
|
return $this->lazy; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public function isAbstract(): bool |
108
|
|
|
{ |
109
|
|
|
return $this->abstract; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Triggers for buildable container. |
114
|
|
|
*/ |
115
|
|
|
public function triggerSharedBuild(string $id, Expr $service, BuilderFactory $builder): Assign |
116
|
|
|
{ |
117
|
|
|
$serviceVar = new ArrayDimFetch( |
118
|
|
|
$builder->propertyFetch($builder->var('this'), $this->public ? 'services' : 'privates'), |
119
|
|
|
new String_($id) |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return new Assign($serviceVar, $service); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|