Passed
Push — master ( cb7cfa...5e4de9 )
by Atanas
02:13
created

PhpView::getLayoutContent()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
ccs 0
cts 0
cp 0
crap 6
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 rendered layout contents.
24
	 *
25
	 * @var array<string>
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
		$stack = static::$layout_content_stack;
51
52
		if ( empty( $stack ) ) {
53
			return '';
54
		}
55
56
		return $stack[ count( $stack ) - 1 ];
57
	}
58
59
	/**
60
	 * Get filepath.
61
	 *
62
	 * @return string
63
	 */
64 1
	public function getFilepath() {
65 1
		return $this->filepath;
66
	}
67
68
	/**
69
	 * Set filepath.
70
	 *
71
	 * @param  string $filepath
72
	 * @return self   $this
73
	 */
74 1
	public function setFilepath( $filepath ) {
75 1
		$this->filepath = $filepath;
76 1
		return $this;
77
	}
78
79
	/**
80
	 * Get layout.
81
	 *
82
	 * @return ViewInterface|null
83
	 */
84 1
	public function getLayout() {
85 1
		return $this->layout;
86
	}
87
88
	/**
89
	 * Set layout.
90
	 *
91
	 * @param  ViewInterface|null $layout
92
	 * @return self               $this
93
	 */
94 1
	public function setLayout( ViewInterface $layout ) {
95 1
		$this->layout = $layout;
96 1
		return $this;
97
	}
98
99
	/**
100
	 * {@inheritDoc}
101
	 * @throws ViewException
102
	 */
103 4
	public function toString() {
104 4
		if ( empty( $this->getName() ) ) {
105 1
			throw new ViewException( 'View must have a name.' );
106
		}
107
108 3
		if ( empty( $this->getFilepath() ) ) {
109 1
			throw new ViewException( 'View must have a filepath.' );
110
		}
111
112 2
		$clone = clone $this;
113 2
		$html = $clone->compose()->render();
114
115 2
		if ( $this->getLayout() !== null ) {
116 1
			static::$layout_content_stack[] = $html;
117 1
			$html = $this->getLayout()->toString();
118 1
			array_pop( static::$layout_content_stack );
119 1
		}
120
121 2
		return $html;
122
	}
123
124
	/**
125
	 * Compose the context.
126
	 *
127
	 * @return self $this
128
	 */
129 4
	protected function compose() {
130 4
		$context = $this->getContext();
131 4
		$this->with( ['global' => View::getGlobals()] );
132 4
		View::compose( $this );
133 4
		$this->with( $context );
134 4
		return $this;
135
	}
136
137
	/**
138
	 * Render the view to a string.
139
	 *
140
	 * @return string
141
	 */
142 1
	protected function render() {
143 1
		$__context = $this->getContext();
144 1
		ob_start();
145 1
		extract( $__context, EXTR_OVERWRITE );
146 1
		include $this->getFilepath();
147 1
		return ob_get_clean();
148
	}
149
150
	/**
151
	 * {@inheritDoc}
152
	 * @throws ViewException
153
	 */
154 1
	public function toResponse() {
155 1
		return (new Response())
156 1
			->withHeader( 'Content-Type', 'text/html' )
157 1
			->withBody( Psr7\stream_for( $this->toString() ) );
158
	}
159
}
160