Passed
Push — master ( 560f42...7bce3d )
by Atanas
02:16
created

ViewService::toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 2
dl 0
loc 2
c 0
b 0
f 0
cc 1
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace WPEmerge\View;
4
5
use Closure;
6
use WPEmerge\Facades\ViewEngine;
7
use WPEmerge\Helpers\Handler;
8
use WPEmerge\Helpers\Mixed;
9
10
/**
11
 * Provide general view-related functionality.
12
 */
13
class ViewService {
14
	/**
15
	 * View engine.
16
	 *
17
	 * @var ViewEngineInterface
18
	 */
19
	protected $view_engine = null;
20
21
	/**
22
	 * Global variables.
23
	 *
24
	 * @var array
25
	 */
26
	protected $globals = [];
27
28
	/**
29
	 * View composers.
30
	 *
31
	 * @var array
32
	 */
33
	protected $composers = [];
34
35
	/**
36
	 * Constructor.
37
	 *
38
	 * @codeCoverageIgnore
39
	 * @param ViewEngineInterface $view_engine
40
	 */
41
	public function __construct( ViewEngineInterface $view_engine ) {
42
		$this->view_engine = $view_engine;
43
	}
44
45
	/**
46
	 * Get global variables.
47
	 *
48
	 * @return array
49
	 */
50 2
	public function getGlobals() {
51 2
		return $this->globals;
52
	}
53
54
	/**
55
	 * Set a global variable.
56
	 *
57
	 * @param  string $key
58
	 * @param  mixed  $value
59
	 * @return void
60
	 */
61 1
	public function addGlobal( $key, $value ) {
62 1
		$this->globals[ $key ] = $value;
63 1
	}
64
65
	/**
66
	 * Set an array of global variables.
67
	 *
68
	 * @param  array $globals
69
	 * @return void
70
	 */
71 1
	public function addGlobals( $globals ) {
72 1
		foreach ( $globals as $key => $value ) {
73 1
			$this->addGlobal( $key, $value );
74
		}
75 1
	}
76
77
	/**
78
	 * Get view composer.
79
	 *
80
	 * @param  string    $view
81
	 * @return Handler[]
82
	 */
83 1
	public function getComposersForView( $view ) {
84 1
		$view = ViewEngine::canonical( $view );
85
86 1
		$composers = [];
87
88 1
		foreach ( $this->composers as $composer ) {
89 1
			if ( in_array( $view, $composer['views'] ) ) {
90 1
				$composers[] = $composer['composer'];
91
			}
92
		}
93
94 1
		return $composers;
95
	}
96
97
    /**
98
     * Add view composer.
99
     *
100
     * @param string|string[] $views
101
     * @param string|Closure  $composer
102
     * @return void
103
     * @throws \Exception
104
     */
105
	public function addComposer( $views, $composer ) {
106 1
		$views = array_map( function( $view ) {
107 1
			return ViewEngine::canonical( $view );
108 1
		}, Mixed::toArray( $views ) );
109 1
		$handler = new Handler( $composer, 'compose' );
110
111 1
		$this->composers[] = [
112 1
			'views' => $views,
113 1
			'composer' => $handler,
114
		];
115 1
	}
116
117
	/**
118
	 * Get the composed context for a view.
119
	 * Passes all arguments to the composer.
120
	 *
121
	 * @param  ViewInterface $view
122
	 * @return void
123
	 */
124 1
	public function compose( ViewInterface $view ) {
125 1
		$composers = $this->getComposersForView( $view->getName() );
126
127 1
		foreach ( $composers as $composer ) {
128 1
			$composer->execute( $view );
129
		}
130 1
	}
131
132
	/**
133
	 * Create a view instance.
134
	 *
135
	 * @param  string|string[] $views
136
	 * @return ViewInterface
137
	 */
138 1
	public function make( $views ) {
139 1
		$views = Mixed::toArray( $views );
140 1
		return $this->view_engine->make( $views );
141
	}
142
}
143