Completed
Push — master ( 332fd2...eb9931 )
by Glynn
33s queued 14s
created

BladeOne_Bootstrap::use()   B

Complexity

Conditions 7
Paths 1

Size

Total Lines 49
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 27
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 49
rs 8.5546
1
<?php
2
3
declare( strict_types=1 );
4
5
/**
6
 * Implementation of BladeOne for the PinkCrab Perique frameworks Renderable interface
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_Provider
23
 */
24
25
namespace PinkCrab\BladeOne;
26
27
use Dice\Dice;
28
use eftec\bladeone\BladeOne;
29
use PinkCrab\Perique\Application\Hooks;
30
use PinkCrab\BladeOne\BladeOne_Provider;
31
use PinkCrab\BladeOne\PinkCrab_BladeOne;
32
use PinkCrab\Perique\Application\Config;
33
use PinkCrab\Perique\Interfaces\Renderable;
34
use PinkCrab\Perique\Services\View\PHP_Engine;
35
use PinkCrab\BladeOne\Abstract_BladeOne_Config;
36
37
class BladeOne_Bootstrap {
38
39
	/**
40
	 * Sets the rules for bladeone and unsets the default PHP_Engine for rendering
41
	 *
42
	 * @param string $template_path
43
	 * @param string|null $compiled_path
44
	 * @param int $mode
45
	 * @param class-string|BladeOne $blade
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string|BladeOne at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string|BladeOne.
Loading history...
46
	 * @return void
47
	 */
48
	public static function use( $template_path = null, ?string $compiled_path = null, int $mode = 0, $blade = null ) {
49
50
		add_filter(
51
			Hooks::APP_INIT_SET_DI_RULES,
52
			function( $rules ) use ( $template_path, $compiled_path, $mode, $blade ) {
53
54
				// Unset the global PHP_Engine useage.
55
				if ( array_key_exists( '*', $rules )
56
				&& array_key_exists( 'substitutions', $rules['*'] )
57
				&& array_key_exists( Renderable::class, $rules['*']['substitutions'] )
58
				&& is_a( $rules['*']['substitutions'][ Renderable::class ], PHP_Engine::class ) ) {
59
60
					// If template path is not set, get from renderable.
61
					if ( is_null( $template_path ) ) {
62
						$template_path = $rules['*']['substitutions'][ Renderable::class ]->base_view_path();
63
					}
64
					unset( $rules['*']['substitutions'][ Renderable::class ] );
65
				}
66
67
				// If there is no compiled path, set to to uploads.
68
				if ( is_null( $compiled_path ) ) {
69
					$wp_upload_dir = wp_upload_dir();
70
					$compiled_path = sprintf( '%1$s%2$scompiled%2$sblade', $wp_upload_dir['basedir'], \DIRECTORY_SEPARATOR );
71
				}
72
73
				// Get the version of Blade to start.
74
				$blade = self::get_blade_instance( $blade );
75
76
				$rules[ BladeOne_Provider::class ] = array(
77
					'substitutions' => array(
78
						PinkCrab_BladeOne::class => new $blade( $template_path, $compiled_path, $mode ),
79
					),
80
					'call'          => array(
81
						array( 'allow_pipe', array() ),
82
					),
83
				);
84
85
				$rules[ Renderable::class ] = array(
86
					'instanceOf' => BladeOne_Provider::class,
87
					'shared'     => true,
88
				);
89
90
				$rules[ Abstract_BladeOne_Config::class ] = array(
91
					'call' => array(
92
						array( 'set_renderable', array( array( Dice::INSTANCE => Renderable::class ) ) ),
93
					),
94
				);
95
96
				return $rules;
97
			}
98
		);
99
	}
100
101
	/**
102
	 * Gets the class used for bladeone instance
103
	 *
104
	 * @param mixed $blade
105
	 * @return class-string
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
106
	 */
107
	protected static function get_blade_instance( $blade ): string {
108
		// If we have a populated instance of BladeOne, get the full class name.
109
		if ( is_object( $blade ) && is_a( $blade, PinkCrab_BladeOne::class ) ) {
110
			$blade = get_class( $blade );
111
		}
112
113
		if ( is_string( $blade ) && is_a( $blade, PinkCrab_BladeOne::class, true ) ) {
114
			return $blade;
115
		}
116
117
		return PinkCrab_BladeOne::class;
118
	}
119
}
120