Passed
Push — master ( 8dc024...328814 )
by Atanas
02:54
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
namespace WPEmerge\View;
4
5
use GuzzleHttp\Psr7;
6
use GuzzleHttp\Psr7\Response;
7
use WPEmerge\Exceptions\ViewException;
8
use WPEmerge\Facades\View;
9
10
/**
11
 * Render a view file with php.
12
 */
13
class PhpView implements ViewInterface {
14
	use HasNameTrait, HasContextTrait;
15
16
	/**
17
	 * Stack of rendered layout contents.
18
	 *
19
	 * @var array<string>
20
	 */
21
	protected static $layout_content_stack = [];
22
23
	/**
24
	 * Filepath to view.
25
	 *
26
	 * @var string
27
	 */
28
	protected $filepath = '';
29
30
	/**
31
	 * Layout to use.
32
	 *
33
	 * @var ViewInterface|null
34
	 */
35
	protected $layout = null;
36
37
	/**
38
	 * Get the top-most layout content from the stack.
39
	 *
40
	 * @codeCoverageIgnore
41
	 * @return string
42
	 */
43
	public static function getLayoutContent() {
44
		$stack = static::$layout_content_stack;
45
46
		if ( empty( $stack ) ) {
47
			return '';
48
		}
49
50
		return $stack[ count( $stack ) - 1 ];
51
	}
52
53
	/**
54
	 * Get filepath.
55
	 *
56
	 * @return string
57
	 */
58 1
	public function getFilepath() {
59 1
		return $this->filepath;
60
	}
61
62
	/**
63
	 * Set filepath.
64
	 *
65
	 * @param  string $filepath
66
	 * @return self   $this
67
	 */
68 1
	public function setFilepath( $filepath ) {
69 1
		$this->filepath = $filepath;
70 1
		return $this;
71
	}
72
73
	/**
74
	 * Get layout.
75
	 *
76
	 * @return ViewInterface|null
77
	 */
78 1
	public function getLayout() {
79 1
		return $this->layout;
80
	}
81
82
	/**
83
	 * Set layout.
84
	 *
85
	 * @param  ViewInterface|null $layout
86
	 * @return self               $this
87
	 */
88 1
	public function setLayout( ViewInterface $layout ) {
89 1
		$this->layout = $layout;
90 1
		return $this;
91
	}
92
93
	/**
94
	 * {@inheritDoc}
95
	 */
96 4
	public function toString() {
97 4
		if ( empty( $this->getName() ) ) {
98 1
			throw new ViewException( 'View must have a name.' );
99
		}
100
101 3
		if ( empty( $this->getFilepath() ) ) {
102 1
			throw new ViewException( 'View must have a filepath.' );
103
		}
104
105 2
		$clone = clone $this;
106 2
		$html = $clone->compose()->render();
107
108 2
		if ( $this->getLayout() !== null ) {
109 1
			static::$layout_content_stack[] = $html;
110 1
			$html = $this->getLayout()->toString();
111 1
			array_pop( static::$layout_content_stack );
112
		}
113
114 2
		return $html;
115
	}
116
117
	/**
118
	 * Compose the context.
119
	 *
120
	 * @return self $this
121
	 */
122 4
	protected function compose() {
123 4
		$context = $this->getContext();
124 4
		$this->with( ['global' => View::getGlobals()] );
125 4
		View::compose( $this );
126 4
		$this->with( $context );
127 4
		return $this;
128
	}
129
130
	/**
131
	 * Render the view to a string.
132
	 *
133
	 * @return string
134
	 */
135 1
	protected function render() {
136 1
		$__context = $this->getContext();
137 1
		ob_start();
138 1
		extract( $__context, EXTR_OVERWRITE );
139 1
		include $this->getFilepath();
140 1
		return ob_get_clean();
141
	}
142
143
	/**
144
	 * {@inheritDoc}
145
	 */
146 1
	public function toResponse() {
147 1
		return (new Response())
148 1
			->withHeader( 'Content-Type', 'text/html' )
149 1
			->withBody( Psr7\stream_for( $this->toString() ) );
150
	}
151
}
152