Advertisement
NFL

Вот такое пиздец...

NFL
Jul 12th, 2011
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.98 KB | None | 0 0
  1. <?php
  2.  
  3. class App_Widget_Manager_Exception extends Exception {
  4.  
  5.     public function __construct($message, $code=128, $previous=NULL) {
  6.         parent::__construct($message, $code, $previous);
  7.     }
  8.  
  9. }
  10.  
  11. class App_Widget_Manager {
  12.  
  13.     protected static $_instance;
  14.     private $_widgetsDir = './Widgets'; //Директория с классами виджетов
  15.     private $_widget; //Экземпляр виджета
  16.     private $_options; //Опции виджета
  17.     private $_widgetName; //имя виджета
  18.     private $_viewsDir = './Widgets/views/'; //Директория view-cкриптов виджета
  19.     private $_defaultAction = 'index'; //Имя action'a по умолчанию при загрузке виджета
  20.     private $_actionName; //Имя текущего метода
  21.  
  22.     public function init() {
  23.        
  24.     }
  25.  
  26.     /**
  27.      * Защита от создания класса конструктором или клонированием
  28.      */
  29.     private function __clone() {
  30.        
  31.     }
  32.  
  33.     private function __construct() {
  34.        
  35.     }
  36.  
  37.     /**
  38.      *
  39.      * @return instanceof App_Widgets_Manager
  40.      */
  41.     public static function getInstance() {
  42.         if (is_null(self::$_instance)) {
  43.             self::$_instance = new self;
  44.         }
  45.         return self::$_instance;
  46.        
  47.     }
  48.  
  49.     /**
  50.      *
  51.      * @param string $widgetName - Имя загружаемого виджета
  52.      */
  53.     public function setWidgetsDir($dirPath) {
  54.         if ((is_dir($dirPath) || (is_readable($dirPath)))) {
  55.             $this->_widgetsDir = $dirPath;
  56.             return $this->_widget;
  57.         } else {
  58.             $this->_widgetsDir = false;
  59.             throw new App_Widget_Manager_Exception('Illegal widgets dir path specified (' . $dirPath . ')');
  60.         }
  61.     }
  62.  
  63.     public function loadWidget($widgetName) {
  64.         //var_dump($this->_widgetsDir . '/' . $widgetName);
  65.         if (!$this->_widgetsDir) {
  66.             throw new App_Widget_Manager_Exception('Illegal widgets dir path specified (' . $dirPath . ')');
  67.         } else {
  68.             if ((!is_dir($this->_widgetsDir . '/' . $widgetName)) || (!is_readable($this->_widgetsDir . '/' . $widgetName))) {
  69.                 throw new App_Widget_Manager_Exception('Can\'t load (' . $widgetName . ')');
  70.             } else {
  71.                 require_once ($this->_widgetsDir . '/' . $widgetName . '/' . $widgetName . '.php');
  72.                 $this->_widgetName = $widgetName;
  73.                 $this->_widget = new $this->_widgetName;
  74.                 //call_user_func($widgetName.'::'.$this->_defaultAction);
  75.                 return $this;
  76.             }
  77.         }
  78.     }
  79.  
  80.     public function setView($viewsDir) {
  81.         // var_dump(is_readable($this->_widgetsDir . '/views' . $viewsDir .'/')&&is_dir($this->_widgetsDir . '/views' . $viewsDir .'/'));
  82.         if ((!is_dir($viewsDir)) || (!is_readable($viewsDir))) {
  83.             throw new App_Widget_Manager_Exception('Illegal widgets view dir !');
  84.         } else {
  85.             $this->_viewsDir = $viewsDir;
  86.         }
  87.         //var_dump($this);
  88.         return $this;
  89.     }
  90.     public function getView (){
  91.         return $this->_viewsDir.'/'.$this->_widgetName;
  92.     }
  93.     /**
  94.      * Массив конфигурационных значений для виджета
  95.      * @param array $config
  96.      * @example array ('viewsPath'=>'path/to/views_dir', 'defaultAction'=>'index')
  97.      */
  98.     public function configure($config) {
  99.  
  100.         if (!isset($config['viewsPath'])) {
  101.             throw new App_Widget_Manager_Exception('You must specify path to folder with view scripts');
  102.         } else {
  103.             $this->setView($config['viewsPath']);
  104.         }
  105.         if (!isset($config['defaultAction'])) {
  106.             $this->_defaultAction = 'index';
  107.         } else {
  108.             $this->_defaultAction = $config['defaultAction'];
  109.         }
  110.         return $this;
  111.         //var_dump($this);
  112.     }
  113.  
  114.     /**
  115.      *
  116.      * @param string $widgetName
  117.      * @param array $options
  118.      */
  119.     public function setOptions($options) {
  120.         if (is_array($options))
  121.             $this->_options = $options;
  122.         else
  123.             throw new App_Widget_Manager_Exception('Options must be an array');
  124.  
  125.         return $this;
  126.     }
  127.     /**
  128.      *
  129.      * @param string $action - Текущий запрошенный action
  130.      * @param array $vars - массив параметров, которые надо передать во view
  131.      * @return App_Widget_Manager
  132.      */
  133.    
  134.     public function run($action=null, $vars=null) {
  135.         if (is_null($action)) {
  136.  
  137.             if (is_callable($this->_widgetName . '::' . $this->_defaultAction)) {
  138.                 call_user_func($this->_widgetName . '::' . $this->_defaultAction);
  139.                 $this->_actionName = $rhis->_defaultAction;
  140.             } else {
  141.                 throw new App_Widget_Manager_Exception('Can\'t call ' . $this->_widgetName . '::' . $this->_defaultAction);
  142.             }
  143.         } elseif (!is_null($action)) {
  144.             if (is_callable($this->_widgetName . '::' . $action)) {
  145.                 call_user_func($this->_widgetName . '::' . $action);
  146.                 $this->_actionName = $action;
  147.             }
  148.             elseif (is_callable($this->_widgetName . '::' . $this->_defaultAction)) {
  149.                 call_user_func($this->_widgetName . '::' . $this->_defaultAction);
  150.                 $this->_actionName = $this->_defaultAction;
  151.                
  152.             }
  153.             else
  154.                 throw new App_Widget_Manager_Exception('Can\'t call ' . $this->_widgetName . '::' . $action);
  155.         }
  156.         $view = new Zend_View();
  157.        // var_dump($this->getView());
  158.         $view->setBasePath($this->getView());  
  159.         if (!is_null($vars)) {
  160.             foreach ($vars as $key => $value) {
  161.                 $view->assign($key, $value);
  162.             }
  163.         }        
  164.         return $view->render($this->_actionName.'.phtml');
  165.     }
  166.  
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement