示例#1
0
 public function testCmsExceptionPhp()
 {
     $theme = Theme::load('test');
     $router = new Router($theme);
     $page = $router->findByUrl('/throw-php');
     $foreignException = new \Symfony\Component\Debug\Exception\FatalErrorException('This is a general error', 100, 1, 'test.php', 20);
     $this->setProtectedProperty($foreignException, 'file', "/modules/cms/classes/CodeParser.php(165) : eval()'d code line 7");
     $exception = new CmsException($page, 300);
     $exception->setMask($foreignException);
     $this->assertEquals($page->getFilePath(), $exception->getFile());
     $this->assertEquals('PHP Content', $exception->getErrorType());
     $this->assertEquals('This is a general error', $exception->getMessage());
 }
示例#2
0
 public function injectPageTwig($page, $loader, $twig)
 {
     if (!isset($page->apiBag['staticPage'])) {
         return;
     }
     $staticPage = $page->apiBag['staticPage'];
     CmsException::mask($staticPage, 400);
     $loader->setObject($staticPage);
     $template = $twig->loadTemplate($staticPage->getFilePath());
     $template->render([]);
     CmsException::unmask();
 }
示例#3
0
 /**
  * Loads the object from a file.
  * @param \Cms\Classes\Theme $theme Specifies the theme the object belongs to.
  * @param string $fileName Specifies the file name, with the extension.
  * The file name can contain only alphanumeric symbols, dashes and dots.
  * @return boolean Returns true if the object was successfully loaded. Otherwise returns false.
  */
 public static function load($theme, $fileName)
 {
     if (($obj = parent::load($theme, $fileName)) === null) {
         return null;
     }
     CmsException::mask($obj, 200);
     $parsedData = SectionParser::parse($obj->content);
     CmsException::unmask();
     $obj->settings = $parsedData['settings'];
     $obj->code = $parsedData['code'];
     $obj->markup = $parsedData['markup'];
     $obj->parseComponentSettings();
     $obj->parseSettings();
     return $obj;
 }
示例#4
0
 /**
  * Renders a requested partial.
  * The framework uses this method internally.
  * @param string $partial The view to load.
  * @param array $parameters Parameter variables to pass to the view.
  * @param bool $throwException Throw an exception if the partial is not found.
  * @return mixed Partial contents or false if not throwing an exception.
  */
 public function renderPartial($name, $parameters = [], $throwException = true)
 {
     $vars = $this->vars;
     /*
      * Alias @ symbol for ::
      */
     if (substr($name, 0, 1) == '@') {
         $name = '::' . substr($name, 1);
     }
     /*
      * Process Component partial
      */
     if (strpos($name, '::') !== false) {
         list($componentAlias, $partialName) = explode('::', $name);
         /*
          * Component alias not supplied
          */
         if (!strlen($componentAlias)) {
             if ($this->componentContext !== null) {
                 $componentObj = $this->componentContext;
             } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) {
                 if ($throwException) {
                     throw new CmsException(Lang::get('cms::lang.partial.not_found_name', ['name' => $partialName]));
                 } else {
                     return false;
                 }
             }
             /*
              * Component alias is supplied
              */
         } else {
             if (($componentObj = $this->findComponentByName($componentAlias)) === null) {
                 if ($throwException) {
                     throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $componentAlias]));
                 } else {
                     return false;
                 }
             }
         }
         $partial = null;
         $this->componentContext = $componentObj;
         /*
          * Check if the theme has an override
          */
         if (strpos($partialName, '/') === false) {
             $overrideName = $componentObj->alias . '/' . $partialName;
             $partial = Partial::loadCached($this->theme, $overrideName);
         }
         /*
          * Check the component partial
          */
         if ($partial === null) {
             $partial = ComponentPartial::loadCached($componentObj, $partialName);
         }
         if ($partial === null) {
             if ($throwException) {
                 throw new CmsException(Lang::get('cms::lang.partial.not_found_name', ['name' => $name]));
             } else {
                 return false;
             }
         }
         /*
          * Set context for self access
          */
         $this->vars['__SELF__'] = $componentObj;
     } else {
         /*
          * Process theme partial
          */
         if (($partial = Partial::loadCached($this->theme, $name)) === null) {
             if ($throwException) {
                 throw new CmsException(Lang::get('cms::lang.partial.not_found_name', ['name' => $name]));
             } else {
                 return false;
             }
         }
     }
     /*
      * Run functions for CMS partials only (Cms\Classes\Partial)
      */
     if ($partial instanceof Partial) {
         $this->partialStack->stackPartial();
         $manager = ComponentManager::instance();
         foreach ($partial->settings['components'] as $component => $properties) {
             // Do not inject the viewBag component to the environment.
             // Not sure if they're needed there by the requirements,
             // but there were problems with array-typed properties used by Static Pages
             // snippets and setComponentPropertiesFromParams(). --ab
             if ($component == 'viewBag') {
                 continue;
             }
             list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : [$component, $component];
             if (!($componentObj = $manager->makeComponent($name, $this->pageObj, $properties))) {
                 throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $name]));
             }
             $componentObj->alias = $alias;
             $parameters[$alias] = $partial->components[$alias] = $componentObj;
             $this->partialStack->addComponent($alias, $componentObj);
             $this->setComponentPropertiesFromParams($componentObj, $parameters);
             $componentObj->init();
         }
         CmsException::mask($this->page, 300);
         $parser = new CodeParser($partial);
         $partialObj = $parser->source($this->page, $this->layout, $this);
         CmsException::unmask();
         CmsException::mask($partial, 300);
         $partialObj->onStart();
         $partial->runComponents();
         $partialObj->onEnd();
         CmsException::unmask();
     }
     /*
      * Render the partial
      */
     CmsException::mask($partial, 400);
     $this->loader->setObject($partial);
     $template = $this->twig->loadTemplate($partial->getFullPath());
     $result = $template->render(array_merge($this->vars, $parameters));
     CmsException::unmask();
     if ($partial instanceof Partial) {
         $this->partialStack->unstackPartial();
     }
     $this->vars = $vars;
     return $result;
 }
