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:
<?php
namespace Nethgui\Module;
class Main extends \Nethgui\Controller\ListComposite
{
private $moduleSet;
private $moduleId;
private $defaultModule;
protected function initializeAttributes(\Nethgui\Module\ModuleAttributesInterface $base)
{
$attributes = new SystemModuleAttributesProvider();
$attributes->initializeFromModule($this);
return $attributes;
}
public function __construct(\Nethgui\Module\ModuleSetInterface $modules, $defaultModule = '')
{
parent::__construct(FALSE);
$this->moduleSet = $modules;
$this->defaultModule = $defaultModule;
}
public function bind(\Nethgui\Controller\RequestInterface $request)
{
$idList = array_filter($request->getParameterNames(), function($p) use ($request) {
return is_array($request->getParameter($p));
});
$this->moduleId = \Nethgui\array_head($request->getPath());
if ( ! $this->moduleId) {
return;
}
try {
foreach ($idList as $moduleIdentifier) {
$moduleInstance = $this->moduleSet->getModule($moduleIdentifier);
$this->addChild($moduleInstance);
}
} catch (\Nethgui\Exception\AuthorizationException $ex) {
throw $ex;
} catch (\RuntimeException $ex) {
throw new \Nethgui\Exception\HttpException('Not found', 404, 1324379722, $ex);
}
$this->authorize($request);
parent::bind($request);
}
private function authorize(\Nethgui\Controller\RequestInterface $request)
{
foreach ($this->getChildren() as $child) {
if ($request->isMutation()) {
$auth = $this->getPolicyDecisionPoint()->authorize($request->getUser(), $child, self::ACTION_MUTATE);
} else {
$auth = $this->getPolicyDecisionPoint()->authorize($request->getUser(), $child, self::ACTION_QUERY);
}
if ($auth->isDenied()) {
throw $auth->asException(1327499272);
}
}
}
public function prepareView(\Nethgui\View\ViewInterface $view)
{
parent::prepareView($view);
if ($this->moduleId) {
$view['moduleView'] = $view[$this->moduleId];
unset($view[$this->moduleId]);
}
}
public function nextPath()
{
if ( ! $this->moduleId) {
if ($this->defaultModule) {
return $this->defaultModule;
}
throw new \Nethgui\Exception\HttpException('Not found', 404, 1324379721);
}
foreach ($this->getChildren() as $child) {
if ($child instanceof \Nethgui\Controller\RequestHandlerInterface && $child->getIdentifier() === $this->moduleId) {
return $child->nextPath();
}
}
return FALSE;
}
}