Maslosoft Staple API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
<?php
/**
* This software package is licensed under AGPL or Commercial license.
*
* @package maslosoft/staple
* @licence AGPL or Commercial
* @copyright Copyright (c) Piotr Masełkowski <pmaselkowski@gmail.com>
* @copyright Copyright (c) Maslosoft
* @link http://maslosoft.com/staple/
*/
namespace Maslosoft\Staple\Renderers;
use Maslosoft\Staple\Exceptions\NotFoundException;
use Maslosoft\Staple\Interfaces\RendererExtensionInterface;
use Maslosoft\Staple\Interfaces\RendererInterface;
use RuntimeException;
use SplFileInfo;
/**
* PassThroughRenderer
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class PassThroughRenderer extends AbstractRenderer implements RendererInterface, RendererExtensionInterface
{
const DispositionAuto = null;
const DispositionInline = 'inline';
const DispositionAttachment = 'attachment';
private $_extension = '';
public $disposition = self::DispositionAuto;
public function render($view = 'index', $data = [])
{
$fileName = sprintf('%s/%s/%s.%s', $this->getOwner()->getRootPath(), $this->getOwner()->getContentPath(), $view, $this->_extension);
$file = '';
$line = 0;
if (headers_sent($file, $line))
{
throw new RuntimeException(sprintf('Could not send file, headers already send. Output started in: %s:%s', $file, $line));
}
if (!file_exists($fileName))
{
throw new NotFoundException(sprintf('File `%s` not found', $fileName));
}
header("X-Sendfile: $fileName");
header('Content-Description: File Transfer');
header(sprintf('Content-Type: %s', mime_content_type($fileName)));
if (!empty($this->disposition))
{
header(sprintf('Content-Disposition: %s; filename=%s', $this->disposition, basename($fileName)));
}
header('Pragma: public');
header('Content-Length: ' . filesize($fileName));
$info = new SplFileInfo($fileName);
header(sprintf('ETag: %s', md5($fileName)));
header(sprintf('Last-Modified: %s', gmdate('D, d M Y H:i:s \G\M\T', $info->getMTime())));
// Cache it
header('Cache-Control: max-age=86400');
header('Expires: ' . gmdate('D, d M Y H:i:s \G\M\T', time() + 86400));
echo file_get_contents($fileName);
exit;
}
public function setExtension($extension)
{
$this->_extension = $extension;
}
}
API documentation generated by ApiGen