示例#5
0
 /**
  * Renders a requested partial.
  * The framework uses this method internally.
  * @param string $partial The view to load.
  * @param array $params Parameter variables to pass to the view.
  * @param bool $throwException Throw an exception if the partial is not found.
  * @return mixed Partial contents or false if not throwing an exception.
  */
 public function renderPartial($name, $parameters = [], $throwException = true)
 {
     /*
      * Alias @ symbol for ::
      */
     if (substr($name, 0, 1) == '@') {
         $name = '::' . substr($name, 1);
     }
     /*
      * Process Component partial
      */
     if (strpos($name, '::') !== false) {
         list($componentAlias, $partialName) = explode('::', $name);
         /*
          * Component alias not supplied
          */
         if (!strlen($componentAlias)) {
             if ($this->componentContext !== null) {
                 $componentObj = $this->componentContext;
             } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) {
                 if ($throwException) {
                     throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name]));
                 } else {
                     return false;
                 }
             }
         } else {
             if (($componentObj = $this->findComponentByName($componentAlias)) === null) {
                 if ($throwException) {
                     throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $componentAlias]));
                 } else {
                     return false;
                 }
             }
         }
         $partial = null;
         $this->componentContext = $componentObj;
         /*
          * Check if the theme has an override
          */
         if (strpos($partialName, '/') === false) {
             $overrideName = $componentObj->alias . '/' . $partialName;
             $partial = Partial::loadCached($this->theme, $overrideName);
         }
         /*
          * Check the component partial
          */
         if ($partial === null) {
             $partial = ComponentPartial::loadCached($componentObj, $partialName);
         }
         if ($partial === null) {
             if ($throwException) {
                 throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name]));
             } else {
                 return false;
             }
         }
         /*
          * Set context for self access
          */
         $this->vars['__SELF__'] = $componentObj;
     } else {
         /*
          * Process theme partial
          */
         if (($partial = Partial::loadCached($this->theme, $name)) === null) {
             if ($throwException) {
                 throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name]));
             } else {
                 return false;
             }
         }
     }
     CmsException::mask($partial, 400);
     $this->loader->setObject($partial);
     $template = $this->twig->loadTemplate($partial->getFullPath());
     $result = $template->render(array_merge($this->vars, $parameters));
     CmsException::unmask();
     $this->componentContext = null;
     return $result;
 }
示例#6
0
 /**
  * Executes the page life cycle.
  * Creates an object from the PHP sections of the page and
  * it's layout, then executes their life cycle functions.
  */
 protected function execPageCycle()
 {
     /*
      * Extensibility
      */
     if ($event = Event::fire('cms.page.start', [$this], true)) {
         return $event;
     }
     if ($event = $this->fireEvent('page.start', [$this], true)) {
         return $event;
     }
     /*
      * Run layout functions
      */
     if ($this->layoutObj) {
         $response = CmsException::capture($this->layout, 300, function () {
             return ($result = $this->layoutObj->onStart()) || ($result = $this->layout->runComponents()) || ($result = $this->layoutObj->onBeforePageStart()) ? $result : null;
         });
         if ($response) {
             return $response;
         }
     }
     /*
      * Run page functions
      */
     $response = CmsException::capture($this->page, 300, function () {
         return ($result = $this->pageObj->onStart()) || ($result = $this->page->runComponents()) || ($result = $this->pageObj->onEnd()) ? $result : null;
     });
     if ($response) {
         return $response;
     }
     /*
      * Run remaining layout functions
      */
     if ($this->layoutObj) {
         $response = CmsException::capture($this->layout, 300, function () {
             return ($result = $this->layoutObj->onEnd()) ? $result : null;
         });
     }
     /*
      * Extensibility
      */
     if ($event = Event::fire('cms.page.end', [$this], true)) {
         return $event;
     }
     if ($event = $this->fireEvent('page.end', [$this], true)) {
         return $event;
     }
     return $response;
 }
 /**
  * If the model is loaded with an invalid INI section, the invalid content will be
  * passed as a special attribute. Look for it, then locate the failure reason.
  * @return void
  */
 protected function validateSettings()
 {
     if (isset($this->attributes[SectionParser::ERROR_INI])) {
         CmsException::mask($this, 200);
         Ini::parse($this->attributes[SectionParser::ERROR_INI]);
         CmsException::unmask();
     }
 }
