1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Bridge for using the WP_Dice container with the PinkCrab App. |
7
|
|
|
* |
8
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
11
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
12
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
13
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
14
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
15
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
16
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
17
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
18
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
19
|
|
|
* |
20
|
|
|
* @author Glynn Quelch <[email protected]> |
21
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
22
|
|
|
* @package PinkCrab\Perique\Dice |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace PinkCrab\Perique\Services\Dice; |
26
|
|
|
|
27
|
|
|
use Dice\Dice; |
28
|
|
|
use PinkCrab\Perique\Application\Hooks; |
29
|
|
|
use PinkCrab\Perique\Interfaces\DI_Container; |
30
|
|
|
use PinkCrab\Perique\Exceptions\DI_Container_Exception; |
31
|
|
|
|
32
|
|
|
class PinkCrab_Dice implements DI_Container { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Holds the instance of DICE to work with. |
36
|
|
|
* |
37
|
|
|
* @var Dice $dice |
38
|
|
|
*/ |
39
|
|
|
protected Dice $dice; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Passes in the initial dice instance. |
43
|
|
|
* |
44
|
|
|
* @param Dice $dice |
45
|
|
|
*/ |
46
|
|
|
public function __construct( Dice $dice ) { |
47
|
|
|
$this->dice = $dice; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Lazy stack instancing. |
52
|
|
|
* |
53
|
|
|
* @param Dice $dice |
54
|
|
|
* @return self |
55
|
|
|
*/ |
56
|
|
|
public static function withDice( Dice $dice ): self { // phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
57
|
|
|
return new PinkCrab_Dice( $dice ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* ContainerInterface implementation of get. |
62
|
|
|
* Will attempt to construct autowired. |
63
|
|
|
* |
64
|
|
|
* @param string $id Class name (fully namespaced.) |
65
|
|
|
* @return object|null |
66
|
|
|
*/ |
67
|
|
|
public function get( string $id ): ?object { |
68
|
|
|
if ( ! $this->has( $id ) ) { |
69
|
|
|
throw new DI_Container_Exception( "{$id} not defined in container", 1 ); |
70
|
|
|
} |
71
|
|
|
return $this->create( $id ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Checks if a specific class is registered or exists. |
76
|
|
|
* Doesn't take into account the ability to autowire. |
77
|
|
|
* |
78
|
|
|
* @param string $id Class name (fully namespaced.) |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function has( string $id ): bool { |
82
|
|
|
$from_dice = $this->dice->getRule( $id ); |
83
|
|
|
// If set in global rules. |
84
|
|
|
if ( array_key_exists( 'substitutions', $from_dice ) |
85
|
|
|
&& array_key_exists( $id, $from_dice['substitutions'] ) ) { |
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
// If set with a replacement instance. |
90
|
|
|
if ( array_key_exists( 'instanceOf', $from_dice ) ) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Checks if the class exists |
95
|
|
|
return class_exists( $id ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Proxy for addRule. |
100
|
|
|
* |
101
|
|
|
* @param string $name |
102
|
|
|
* @param array<string, string|object|mixed[]> $rule |
103
|
|
|
* @return PinkCrab_Dice |
104
|
|
|
*/ |
105
|
|
|
public function addRule( string $name, array $rule ): DI_Container { // phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
106
|
|
|
$this->dice = $this->dice->addRule( $name, $rule ); |
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Proxy for addRules |
112
|
|
|
* |
113
|
|
|
* @param array<string, mixed[]> $rules |
114
|
|
|
* @return PinkCrab_Dice |
115
|
|
|
*/ |
116
|
|
|
public function addRules( array $rules ): DI_Container { // phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid |
117
|
|
|
$this->dice = $this->dice->addRules( apply_filters( Hooks::APP_INIT_SET_DI_RULES, $rules ) ); |
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Proxy for create, but with third param removed (see dice code comments) |
123
|
|
|
* |
124
|
|
|
* @param string $name |
125
|
|
|
* @param array<mixed> $args |
126
|
|
|
* @return object|null |
127
|
|
|
*/ |
128
|
|
|
public function create( string $name, array $args = array() ): ?object { |
129
|
|
|
return $this->dice->create( $name, $args ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Gets a defined rule from the container. |
134
|
|
|
* |
135
|
|
|
* @param string $name |
136
|
|
|
* @return array<mixed> |
137
|
|
|
*/ |
138
|
|
|
public function getRule( string $name ): ?array { |
139
|
|
|
return $this->dice->getRule( $name ); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|