/** * Constructor. Please use only getInstance to retrieve the single instance. * * @see Context#getInstance */ private function __construct() { $this->request = new Request(); $this->response = new Response(); $this->viewData = array(); $this->behaviors = array(); if (PHP_SAPI != 'cli') { $resolver = new Resolver($this->request); //$this->extensionName = $resolver->getExtensionFromURL(); $this->moduleName = Helpers\Camelizer::firstToUpper($resolver->getModule()); $this->actionName = Helpers\Camelizer::firstToLower($resolver->getAction()); } }
public function execute() { $module = $this->context->getModuleName(); $action = $this->context->getActionName(); // if module exist include the class if ($module !== null) { $exptectedPath = DIR_APP . DIR_ACTIONS . $module . '.php'; if (file_exists($exptectedPath)) { require_once $exptectedPath; } else { throw new ActionEnforcingException("Module '" . Helpers\Camelizer::firstToUpper($module) . "' does not exist in {$exptectedPath}.", $this->context->getModuleName(), $this->context->getActionName()); } if (defined('ROOT_PATH')) { $root = realpath(ROOT_PATH); } else { $root = realpath($_SERVER['DOCUMENT_ROOT']); } if (preg_match("/^\\//", $root) && !preg_match("/\\/\$/", $root)) { $root .= '/'; } else { if (!preg_match("/\\\$/", $root)) { $root .= '\\'; } } $relPath = str_replace($root, '', realpath(dirname($exptectedPath))); $relPath = str_replace('/', '\\', $relPath); $className = $relPath . '\\' . $module; if (!class_exists($className)) { throw new ActionEnforcingException("Unable to load {$className} in {$exptectedPath}", $this->context->getModuleName(), $this->context->getActionName()); } // File gracefully loaded. $this->context->setModuleName($module); $this->context->setActionName($action); $moduleInstance = new $className(); } else { throw new ActionEnforcingException("No Module file matching requested module.", $this->context->getModuleName(), $this->context->getActionName()); } // if the method related to the specified action exists, call it if (method_exists($moduleInstance, $action)) { // search parameters method $reflect = new \ReflectionMethod($className, $action); $parameters = $reflect->getParameters(); $tabParam = array(); foreach ($parameters as $param) { $tabParam[$param->getName()] = $this->context->getRequest()->getParameter($param->getName()); } // Action method is invoked, passing request parameters as // method parameters. Common\Logger::t('Invoking ' . get_class($moduleInstance) . '::' . $action, array('GENERIS', 'CLEARRFW')); call_user_func_array(array($moduleInstance, $action), $tabParam); // Render the view if selected. if ($view = $moduleInstance->hasView()) { $renderer = $moduleInstance->getRenderer(); $renderer->setTemplateDir(DIR_APP . DIR_VIEWS); $renderer->setAssetsUrl(ROOT_URL . DIR_ASSETS); echo $renderer->render(); } } else { throw new ActionEnforcingException("Unable to find the appropriate action for Module '{$module}'.", $this->context->getModuleName(), $this->context->getActionName()); } }