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: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222:
<?php
namespace Nethgui\Module;
class Resource extends \Nethgui\Controller\AbstractController implements \Nethgui\Component\DependencyConsumer
{
private $fileName;
private $cachePath;
private $staticFiles;
protected function initializeAttributes(\Nethgui\Module\ModuleAttributesInterface $base)
{
$attributes = new SystemModuleAttributesProvider();
$attributes->initializeFromModule($this);
return $attributes;
}
public function bind(\Nethgui\Controller\RequestInterface $request)
{
parent::bind($request);
$fileName = implode('/', $request->getPath());
if ( ! $fileName) {
$this->fileName = FALSE;
return;
}
$this->fileName = $fileName . '.' . $request->getExtension();
if ( ! $this->getPhpWrapper()->file_exists($this->getCachePath($this->fileName))) {
throw new \Nethgui\Exception\HttpException('Not Found', 404, 1324373071);
}
}
public function prepareViewXhtml(\Nethgui\View\ViewInterface $view)
{
$fragments = array(
'js' => "<script src='%URI'></script>",
'css' => "<link rel='stylesheet' type='text/css' href='%URI' />"
);
foreach (array_keys($fragments) as $ext) {
$view[$ext] = $view->spawnView($this);
$thisModule = $this;
$templateClosure = function(\Nethgui\Renderer\AbstractRenderer $renderer)
use ($ext, $thisModule, $fragments) {
$uriList = $thisModule->getUseList($ext);
$cachedFile = $thisModule->getFileName($ext);
if ($cachedFile !== FALSE) {
$uriList[] = $renderer->getModuleUrl('/Resource/' . $cachedFile);
}
$output = '';
foreach ($uriList as $uri) {
$output .= strtr($fragments[$ext], array('%URI' => $uri));
}
return $output;
};
$view[$ext]->setTemplate($templateClosure);
}
$view->setTemplate(FALSE);
}
public function prepareView(\Nethgui\View\ViewInterface $view)
{
parent::prepareView($view);
if ($view->getTargetFormat() == 'xhtml') {
$this->prepareViewXhtml($view);
} elseif ($this->fileName) {
$phpWrapper = $this->getPhpWrapper();
$filePath = $this->getCachePath($this->fileName);
$view->setTemplate(function(\Nethgui\Renderer\TemplateRenderer $renderer, $T, \Nethgui\Utility\HttpResponse $httpResponse) use ($filePath, $phpWrapper) {
$meta = array();
$content = $phpWrapper->file_get_contents_extended($filePath, $meta);
if ($meta['size'] > 0) {
$httpResponse->addHeader(sprintf('Content-Length: %d', $meta['size']));
}
if (NETHGUI_ENABLE_HTTP_CACHE_HEADERS) {
$httpResponse
->addHeader(sprintf('Last-Modified: %s', date(DATE_RFC1123, $phpWrapper->filemtime($filePath))))
->addHeader(sprintf('Expires: %s', date(DATE_RFC1123, $phpWrapper->time() + 3600)))
;
}
return $content;
});
}
}
public function getUseList($ext)
{
return $this->staticFiles->getUseList($ext);
}
private function getCode($ext)
{
return $this->staticFiles->getCode($ext);
}
protected function calcFileName($ext)
{
if ( ! $this->getCode($ext)) {
return FALSE;
}
$fileName = substr(md5(serialize($this->getCode($ext))), 0, 8) . '.' . $ext;
return $fileName;
}
protected function getCachePath($fileName = '')
{
if ( ! isset($this->cachePath)) {
$dirHash = sprintf('/nethgui-resource-cache-%s/', substr(md5($this->getPhpWrapper()->phpReadGlobalVariable('SCRIPT_FILENAME')), 0, 5));
$this->cachePath = $this->getPhpWrapper()->ini_get('session.save_path') . $dirHash;
if ( ! $this->getPhpWrapper()->file_exists($this->cachePath)) {
$this->getPhpWrapper()->mkdir($this->cachePath, 0755);
}
}
return $this->cachePath . $fileName;
}
protected function cacheWrite($fileName, $ext)
{
$resource = $this->getPhpWrapper()->fopen($this->getCachePath($fileName), 'w');
if ($resource === FALSE) {
$error = error_get_last();
if ( ! is_null($error)) {
$message = $error['message'];
} else {
$message = '';
}
throw new \UnexpectedValueException(sprintf('%s: cannot open a file in Cache directory - %s', get_class($this), $message), 1324393391);
}
foreach ($this->getCode($ext) as $part) {
if ($part['file']) {
$data = @$this->getPhpWrapper()->file_get_contents($part['file']);
if ( ! $data) {
$this->getLog()->warning(sprintf('%s: File not found, or missing data.', $part['file']));
}
} else {
$data = $part['data'];
}
$this->getPhpWrapper()->fwrite($resource, $data);
}
$this->getPhpWrapper()->fclose($resource);
}
public function getFileName($ext)
{
static $fileNames = array();
if ( ! isset($fileNames[$ext])) {
$fileNames[$ext] = $this->calcFileName($ext);
if ($fileNames[$ext] !== FALSE && ! $this->getPhpWrapper()->file_exists($fileNames[$ext])) {
$this->cacheWrite($fileNames[$ext], $ext);
}
}
return $fileNames[$ext];
}
public function setStaticFilesModel(\Nethgui\Model\StaticFiles $staticFiles)
{
$this->staticFiles = $staticFiles;
return $this;
}
public function getDependencySetters()
{
return array('StaticFiles' => array($this, 'setStaticFilesModel'));
}
}