ion/src/Ion/View/HttpView.php

103 lines
1.9 KiB
PHP
Raw Normal View History

<?php
/**
* Ion
*
* Building blocks for web development
*
* @package Ion
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
* @license MIT
*/
namespace Aviat\Ion\View;
use Zend\Diactoros\Response\SapiEmitter;
2016-08-26 17:21:50 -04:00
use Aviat\Ion\Exception\DoubleRenderException;
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 = '';
/**
* Do a redirect
*
* @codeCoverageIgnore
* @param string $url
2016-08-26 17:21:50 -04:00
* @param int $code
* @return void
*/
public function redirect($url, $code)
{
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
*/
public function setStatusCode($code)
{
$this->response = $this->response->withStatus($code)
->withProtocolVersion('1.1');
return $this;
}
/**
2016-08-26 17:21:50 -04:00
* @inheritdoc
*/
public function send()
{
$this->output();
}
/**
* Send the appropriate response
*
* @codeCoverageIgnore
2016-08-26 17:21:50 -04:00
* @throws DoubleRenderException
* @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")
->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;
}
}