示例#8
0
 /**
  * Renders a requested partial.
  * The framework uses this method internally.
  * @param string $partial The view to load.
  * @param array $params Parameter variables to pass to the view.
  * @param bool $throwException Throw an exception if the partial is not found.
  * @return mixed Partial contents or false if not throwing an exception.
  */
 public function renderPartial($name, $parameters = [], $throwException = true)
 {
     $vars = $this->vars;
     /*
      * Alias @ symbol for ::
      */
     if (substr($name, 0, 1) == '@') {
         $name = '::' . substr($name, 1);
     }
     /*
      * Process Component partial
      */
     if (strpos($name, '::') !== false) {
         list($componentAlias, $partialName) = explode('::', $name);
         /*
          * Component alias not supplied
          */
         if (!strlen($componentAlias)) {
             if ($this->componentContext !== null) {
                 $componentObj = $this->componentContext;
             } elseif (($componentObj = $this->findComponentByPartial($partialName)) === null) {
                 if ($throwException) {
                     throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name]));
                 } else {
                     return false;
                 }
             }
             /*
              * Component alias is supplied
              */
         } else {
             if (($componentObj = $this->findComponentByName($componentAlias)) === null) {
                 if ($throwException) {
                     throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $componentAlias]));
                 } else {
                     return false;
                 }
             }
         }
         $partial = null;
         $this->componentContext = $componentObj;
         /*
          * Check if the theme has an override
          */
         if (strpos($partialName, '/') === false) {
             $overrideName = $componentObj->alias . '/' . $partialName;
             $partial = Partial::loadCached($this->theme, $overrideName);
         }
         /*
          * Check the component partial
          */
         if ($partial === null) {
             $partial = ComponentPartial::loadCached($componentObj, $partialName);
         }
         if ($partial === null) {
             if ($throwException) {
                 throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name]));
             } else {
                 return false;
             }
         }
         /*
          * Set context for self access
          */
         $this->vars['__SELF__'] = $componentObj;
     } else {
         /*
          * Process theme partial
          */
         if (($partial = Partial::loadCached($this->theme, $name)) === null) {
             if ($throwException) {
                 throw new CmsException(Lang::get('cms::lang.partial.not_found', ['name' => $name]));
             } else {
                 return false;
             }
         }
     }
     /*
      * Run functions for CMS partials only (Cms\Classes\Partial)
      */
     if ($partial instanceof Partial) {
         $manager = ComponentManager::instance();
         foreach ($partial->settings['components'] as $component => $properties) {
             list($name, $alias) = strpos($component, ' ') ? explode(' ', $component) : [$component, $component];
             if (!($componentObj = $manager->makeComponent($name, $this->pageObj, $properties))) {
                 throw new CmsException(Lang::get('cms::lang.component.not_found', ['name' => $name]));
             }
             $componentObj->alias = $alias;
             $parameters[$alias] = $partial->components[$alias] = $componentObj;
             array_push($this->partialComponentStack, ['name' => $alias, 'obj' => $componentObj]);
             $this->setComponentPropertiesFromParameters($componentObj, $parameters);
             $componentObj->init();
             $componentObj->onInit();
             // Deprecated: Remove ithis line if year >= 2015
         }
         CmsException::mask($this->page, 300);
         $parser = new CodeParser($partial);
         $partialObj = $parser->source($this->page, $this->layout, $this);
         CmsException::unmask();
         CmsException::mask($partial, 300);
         $partialObj->onStart();
         $partial->runComponents();
         $partialObj->onEnd();
         CmsException::unmask();
     }
     /*
      * Render the parital
      */
     CmsException::mask($partial, 400);
     $this->loader->setObject($partial);
     $template = $this->twig->loadTemplate($partial->getFullPath());
     $result = $template->render(array_merge($this->vars, $parameters));
     CmsException::unmask();
     if ($partial instanceof Partial) {
         if ($this->partialComponentStack) {
             array_pop($this->partialComponentStack);
         }
     }
     $this->vars = $vars;
     $this->componentContext = null;
     return $result;
 }