/** * <pre> * Instanciates the Spirit * Calls the methodName method with the given args * render the view of that Method * returns the output as string * </pre> * @note SpiritController* is reused if already instantiated once. * @note e.g. one SpiritController is not instanciated more than once * @warning Throws Exception if Tried to Call non Existing method * @warning If View for that Specific method is not found general View of that Controler will be Called * @param string $spiritName * @param string $methodName='main' * @param array $args=array() */ public function run($spiritName, $methodName = 'main', $args = array()) { $controller = $this->executeLogic($spiritName, $methodName, $args); $this->storeXDO($controller->xdo); //{ Make Controller properties Accessible through View $meta = $controller->meta; $xdo = $controller->xdo; $data = $controller->data; //} $controller->serialize(); //{ Run the Coordinator $params = null; $coordinatorPath = $this->coordinator($spiritName); if (file_exists($coordinatorPath)) { require $coordinatorPath; $coordinatorClassName = ucfirst($spiritName) . 'Coordinator'; if (class_exists($coordinatorClassName)) { $params = new $coordinatorClassName(); $coordinatorReflection = new ReflectionObject($params); try { $method = $coordinatorReflection->getMethod($methodName); $method->invoke($params); //TODO No Arguments ? Think Again } catch (ReflectionException $ex) { //TODO Call the dafult method assert("/*Cannot get method {$coordinatorClassName}::{$methodName}*/"); } } else { assert("/*No Class {$coordinatorClassName} found*/"); } } else { //Coordinator is Optional } //} ob_start(); $__viewName = $this->view($spiritName, $methodName); $scope = function () use($__viewName, $controller, $data, $xdo, $meta) { require $__viewName; }; $scope(); $this->viewContents = ob_get_contents(); ob_end_clean(); if (ControllerTray::instance()->bongParsing) { $parser = new \SuSAX\Parser(new BongParser(function ($spiritName, $methodName, $arguments, $tagName, $instanceId = null) use($controller) { switch ($tagName) { case 'spirit': return !$instanceId ? $controller->spirit($spiritName)->call($methodName, $arguments) : $controller->spirit($spiritName)->instance($instanceId)->call($methodName, $arguments); break; case 'embed': return !$instanceId ? $controller->embed($spiritName)->call($methodName, $arguments) : $controller->embed($spiritName)->instance($instanceId)->call($methodName, $arguments); break; } })); $parser->setNsFocus('bong'); $parser->setText($this->viewContents); $this->spiritInfo = new stdClass(); $this->spiritInfo->name = $spiritName; $this->spiritInfo->method = $methodName; $this->viewContents = $parser->parse(); } if (ControllerTray::instance()->renderLayout) { ob_start(); require $this->layout($spiritName, $methodName); $this->responseBuffer = ob_get_contents(); ob_end_clean(); } else { $this->responseBuffer = ControllerTray::instance()->trim ? trim($this->viewContents) : $this->viewContents; } //echo $this->viewContents; //echo $this->responseBuffer; }
/** * \algorithm * layoutPath: * projectLayout = Path::currentProject(apps.layout.+controller.@layout.php) * if exists(projectLayout) * return projectLayout * else return default Layout * * paramPath: * projectParam = Path::currentProject(apps.layout.+controller.@param.php) * if exists(projectParam) * return projectParam * else return default param * * viewPath: * return Path::currentProject(apps.layout.+controller.-method.@view.php) * * controllerPath: * return Path::currentProject(apps.+controller) * * processView: * require(viewPath()) * * main: * require(paramPath()) * require(controllerPath()) * startOutputBuffering() * $controller = new Controller(); * closure syncVars(&$controller){ * foreach($controller->data as $key => &$value){ * local $key = $value; * } * processView() * } * $this->viewContents = getOutputBufferContents() * endOutputBuffering() * startOutputBuffering() * require(layoutPath()) * $response = getOutputBufferContents() * endOutputBuffering() * * \endalgorithm */ public function run() { $controller = $this->executeLogic(); //$this->storeXDO($controller->xdo); //{ Make Variables Accessible to View $data = $controller->data; if (isset($controller->xdo)) { $xdo = $controller->xdo; } if (isset($controller->session)) { $session = $controller->session; } $meta = $controller->meta; //} //{ Load The View if (ControllerTray::instance()->renderView) { if (isset($this->_methodReturn) && is_int($this->_methodReturn) && $this->_methodReturn == -1) { return; } $__viewName = $this->view(); ob_start(); $scope = function () use($__viewName, $controller, $data, $session, $xdo, $meta) { require $__viewName; }; $scope(); $this->viewContents = ob_get_contents(); ob_end_clean(); if (ControllerTray::instance()->bongParsing) { $parser = new \SuSAX\Parser(new BongParser(function ($spiritName, $methodName, $arguments, $tagName, $instanceId = null) use($controller) { switch ($tagName) { case 'spirit': return !$instanceId ? $controller->spirit($spiritName)->call($methodName, $arguments) : $controller->spirit($spiritName)->instance($instanceId)->call($methodName, $arguments); break; } })); $parser->setNsFocus('bong'); $parser->setText($this->viewContents); $this->viewContents = $parser->parse(); } if ($this->_systemView) { $controller->dumpStrap(); } } //} if (ControllerTray::instance()->renderView && ControllerTray::instance()->renderLayout) { /// < Layout cannot be rendered If no View is rendered $this->mergeParams(); $params = $this->_arrangedParams; ob_start(); require $this->layout(); $this->responseBuffer = ob_get_contents(); ob_end_clean(); } else { if (ControllerTray::instance()->renderView) { $this->responseBuffer = ControllerTray::instance()->trim ? trim($this->viewContents) : $this->viewContents; } else { if (isset($this->_methodReturn)) { switch (ControllerTray::instance()->responseType) { case 'scrap/xml': http::contentType('text/xml'); $packer = new XMLPacker($this->_methodReturn); $this->responseBuffer = $packer->toXML()->saveXML(); break; case 'scrap/json': http::contentType('application/json'); $this->responseBuffer = json_encode($this->_methodReturn); break; case 'scrap/plain': default: http::contentType('text/plain'); $this->responseBuffer = $this->_methodReturn; } } } } if (ControllerTray::instance()->xsltView) { $this->processXSLView($controller->storage()); } //echo $this->responseBuffer; }