1 | <?php |
||
2 | |||
3 | declare( strict_types=1 ); |
||
4 | |||
5 | /** |
||
6 | * The BladeOne Module for Perique. |
||
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\BladeOne_Engine |
||
23 | */ |
||
24 | |||
25 | namespace PinkCrab\BladeOne; |
||
26 | |||
27 | use PinkCrab\Loader\Hook_Loader; |
||
28 | use PinkCrab\BladeOne\BladeOne_Engine; |
||
29 | use PinkCrab\Perique\Interfaces\Module; |
||
30 | use PinkCrab\BladeOne\PinkCrab_BladeOne; |
||
31 | use PinkCrab\Perique\Services\View\View; |
||
32 | use PinkCrab\Perique\Interfaces\Renderable; |
||
33 | use PinkCrab\Perique\Application\App_Config; |
||
34 | use PinkCrab\Perique\Interfaces\DI_Container; |
||
35 | |||
36 | /** |
||
37 | * BladeOne Module for Perique. |
||
38 | */ |
||
39 | class BladeOne implements Module { |
||
40 | |||
41 | private ?string $template_path = null; |
||
42 | private ?string $compiled_path = null; |
||
43 | |||
44 | private int $mode = PinkCrab_BladeOne::MODE_AUTO; |
||
45 | private int $comment_mode = PinkCrab_BladeOne::COMMENT_PHP; |
||
46 | |||
47 | /** |
||
48 | * Holds the config closure. |
||
49 | * |
||
50 | * @var ?\Closure(BladeOne_Engine):BladeOne_Engine |
||
51 | */ |
||
52 | private $config = null; |
||
53 | |||
54 | /** |
||
55 | * Set the template path. |
||
56 | * |
||
57 | * @param string $template_path |
||
58 | * @return self |
||
59 | */ |
||
60 | public function template_path( string $template_path ): self { |
||
61 | $this->template_path = $template_path; |
||
62 | return $this; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Set the compiled path. |
||
67 | * |
||
68 | * @param string $compiled_path |
||
69 | * @return self |
||
70 | */ |
||
71 | public function compiled_path( string $compiled_path ): self { |
||
72 | $this->compiled_path = $compiled_path; |
||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * Set the mode. |
||
78 | * |
||
79 | * @param integer $mode |
||
80 | * @return self |
||
81 | */ |
||
82 | public function mode( int $mode ): self { |
||
83 | $this->mode = $mode; |
||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Set the comment mode. |
||
89 | * |
||
90 | * @param integer $comment_mode |
||
91 | * @return self |
||
92 | */ |
||
93 | public function comment_mode( int $comment_mode ): self { |
||
94 | $this->comment_mode = $comment_mode; |
||
95 | return $this; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Provider config. |
||
100 | * |
||
101 | * @param \Closure(BladeOne_Engine):BladeOne_Engine $config |
||
102 | * @return self |
||
103 | */ |
||
104 | public function config( \Closure $config ): self { |
||
105 | $this->config = $config; |
||
106 | return $this; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Creates the shared instance of the module and defines the |
||
111 | * DI Rules to use the BladeOne_Engine. |
||
112 | * |
||
113 | * @pram App_Config $config |
||
114 | * @pram Hook_Loader $loader |
||
115 | * @pram DI_Container $di_container |
||
116 | * @return void |
||
117 | */ |
||
118 | public function pre_boot( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
||
119 | // @codeCoverageIgnoreStart |
||
120 | if ( ! function_exists( 'WP_Filesystem' ) ) { |
||
121 | // @phpstan-ignore-next-line |
||
122 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
||
123 | } |
||
124 | // @codeCoverageIgnoreEnd |
||
125 | \WP_Filesystem(); |
||
126 | global $wp_filesystem; |
||
127 | |||
128 | // If we dont have an instance of the WP_Filesystem, throw an exception. |
||
129 | if ( ! $wp_filesystem instanceof \WP_Filesystem_Base ) { |
||
130 | // @codeCoverageIgnoreStart |
||
131 | throw new \RuntimeException( 'Unable to create WP_Filesystem instance' ); |
||
132 | // @codeCoverageIgnoreEnd |
||
133 | } |
||
134 | |||
135 | $wp_upload_dir = wp_upload_dir(); |
||
136 | $compiled_path = $this->compiled_path ?? sprintf( '%1$s%2$sblade-cache', $wp_upload_dir['basedir'], \DIRECTORY_SEPARATOR ); // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
||
137 | $instance = new PinkCrab_BladeOne( |
||
138 | $this->template_path ?? $config->view_path(), |
||
139 | $compiled_path, |
||
140 | $this->mode, |
||
141 | $this->comment_mode |
||
142 | ); |
||
143 | |||
144 | $instance->setAuth( ...$this->get_auth_data() ); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
145 | |||
146 | // Create the compiled path if it does not exist. |
||
147 | if ( ! $wp_filesystem->exists( $compiled_path ) ) { |
||
148 | |||
149 | // Create the directory. |
||
150 | $wp_filesystem->mkdir( $compiled_path ); // phpcs:ignore WordPress.VIP.MkdirPermissions |
||
151 | } |
||
152 | |||
153 | $di_container->addRule( |
||
154 | BladeOne_Engine::class, |
||
155 | array( |
||
156 | 'constructParams' => array( |
||
157 | $instance, |
||
158 | ), |
||
159 | 'call' => array( |
||
160 | array( 'allow_pipe', array() ), |
||
161 | ), |
||
162 | 'shared' => true, |
||
163 | ) |
||
164 | ); |
||
165 | |||
166 | $di_container->addRule( |
||
167 | Renderable::class, |
||
168 | array( |
||
169 | 'instanceOf' => BladeOne_Engine::class, |
||
170 | 'shared' => true, |
||
171 | ) |
||
172 | ); |
||
173 | |||
174 | $di_container->addRule( |
||
175 | View::class, |
||
176 | array( |
||
177 | 'substitutions' => array( |
||
178 | Renderable::class => BladeOne_Engine::class, |
||
179 | ), |
||
180 | 'shared' => true, |
||
181 | ) |
||
182 | ); |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Gets the current logged in user details |
||
187 | * |
||
188 | * @return array{0:string, 1:string, 2:string[]} |
||
189 | */ |
||
190 | private function get_auth_data(): array { |
||
191 | |||
192 | // @codeCoverageIgnoreStart |
||
193 | if ( ! function_exists( 'wp_get_current_user' ) ) { |
||
194 | // @phpstan-ignore-next-line |
||
195 | require_once ABSPATH . 'wp-includes/pluggable.php'; |
||
196 | } |
||
197 | // @codeCoverageIgnoreEnd |
||
198 | |||
199 | $user = \wp_get_current_user(); |
||
200 | return array( |
||
201 | 0 !== $user->ID ? $user->user_login : '', |
||
202 | 0 !== $user->ID ? $user->roles[0] : '', |
||
203 | 0 !== $user->ID ? array_keys( array_filter( $user->allcaps ) ) : array(), |
||
204 | ); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * Allows for the config to be passed to the provider, before its used. |
||
209 | * |
||
210 | * @pram App_Config $config |
||
211 | * @pram Hook_Loader $loader |
||
212 | * @pram DI_Container $di_container |
||
213 | * @return void |
||
214 | */ |
||
215 | public function pre_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
||
216 | |||
217 | $provider = $di_container->create( BladeOne_Engine::class ); |
||
218 | |||
219 | // If dont have an instance of BladeOne_Engine, return. |
||
220 | if ( ! $provider instanceof BladeOne_Engine ) { |
||
221 | throw new \RuntimeException( 'Unable to create BladeOne_Engine instance to configure instance' ); |
||
222 | } |
||
223 | |||
224 | // Pass the config to the provider, if set. |
||
225 | if ( ! is_null( $this->config ) ) { |
||
226 | \call_user_func( $this->config, $provider ); |
||
227 | } |
||
228 | } |
||
229 | |||
230 | ## Unused methods |
||
231 | |||
232 | |||
233 | /** |
||
234 | * Unused Mthod. |
||
235 | * |
||
236 | * @param App_Config $config The App_Config instance. |
||
237 | * @param Hook_Loader $loader The Hook_Loader instance. |
||
238 | * @param DI_Container $di_container The DI_Container instance. |
||
239 | * |
||
240 | * @return void |
||
241 | */ |
||
242 | public function post_register( App_Config $config, Hook_Loader $loader, DI_Container $di_container ): void {} // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInImplementedInterfaceBeforeLastUsed |
||
243 | |||
244 | /** |
||
245 | * Unused method. |
||
246 | * |
||
247 | * @return string|null |
||
248 | */ |
||
249 | public function get_middleware(): ?string { |
||
250 | return null; |
||
251 | } |
||
252 | } |
||
253 |