Completed
Branch master (479eaa)
by Atanas
02:08
created

PhpView::toResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
nc 1
nop 0
dl 0
loc 4
c 0
b 0
f 0
cc 1
ccs 4
cts 4
cp 1
crap 1
rs 10
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
		return $view->render();
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 static $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 static             $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
		static::$layout_content_stack[] = $clone->compose();
114
115 2
		if ( $this->getLayout() !== null ) {
116 1
			return $this->getLayout()->toString();
117
		}
118
119 2
		return static::getLayoutContent();
120
	}
121
122
	/**
123
	 * Compose the context.
124
	 *
125
	 * @return static $this
126
	 */
127 4
	protected function compose() {
128 4
		$context = $this->getContext();
129 4
		$this->with( ['global' => View::getGlobals()] );
130 4
		View::compose( $this );
131 4
		$this->with( $context );
132 4
		return $this;
133
	}
134
135
	/**
136
	 * Render the view to a string.
137
	 *
138
	 * @return string
139
	 */
140 1
	protected function render() {
141 1
		$__context = $this->getContext();
142 1
		ob_start();
143 1
		extract( $__context, EXTR_OVERWRITE );
144
		/** @noinspection PhpIncludeInspection */
145 1
		include $this->getFilepath();
146 1
		return ob_get_clean();
147
	}
148
149
	/**
150
	 * {@inheritDoc}
151
	 * @throws ViewException
152
	 */
153 1
	public function toResponse() {
154 1
		return (new Response())
155 1
			->withHeader( 'Content-Type', 'text/html' )
156 1
			->withBody( Psr7\stream_for( $this->toString() ) );
157
	}
158
}
159