2016-10-13 13:11:22 -04:00
|
|
|
<?php declare(strict_types=1);
|
2016-08-04 14:55:37 -04:00
|
|
|
/**
|
|
|
|
* Ion
|
|
|
|
*
|
|
|
|
* Building blocks for web development
|
|
|
|
*
|
2016-10-13 13:11:22 -04:00
|
|
|
* PHP version 7
|
2016-08-26 23:10:20 -04:00
|
|
|
*
|
2016-08-04 14:55:37 -04:00
|
|
|
* @package Ion
|
2016-08-26 23:10:20 -04:00
|
|
|
* @author Timothy J. Warren <tim@timshomepage.net>
|
|
|
|
* @copyright 2015 - 2016 Timothy J. Warren
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @version 1.0.0
|
|
|
|
* @link https://git.timshomepage.net/timw4mail/ion
|
2016-08-04 14:55:37 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Aviat\Ion\View;
|
|
|
|
|
|
|
|
use Zend\Diactoros\Response\SapiEmitter;
|
2016-08-26 17:21:50 -04:00
|
|
|
|
|
|
|
use Aviat\Ion\Exception\DoubleRenderException;
|
2016-08-04 14:55:37 -04:00
|
|
|
use Aviat\Ion\View as BaseView;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base view class for Http output
|
|
|
|
*/
|
|
|
|
class HttpView extends BaseView {
|
|
|
|
|
2016-08-26 17:21:50 -04:00
|
|
|
/**
|
|
|
|
* Response mime type
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $contentType = '';
|
|
|
|
|
2016-08-04 14:55:37 -04:00
|
|
|
/**
|
|
|
|
* Do a redirect
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
* @param string $url
|
2016-08-26 17:21:50 -04:00
|
|
|
* @param int $code
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public function redirect(string $url, int $code)
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
ob_start();
|
|
|
|
$message = $this->response->getReasonPhrase($code);
|
|
|
|
$this->setStatusCode($code);
|
|
|
|
$this->response->withHeader('Location', $url);
|
|
|
|
|
|
|
|
if (PHP_SAPI !== 'cli')
|
|
|
|
{
|
|
|
|
header("HTTP/1.1 ${code} ${message}");
|
|
|
|
header("Location: {$url}");
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->hasRendered = TRUE;
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the status code of the request
|
|
|
|
*
|
|
|
|
* @param int $code
|
|
|
|
* @return HttpView
|
|
|
|
*/
|
2016-10-20 20:08:11 -04:00
|
|
|
public function setStatusCode(int $code): HttpView
|
2016-08-04 14:55:37 -04:00
|
|
|
{
|
|
|
|
$this->response = $this->response->withStatus($code)
|
|
|
|
->withProtocolVersion('1.1');
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-08-26 23:10:20 -04:00
|
|
|
* Send output to client. As it renders the view,
|
|
|
|
* any attempt to call again will result in a DoubleRenderException.
|
|
|
|
*
|
|
|
|
* @throws DoubleRenderException
|
|
|
|
* @return void
|
2016-08-04 14:55:37 -04:00
|
|
|
*/
|
|
|
|
public function send()
|
|
|
|
{
|
|
|
|
$this->output();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send the appropriate response
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2016-08-26 17:21:50 -04:00
|
|
|
* @throws DoubleRenderException
|
2016-08-04 14:55:37 -04:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function output()
|
|
|
|
{
|
2016-08-26 17:21:50 -04:00
|
|
|
if ($this->hasRendered)
|
|
|
|
{
|
|
|
|
throw new DoubleRenderException();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->response = $this->response->withHeader('Content-type', "{$this->contentType};charset=utf-8")
|
2016-08-04 14:55:37 -04:00
|
|
|
->withHeader('Content-Security-Policy', "script-src 'self'")
|
|
|
|
->withHeader('X-Content-Type-Options', 'nosniff')
|
|
|
|
->withHeader('X-XSS-Protection', '1;mode=block')
|
|
|
|
->withHeader('X-Frame-Options', 'SAMEORIGIN');
|
|
|
|
|
|
|
|
$sender = new SapiEmitter($this->response);
|
|
|
|
$sender->emit($this->response);
|
|
|
|
|
2016-08-26 17:21:50 -04:00
|
|
|
$this->hasRendered = TRUE;
|
|
|
|
}
|
2016-08-04 14:55:37 -04:00
|
|
|
}
|