예제 #1
0
 /**
  * conveniance method to create urls based on the current MVC context and
  * it for the used kind of url resolving
  *
  * @access public
  * @author Jerome Bogaerts, <*****@*****.**>
  * @param  string action
  * @param  string module
  * @param  string extension
  * @param  array params
  * @return string
  */
 public static function url($action = null, $module = null, $extension = null, $params = array())
 {
     $returnValue = (string) '';
     // section 127-0-1-1-4955a5a0:1242e3739c6:-8000:0000000000001A26 begin
     $context = Context::getInstance();
     if (is_null($module)) {
         $module = $context->getModuleName();
     }
     if (is_null($action)) {
         $action = $context->getActionName();
     }
     if (is_null($extension) || empty($extension)) {
         $returnValue = self::getBaseUrl() . $module . '/' . $action;
     } else {
         $returnValue = self::getRootUrl() . $extension . '/' . $module . '/' . $action;
     }
     if (count($params) > 0) {
         $returnValue .= '?';
         if (is_string($params)) {
             $returnValue .= $params;
         }
         if (is_array($params)) {
             foreach ($params as $key => $value) {
                 $returnValue .= $key . '=' . urlencode($value) . '&';
             }
             $returnValue = substr($returnValue, 0, -1);
         }
     }
     // section 127-0-1-1-4955a5a0:1242e3739c6:-8000:0000000000001A26 end
     return (string) $returnValue;
 }
 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());
     }
 }