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:
<?php
namespace Nethgui\Module\Help;
class Common extends \Nethgui\Controller\AbstractController implements \Nethgui\Component\DependencyConsumer
{
private $includes = array();
private $module;
private $fileNameResolver;
private $nsMap = array();
public function getModuleSet()
{
return $this->getParent()->getModuleSet();
}
public function bind(\Nethgui\Controller\RequestInterface $request)
{
parent::bind($request);
$fileName = \Nethgui\array_head($request->getPath());
if (preg_match('/[a-z][a-z0-9]+(.rst)/i', $fileName) == 0 && preg_match('/[a-z][a-z0-9]+(.html)/i', $fileName) == 0) {
throw new \Nethgui\Exception\HttpException('Not found', 404, 1322148405);
}
if (substr($fileName, -3) == 'rst') {
$this->module = $this->getModuleSet()->getModule(substr($fileName, 0, -4));
} else {
$this->module = $this->getModuleSet()->getModule(substr($fileName, 0, -5));
}
if (is_null($this->module)) {
throw new \Nethgui\Exception\HttpException('Not found', 404, 1322148406);
}
$this->module->setPlatform($this->getPlatform());
if ( ! $this->module->isInitialized()) {
$this->module->initialize();
}
}
public function setFileNameResolver($fileNameResolver)
{
$this->fileNameResolver = $fileNameResolver;
return $this;
}
protected function getTargetModule()
{
return $this->module;
}
protected function getHelpDocumentPath(\Nethgui\Module\ModuleInterface $module)
{
$parts = explode('\\', get_class($module));
$locale = str_replace('-', '_', $this->getRequest()->getLocale());
$lang = substr($locale, 0, 2);
$fileName = implode('_', $parts) . '.html';
$nsList = array_keys($this->nsMap);
foreach ($nsList as $ns) {
$pathLocale = call_user_func($this->fileNameResolver, implode("\\", array($ns, 'Help', $locale, $fileName)));
if($this->getPhpWrapper()->file_exists($pathLocale)) {
return $pathLocale;
}
$pathLang = call_user_func($this->fileNameResolver, implode("\\", array($ns, 'Help', $lang, $fileName)));
if($this->getPhpWrapper()->file_exists($pathLang)) {
return $pathLang;
}
}
return call_user_func($this->fileNameResolver, implode("\\", array($ns, 'Help', 'en', $fileName)));
}
protected function getFileNameResolver()
{
return $this->fileNameResolver;
}
protected function readHelpDocument($filePath)
{
$document = new \XMLReader();
set_error_handler(function ($errno, $errstr) {
}, E_WARNING | E_NOTICE);
if ($document->open('file://' . $filePath, 'utf-8', LIBXML_NOENT) === TRUE) {
while ($document->name != 'body' && $document->read());
while ($document->name != 'div' && $document->read());
$content = $document->readInnerXml();
} else {
$content = 'Not found';
throw new \Nethgui\Exception\HttpException(sprintf("%s: resource not found", __CLASS__), 404, 1333119424);
}
restore_error_handler();
return $this->expandIncludes($content);
}
public function expandIncludes($contents)
{
$self = $this;
return preg_replace_callback(
'/{{{INCLUDE\s+([^}\s]+)}}}/', function($matches) use ($self, $contents) {
return $self->readHelpDocumentsByPattern($matches[1], $contents);
}, $contents);
}
public function readHelpDocumentsByPattern($pattern)
{
if (strstr($pattern, '/') !== FALSE) {
throw new \UnexpectedValueException(sprintf('%s: Forbidden slash "/" character in INCLUDE pattern', __CLASS__), 1338288914);
}
$absolutePattern = dirname($this->getHelpDocumentPath($this->getTargetModule())) . '/' . $pattern;
$expansion = '';
foreach ($this->getPhpWrapper()->glob($absolutePattern) as $fileName) {
if (substr($fileName, -4) !== '.rst' && substr($fileName, -5) !== '.html') {
throw new \UnexpectedValueException(sprintf('%s: Forbidden file name extension in help document `%s`.', __CLASS__, basename($fileName)), 1338288817);
}
if (isset($this->includes[$fileName])) {
throw new \RuntimeException(sprintf('%s: the file has already been included: `%s`.', __CLASS__, basename($fileName)), 1338289668);
}
$this->includes[$fileName] = TRUE;
$expansion .= $this->readHelpDocument($fileName);
}
return $expansion;
}
public function setNamespaceMap($nsMap) {
$this->nsMap = $nsMap;
return $this;
}
public function getDependencySetters()
{
return array(
'namespaceMap' => array($this, 'setNamespaceMap')
);
}
}