Completed
Push — master ( da5ca0...e55bc1 )
by Atanas
02:05
created

PhpView::getLayout()   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
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 2
cts 2
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\Facades\View;
8
9
/**
10
 * Render a view file with php.
11
 */
12
class PhpView implements ViewInterface {
13
	use HasNameTrait, HasContextTrait;
14
15
	/**
16
	 * Stack of rendered layout contents.
17
	 *
18
	 * @var array<string>
19
	 */
20
	protected static $layout_content_stack = [];
21
22
	/**
23
	 * Filepath to view.
24
	 *
25
	 * @var string
26
	 */
27
	protected $filepath = '';
28
29
	/**
30
	 * Layout to use.
31
	 *
32
	 * @var ViewInterface|null
33
	 */
34
	protected $layout = null;
35
36
	/**
37
	 * Get the top-most layout content from the stack.
38
	 *
39
	 * @codeCoverageIgnore
40
	 * @return string
41
	 */
42
	public static function getLayoutContent() {
43
		$stack = static::$layout_content_stack;
44
45
		if ( empty( $stack ) ) {
46
			return '';
47
		}
48
49
		return $stack[ count( $stack ) - 1 ];
50
	}
51
52
	/**
53
	 * Get filepath.
54
	 *
55
	 * @return string
56
	 */
57 1
	public function getFilepath() {
58 1
		return $this->filepath;
59
	}
60
61
	/**
62
	 * Set filepath.
63
	 *
64
	 * @param  string $filepath
65
	 * @return self   $this
66
	 */
67 1
	public function setFilepath( $filepath ) {
68 1
		$this->filepath = $filepath;
69 1
		return $this;
70
	}
71
72
	/**
73
	 * Get layout.
74
	 *
75
	 * @return ViewInterface|null
76
	 */
77 1
	public function getLayout() {
78 1
		return $this->layout;
79
	}
80
81
	/**
82
	 * Set layout.
83
	 *
84
	 * @param  ViewInterface|null $layout
85
	 * @return self               $this
86
	 */
87 1
	public function setLayout( ViewInterface $layout ) {
88 1
		$this->layout = $layout;
89 1
		return $this;
90
	}
91
92
	/**
93
	 * {@inheritDoc}
94
	 * @throws ViewException
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 1
		}
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
	 * @throws ViewException
146
	 */
147 1
	public function toResponse() {
148 1
		return (new Response())
149 1
			->withHeader( 'Content-Type', 'text/html' )
150 1
			->withBody( Psr7\stream_for( $this->toString() ) );
151
	}
152
}
153