for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace WPEmerge\View;
use Exception;
use GuzzleHttp\Psr7;
use GuzzleHttp\Psr7\Response;
use WPEmerge\Facades\View;
/**
* Render a view file with php.
*/
class PhpView implements ViewInterface {
use HasContextTrait;
* View name.
*
* @var string
protected $name = '';
* Filepath to view.
protected $filepath = '';
* {@inheritDoc}
public function getName() {
return $this->name;
}
public function setName( $name ) {
$this->name = $name;
return $this;
* Get filepath.
* @return string
public function getFilepath() {
return $this->filepath;
* Set filepath.
* @param string $filepath
* @return self $this
public function setFilepath( $filepath ) {
$this->filepath = $filepath;
public function toString() {
if ( empty( $this->getName() ) ) {
throw new Exception( 'View must have a name.' );
if ( empty( $this->getFilepath() ) ) {
throw new Exception( 'View must have a filepath.' );
$global_context = ['global' => View::getGlobals()];
$local_context = $this->getContext();
$this->with( $global_context );
View::compose( $this );
$this->with( $local_context );
$renderer = function() {
ob_start();
$__context = $this->getContext();
extract( $__context );
include( $this->getFilepath() );
return ob_get_clean();
};
return $renderer();
public function toResponse() {
return (new Response())
->withHeader( 'Content-Type', 'text/html' )
->withBody( Psr7\stream_for( $this->toString() ) );