Passed
Push — master ( ba86e8...c40970 )
by Atanas
01:50
created

PhpView::compose()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 6
ccs 6
cts 6
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   WPEmerge
4
 * @author    Atanas Angelov <[email protected]>
5
 * @copyright 2018 Atanas Angelov
6
 * @license   https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
7
 * @link      https://wpemerge.com/
8
 */
9
10
namespace WPEmerge\View;
11
12
use GuzzleHttp\Psr7;
13
use GuzzleHttp\Psr7\Response;
14
use WPEmerge\Facades\View;
15
16
/**
17
 * Render a view file with php.
18
 */
19
class PhpView implements ViewInterface {
20
	use HasNameTrait, HasContextTrait;
21
22
	/**
23
	 * Stack of views ready to be rendered.
24
	 *
25
	 * @var array<ViewInterface>
26
	 */
27
	protected static $layout_content_stack = [];
28
29
	/**
30
	 * Filepath to view.
31
	 *
32
	 * @var string
33
	 */
34
	protected $filepath = '';
35
36
	/**
37
	 * Layout to use.
38
	 *
39
	 * @var ViewInterface|null
40
	 */
41
	protected $layout = null;
42
43
	/**
44
	 * Get the top-most layout content from the stack.
45
	 *
46
	 * @codeCoverageIgnore
47
	 * @return string
48
	 */
49
	public static function getLayoutContent() {
50
		$view = array_pop( static::$layout_content_stack );
51
52
		if ( ! $view ) {
53
			return '';
54
		}
55
56
		$clone = clone $view;
57
		View::compose( $clone );
58
		return $clone->render();
59
	}
60
61
	/**
62
	 * Get filepath.
63
	 *
64
	 * @return string
65
	 */
66 1
	public function getFilepath() {
67 1
		return $this->filepath;
68
	}
69
70
	/**
71
	 * Set filepath.
72
	 *
73
	 * @param  string $filepath
74
	 * @return static $this
75
	 */
76 1
	public function setFilepath( $filepath ) {
77 1
		$this->filepath = $filepath;
78 1
		return $this;
79
	}
80
81
	/**
82
	 * Get layout.
83
	 *
84
	 * @return ViewInterface|null
85
	 */
86 1
	public function getLayout() {
87 1
		return $this->layout;
88
	}
89
90
	/**
91
	 * Set layout.
92
	 *
93
	 * @param  ViewInterface|null $layout
94
	 * @return static             $this
95
	 */
96 1
	public function setLayout( ViewInterface $layout ) {
97 1
		$this->layout = $layout;
98 1
		return $this;
99
	}
100
101
	/**
102
	 * {@inheritDoc}
103
	 * @throws ViewException
104
	 */
105 4
	public function toString() {
106 4
		if ( empty( $this->getName() ) ) {
107 1
			throw new ViewException( 'View must have a name.' );
108
		}
109
110 3
		if ( empty( $this->getFilepath() ) ) {
111 1
			throw new ViewException( 'View must have a filepath.' );
112
		}
113
114 2
		static::$layout_content_stack[] = $this;
115
116 2
		if ( $this->getLayout() !== null ) {
117 1
			return $this->getLayout()->toString();
118
		}
119
120 2
		return static::getLayoutContent();
121
	}
122
123
	/**
124
	 * Render the view to a string.
125
	 *
126
	 * @return string
127
	 */
128 1
	protected function render() {
129 1
		$__context = $this->getContext();
130 1
		ob_start();
131 1
		extract( $__context, EXTR_OVERWRITE );
132
		/** @noinspection PhpIncludeInspection */
133 1
		include $this->getFilepath();
134 1
		return ob_get_clean();
135
	}
136
137
	/**
138
	 * {@inheritDoc}
139
	 * @throws ViewException
140
	 */
141 1
	public function toResponse() {
142 1
		return (new Response())
143 1
			->withHeader( 'Content-Type', 'text/html' )
144 1
			->withBody( Psr7\stream_for( $this->toString() ) );
145
	}
146
}
147