| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package WPEmerge |
| 4 |
* @author Atanas Angelov <hi@atanas.dev> |
| 5 |
* @copyright 2017-2019 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 |
public function getFilepath() { |
| 58 |
return $this->filepath; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Set filepath. |
| 63 |
* |
| 64 |
* @param string $filepath |
| 65 |
* @return static $this |
| 66 |
*/ |
| 67 |
public function setFilepath( $filepath ) { |
| 68 |
$this->filepath = $filepath; |
| 69 |
return $this; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Get layout. |
| 74 |
* |
| 75 |
* @return ViewInterface|null |
| 76 |
*/ |
| 77 |
public function getLayout() { |
| 78 |
return $this->layout; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Set layout. |
| 83 |
* |
| 84 |
* @param ViewInterface|null $layout |
| 85 |
* @return static $this |
| 86 |
*/ |
| 87 |
public function setLayout( $layout ) { |
| 88 |
$this->layout = $layout; |
| 89 |
return $this; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* {@inheritDoc} |
| 94 |
* @throws ViewException |
| 95 |
*/ |
| 96 |
public function toString() { |
| 97 |
if ( empty( $this->getName() ) ) { |
| 98 |
throw new ViewException( 'View must have a name.' ); |
| 99 |
} |
| 100 |
|
| 101 |
if ( empty( $this->getFilepath() ) ) { |
| 102 |
throw new ViewException( 'View must have a filepath.' ); |
| 103 |
} |
| 104 |
|
| 105 |
$this->engine->pushLayoutContent( $this ); |
| 106 |
|
| 107 |
if ( $this->getLayout() !== null ) { |
| 108 |
return $this->getLayout()->toString(); |
| 109 |
} |
| 110 |
|
| 111 |
return $this->engine->getLayoutContent(); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* {@inheritDoc} |
| 116 |
* @throws ViewException |
| 117 |
*/ |
| 118 |
public function toResponse() { |
| 119 |
return (new Response()) |
| 120 |
->withHeader( 'Content-Type', 'text/html' ) |
| 121 |
->withBody( Psr7\stream_for( $this->toString() ) ); |
| 122 |
} |
| 123 |
} |
| 124 |
|