|
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
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Render a view file with php. |
|
17
|
|
|
*/ |
|
18
|
|
|
class PhpView implements ViewInterface { |
|
19
|
|
|
use HasNameTrait, HasContextTrait; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* PHP view engine. |
|
23
|
|
|
* |
|
24
|
|
|
* @var PhpViewEngine |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $engine = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Filepath to view. |
|
30
|
|
|
* |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $filepath = ''; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Layout to use. |
|
37
|
|
|
* |
|
38
|
|
|
* @var ViewInterface|null |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $layout = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @codeCoverageIgnore |
|
46
|
|
|
* @param PhpViewEngine $engine |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct( PhpViewEngine $engine ) { |
|
49
|
|
|
$this->engine = $engine; |
|
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 static $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 static $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 |
|
$this->engine->pushLayoutContent( $this ); |
|
106
|
|
|
|
|
107
|
2 |
|
if ( $this->getLayout() !== null ) { |
|
108
|
1 |
|
return $this->getLayout()->toString(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
return $this->engine->getLayoutContent(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritDoc} |
|
116
|
|
|
* @throws ViewException |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function toResponse() { |
|
119
|
1 |
|
return (new Response()) |
|
120
|
1 |
|
->withHeader( 'Content-Type', 'text/html' ) |
|
121
|
1 |
|
->withBody( Psr7\stream_for( $this->toString() ) ); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|