energine
[ class tree: energine ] [ index: energine ] [ all elements ]

Source for file ComponentManager.class.php

Documentation is available at ComponentManager.class.php

  1. <?php
  2.  
  3. /**
  4.  * Класс ComponentManager.
  5.  *
  6.  * @package energine
  7.  * @subpackage core
  8.  * @author dr.Pavka
  9.  * @copyright Energine 2006
  10.  * @version $Id$
  11.  */
  12.  
  13.  
  14. /**
  15.  * Менеджер набора компонентов документа.
  16.  *
  17.  * @package energine
  18.  * @subpackage core
  19.  * @author dr.Pavka
  20.  * @final
  21.  */
  22. final class ComponentManager extends Object {
  23.  
  24.     /**
  25.      * @access private
  26.      * @var array набор компонентов
  27.      */
  28.     private $components = array();
  29.  
  30.     /**
  31.      * @access private
  32.      * @var Document документ
  33.      */
  34.     private $document;
  35.  
  36.     /**
  37.      * Конструктор класса.
  38.      *
  39.      * @access public
  40.      * @param Document $document 
  41.      * @return void 
  42.      */
  43.     public function __construct(Document $document{
  44.         parent::__construct();
  45.  
  46.         $this->document = $document;
  47.     }
  48.  
  49.  
  50.     /**
  51.      * Добавляет компонент.
  52.      *
  53.      * @access public
  54.      * @param Component $component 
  55.      * @param string имя файла шаблона в котором находится компонент
  56.      * @return void 
  57.      */
  58.     public function addComponent(Component $component$fileName false{
  59.         $this->components[$component->getName()array(
  60.         'component' => $component,
  61.         'file' => $fileName
  62.         );
  63.     }
  64.  
  65.     /**
  66.      * Возвращает компонент с указанным именем.
  67.      *
  68.      * @access public
  69.      * @param string $name имя компонента
  70.      * @return Component 
  71.      */
  72.     public function getComponentByName($name{
  73.         $result false;
  74.         if (isset($this->components[$name])) {
  75.             $result $this->components[$name]['component'];
  76.         }
  77.         return $result;
  78.     }
  79.  
  80.     /**
  81.      * Возвращает набор компонентов по имени класса.
  82.      *
  83.      * @access public
  84.      * @param string $className имя класса
  85.      * @return array 
  86.      */
  87.     public function getComponentsByClassName($className{
  88.         $result array();
  89.         foreach ($this->components as $componentName => $component{
  90.             if (get_class($component['component']== $className{
  91.                 $result[$componentName$component['component'];
  92.             }
  93.         }
  94.  
  95.         return $result;
  96.     }
  97.  
  98.     /**
  99.      * Загружает описания компонентов из файла шаблона(layout или content)
  100.      *
  101.      * @param string имя файла content'а или layout'а
  102.      * @param string имя компонента который нужно загрузить
  103.      * @return bool Возвращает флаг указівающий на то загружены ли компоненты
  104.      * @access public
  105.      */
  106.  
  107.     public function loadComponentsFromFile($fileName$onlyComponent false{
  108.         $result false;
  109.         //проверяем существует ли такой файл
  110.         if (!file_exists($fileName)) {
  111.             throw new SystemException('ERR_DEV_NO_TEMPLATE_FILE'SystemException::ERR_CRITICAL$fileName);
  112.         }
  113.         //и можно ли из него загрузить данные
  114.         if (!($file simplexml_load_file($fileName))) {
  115.             throw new SystemException('ERR_DEV_BAD_TEMPLATE_FILE'SystemException::ERR_CRITICAL$fileName);
  116.         }
  117.         if ($onlyComponent{
  118.             $components $file->xpath("/*/component[@name='".$onlyComponent."']");
  119.         }
  120.         else {
  121.             $components $file->children();
  122.             //$components = $file->xpath('/*/component');
  123.         }
  124.         if (!empty($components)) {
  125.             $result true;
  126.             foreach ($components as $componentDescription{
  127.                 $this->addComponent($this->createComponentFromXML($componentDescription)$fileName);
  128.             }
  129.         }
  130.  
  131.         return $result;
  132.  
  133.     }
  134.  
  135.     /**
  136.      * Создание компонента из XML описания
  137.      *
  138.      * @param SimpleXMLElement описание компонента
  139.      * @return Component 
  140.      * @access public
  141.      */
  142.  
  143.     public function createComponentFromXML(SimpleXMLElement $componentDescription{
  144.         // перечень необходимых атрибутов компонента
  145.         $requiredAttributes array('name''module''class');
  146.  
  147.         //после отработки итератора должны получить $name, $module, $class
  148.         foreach ($requiredAttributes as $attrName{
  149.             if (!isset($componentDescription[$attrName])) {
  150.                 throw new SystemException("ERR_DEV_NO_REQUIRED_ATTRIB $attrName"SystemException::ERR_DEVELOPER);
  151.             }
  152.             $$attrName = (string)$componentDescription[$attrName];
  153.         }
  154.  
  155.  
  156.         // извлекаем параметры компонента
  157.         $params null;
  158.         if (isset($componentDescription->params)) {
  159.             $params array();
  160.             foreach ($componentDescription->params->param as $tagName => $paramDescr{
  161.                 if ($tagName == 'param'{
  162.                     if (isset($paramDescr['name'])) {
  163.                         $paramName = (string)$paramDescr['name'];
  164.                         $paramValue = (string)$paramDescr;
  165.  
  166.                         //Если в массиве параметров уже существует параметр с таким именем, превращаем этот параметр в массив
  167.                         if (isset($params[$paramName])) {
  168.                             if (!is_array($params[$paramName])) {
  169.                                 $params[$paramNamearray($params[$paramName]);
  170.                             }
  171.                             array_push($params[$paramName]$paramValue);
  172.                         }
  173.                         else {
  174.                             $params[$paramName$paramValue;
  175.                         }
  176.                     }
  177.                 }
  178.             }
  179.         }
  180.  
  181.         $component $this->createComponent($name$module$class$params);
  182.  
  183.         return $component;
  184.     }
  185.  
  186.     /**
  187.      * Создает компонент.
  188.      *
  189.      * @access public
  190.      * @param string $name 
  191.      * @param string $module 
  192.      * @param string $class 
  193.      * @param array $params 
  194.      * @return Component 
  195.      */
  196.     public function createComponent($name$module$class$params null{
  197.         try {
  198.             $result new $class($name$module$this->document$params);    
  199.         }
  200.         catch(SystemException $e{
  201.             throw new SystemException('ERR_CLASS_NOT_FOUND'SystemException::ERR_DEVELOPERarray(
  202.                 'class' => (($module !== 'site')?str_replace('*'$moduleCORE_COMPONENTS_DIR):SITE_COMPONENTS_DIR.$module).'/'.$class.'.class.php'
  203.             ));
  204.         }
  205.         return $result;
  206.     }
  207.     /**
  208.      * Возвращает набор компонентов
  209.      *
  210.      * @return array 
  211.      * @access public
  212.      */
  213.  
  214.     public function getComponents({
  215.         return $this->components;
  216.     }
  217.  
  218. }
В создании документации нам помог: phpDocumentor