Failed Conditions
Branch refactor/kernels (fbf61a)
by Atanas
01:47
created

src/View/PhpView.php (1 issue)

Labels
Severity
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
		return $view->render();
53
	}
54
55
	/**
56
	 * Get filepath.
57
	 *
58
	 * @return string
59
	 */
60 1
	public function getFilepath() {
61 1
		return $this->filepath;
62
	}
63
64
	/**
65
	 * Set filepath.
66
	 *
67
	 * @param  string $filepath
68
	 * @return static $this
69
	 */
70 1
	public function setFilepath( $filepath ) {
71 1
		$this->filepath = $filepath;
72 1
		return $this;
73
	}
74
75
	/**
76
	 * Get layout.
77
	 *
78
	 * @return ViewInterface|null
79
	 */
80 1
	public function getLayout() {
81 1
		return $this->layout;
82
	}
83
84
	/**
85
	 * Set layout.
86
	 *
87
	 * @param  ViewInterface|null $layout
88
	 * @return static             $this
89
	 */
90 1
	public function setLayout( ViewInterface $layout ) {
91 1
		$this->layout = $layout;
92 1
		return $this;
93
	}
94
95
	/**
96
	 * {@inheritDoc}
97
	 * @throws ViewException
98
	 */
99 4
	public function toString() {
100 4
		if ( empty( $this->getName() ) ) {
101 1
			throw new ViewException( 'View must have a name.' );
102
		}
103
104 3
		if ( empty( $this->getFilepath() ) ) {
105 1
			throw new ViewException( 'View must have a filepath.' );
106
		}
107
108 2
		$clone = clone $this;
109 2
		static::$layout_content_stack[] = $clone->compose();
110
111 2
		if ( $this->getLayout() !== null ) {
112 1
			return $this->getLayout()->toString();
113
		}
114
115 2
		return static::getLayoutContent();
116
	}
117
118
	/**
119
	 * Compose the context.
120
	 *
121
	 * @return static $this
122
	 */
123 4
	protected function compose() {
124 4
		$context = $this->getContext();
125 4
		$this->with( ['global' => View::getGlobals()] );
126 4
		View::compose( $this );
127 4
		$this->with( $context );
128 4
		return $this;
129
	}
130
131
	/**
132
	 * Render the view to a string.
133
	 *
134
	 * @return string
135
	 */
136 1
	protected function render() {
137 1
		$__context = $this->getContext();
138 1
		ob_start();
139 1
		extract( $__context, EXTR_OVERWRITE );
140 1
		include $this->getFilepath();
141 1
		return ob_get_clean();
142
	}
143
144
	/**
145
	 * {@inheritDoc}
146
	 * @throws ViewException
147
	 */
148 1
	public function toResponse() {
149 1
		return (new Response())
150 1
			->withHeader( 'Content-Type', 'text/html' )
151 1
			->withBody( Psr7\stream_for( $this->toString() ) );
1 ignored issue
show
The function stream_for was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

151
			->withBody( /** @scrutinizer ignore-call */ Psr7\stream_for( $this->toString() ) );
Loading history...
152
	}
153
}
154