-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathPhpView.php
More file actions
123 lines (106 loc) · 2.09 KB
/
PhpView.php
File metadata and controls
123 lines (106 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
/**
* @package WPEmerge
* @author Atanas Angelov <hi@atanas.dev>
* @copyright 2017-2019 Atanas Angelov
* @license https://www.gnu.org/licenses/gpl-2.0.html GPL-2.0
* @link https://wpemerge.com/
*/
namespace WPEmerge\View;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Response;
/**
* Render a view file with php.
*/
class PhpView implements ViewInterface {
use HasNameTrait, HasContextTrait;
/**
* PHP view engine.
*
* @var PhpViewEngine
*/
protected $engine = null;
/**
* Filepath to view.
*
* @var string
*/
protected $filepath = '';
/**
* Layout to use.
*
* @var ViewInterface|null
*/
protected $layout = null;
/**
* Constructor.
*
* @codeCoverageIgnore
* @param PhpViewEngine $engine
*/
public function __construct( PhpViewEngine $engine ) {
$this->engine = $engine;
}
/**
* Get filepath.
*
* @return string
*/
public function getFilepath() {
return $this->filepath;
}
/**
* Set filepath.
*
* @param string $filepath
* @return static $this
*/
public function setFilepath( $filepath ) {
$this->filepath = $filepath;
return $this;
}
/**
* Get layout.
*
* @return ViewInterface|null
*/
public function getLayout() {
return $this->layout;
}
/**
* Set layout.
*
* @param ViewInterface|null $layout
* @return static $this
*/
public function setLayout( $layout ) {
$this->layout = $layout;
return $this;
}
/**
* {@inheritDoc}
* @throws ViewException
*/
public function toString() {
if ( empty( $this->getName() ) ) {
throw new ViewException( 'View must have a name.' );
}
if ( empty( $this->getFilepath() ) ) {
throw new ViewException( 'View must have a filepath.' );
}
$this->engine->pushLayoutContent( $this );
if ( $this->getLayout() !== null ) {
return $this->getLayout()->toString();
}
return $this->engine->getLayoutContent();
}
/**
* {@inheritDoc}
* @throws ViewException
*/
public function toResponse() {
return (new Response())
->withHeader( 'Content-Type', 'text/html' )
->withBody( Psr7\stream_for( $this->toString() ) );
}
}