Example #1
0
 /**
  * Rendering a layout. It also renders the views that the layout contains.
  * @throws AiryException
  */
 public function render()
 {
     //To get the layout file
     $layoutContent = file_get_contents($this->_layoutPath);
     $this->prepareContent($layoutContent);
     //Fetch each views
     $viewContents = array();
     foreach ($this->_layout as $contentKey => $viewComponent) {
         //check if it is an array that contains module controller action
         if (is_array($viewComponent)) {
             $moduleName = MvcReg::getModuleName();
             $moduleName = isset($viewComponent[self::MODULE]) ? $viewComponent[self::MODULE] : $moduleName;
             try {
                 if (isset($viewComponent[self::CONTROLLER])) {
                     $controllerName = $viewComponent[self::CONTROLLER];
                 } else {
                     throw new AiryException('Layout is missing controller');
                 }
                 if (isset($viewComponent[self::ACTION])) {
                     $actionName = $viewComponent[self::ACTION];
                 } else {
                     throw new AiryException('Layout is missing controller');
                 }
                 $paramString = "";
                 if (isset($viewComponent[self::PARAMS])) {
                     $paramString = $this->getParamString($viewComponent[self::PARAMS]);
                 }
                 $HttpServerHost = PathService::getAbsoluteHostURL();
                 $config = Config::getInstance();
                 $LeadingUrl = $HttpServerHost . "/" . $config->getLeadFileName();
                 $mvcKeywords = $config->getMVCKeyword();
                 $moduleKey = $mvcKeywords['module'];
                 $controllerKey = $mvcKeywords['controller'];
                 $actionKey = $mvcKeywords['action'];
                 $moduleName = RouterHelper::hyphenToCamelCase($moduleName);
                 $controllerName = RouterHelper::hyphenToCamelCase($controllerName, TRUE);
                 $actionName = RouterHelper::hyphenToCamelCase($actionName);
                 $actionPath = $moduleKey . "=" . $moduleName . "&" . $controllerKey . "=" . $controllerName . "&" . $actionKey . "=" . $actionName;
                 $url = $LeadingUrl . "?" . $actionPath . $paramString;
                 //check if it is already signed in
                 //if yes, add current action into allow action query string
                 if (Authentication::isLogin($moduleName)) {
                     $filename = "mca_file" . microtime(true);
                     $md5Filename = md5($filename);
                     $content = $moduleName . ";" . $controllerName . ";" . $actionName;
                     $content = md5($content);
                     FileCache::saveFile($md5Filename, $content);
                     $url = $url . '&' . self::ALLOW_THIS_ACTION . '=' . $md5Filename;
                 }
                 $viewContent = $this->getData($url);
                 $viewContents[$contentKey] = $viewContent;
             } catch (Exception $e) {
                 $errorMsg = sprintf("View Exception: %s", $e->getMessage());
                 throw new AiryException($errorMsg);
             }
         } else {
             if ($viewComponent instanceof AppView) {
                 //Use $this->_view->render();
                 $viewComponent->setInLayout(true);
                 $viewContent = $viewComponent->render();
                 $viewContents[$contentKey] = $viewContent;
             } else {
                 $viewContent = file_get_contents($viewComponent);
                 $viewContent = Language::getInstance()->replaceWordByKey($viewContent);
                 $viewContents[$contentKey] = $viewContent;
             }
         }
     }
     /**
      * Deal with layout variables 
      */
     if (!is_null($this->_variables)) {
         foreach ($this->_variables as $name => $value) {
             if ($value instanceof UIComponent || $value instanceof JUIComponent) {
                 $htmlValue = $value->render();
                 $newHtmlValue = Language::getInstance()->replaceWordByKey($htmlValue);
                 ${$name} = $newHtmlValue;
             } else {
                 ${$name} = $value;
             }
         }
     }
     //Loop through each contents
     //Replace view components with keywords
     $layoutContent = $this->composeContent($layoutContent, $viewContents);
     //Check if inserting doctype at the beginning of the view content
     if (!$this->_noDoctype) {
         if (is_null($this->_doctype)) {
             $this->setDoctype();
         }
     }
     $layoutContent = $this->_doctype . $layoutContent;
     //Stream output
     $existed = in_array("airy.layout", stream_get_wrappers());
     if ($existed) {
         stream_wrapper_unregister("airy.layout");
     }
     stream_wrapper_register('airy.layout', 'StreamHelper');
     $fp = fopen("airy.layout://layout_content", "r+");
     fwrite($fp, $layoutContent);
     fclose($fp);
     include "airy.layout://layout_content";
 }
Example #2
0
 /**
  * This method render the view.
  *
  * @throws Exception 
  * @var  $httpServerHost absolute host url.
  * @var  $serverHost absolute host path.
  * @var  $ABSOLUTE_URL absolute host url.
  * @var  $SERVER_HOST absolute host path.
  * @var  $LEAD_FILENAME leading filename ex: index.php.
  * 
  */
 public function render()
 {
     try {
         if (!is_null($this->_viewFilePath) && file_exists($this->_viewFilePath)) {
             if (!is_null($this->_variables)) {
                 foreach ($this->_variables as $name => $value) {
                     if ($value instanceof UIComponent || $value instanceof JsUIComponentInterface) {
                         $htmlValue = $value->render();
                         $newHtmlValue = $this->_languageService->replaceWordByKey($htmlValue);
                         ${$name} = $newHtmlValue;
                         $this->hasAnyScript($newHtmlValue);
                     } else {
                         ${$name} = $value;
                     }
                 }
             }
             $httpServerHost = PathService::getAbsoluteHostURL();
             $serverHost = PathService::getAbsoluteHostPath();
             $ABSOLUTE_URL = PathService::getAbsoluteHostURL();
             $SERVER_HOST = PathService::getAbsoluteHostPath();
             $LEAD_FILENAME = Config::getInstance()->getLeadFileName();
             $viewContent = file_get_contents($this->_viewFilePath);
             $this->hasAnyScript($viewContent);
             //hasScript check if there is a need for javascript library to be added in
             //If there is no javascript UI component, but setScript == true, we still
             //add plugins. Otherwise, we simply do not add any libraries.
             if ($this->_hasScript) {
                 //add plugins
                 $viewContent = $this->addPluginLib($viewContent);
             }
             //$viewContent = $this->_languageService->replaceWordByKey($viewContent);
             //Check if inserting doctype at the beginning of the view content
             if (!$this->_inLayout) {
                 if (!$this->_noDoctype) {
                     if (is_null($this->_doctype)) {
                         $this->setDoctype();
                     }
                     $viewContent = $this->_doctype . $viewContent;
                 }
             }
             $fp = fopen("airy.view://view_content", "r+");
             fwrite($fp, $viewContent);
             fclose($fp);
             if (!$this->_inLayout) {
                 //use ob_start to push all include files into a buffer
                 //and then call the callback function replaceLanguageWords
                 //only use stream writter cannot fulfill this
                 ob_start(array($this, 'replaceLanguageWords'));
                 include "airy.view://view_content";
                 ob_end_flush();
             } else {
                 $this->_viewScripts = file_get_contents("airy.view://view_content");
                 return $this->_viewScripts;
             }
         } else {
             throw new Exception("No View File {$this->_viewFilePath} Existed!");
         }
     } catch (Exception $e) {
         echo 'Exception: ', $e->getMessage(), "\n";
     }
 }