Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class App_Widget_Manager_Exception extends Exception {
- public function __construct($message, $code=128, $previous=NULL) {
- parent::__construct($message, $code, $previous);
- }
- }
- class App_Widget_Manager {
- protected static $_instance;
- private $_widgetsDir = './Widgets'; //Директория с классами виджетов
- private $_widget; //Экземпляр виджета
- private $_options; //Опции виджета
- private $_widgetName; //имя виджета
- private $_viewsDir = './Widgets/views/'; //Директория view-cкриптов виджета
- private $_defaultAction = 'index'; //Имя action'a по умолчанию при загрузке виджета
- private $_actionName; //Имя текущего метода
- public function init() {
- }
- /**
- * Защита от создания класса конструктором или клонированием
- */
- private function __clone() {
- }
- private function __construct() {
- }
- /**
- *
- * @return instanceof App_Widgets_Manager
- */
- public static function getInstance() {
- if (is_null(self::$_instance)) {
- self::$_instance = new self;
- }
- return self::$_instance;
- }
- /**
- *
- * @param string $widgetName - Имя загружаемого виджета
- */
- public function setWidgetsDir($dirPath) {
- if ((is_dir($dirPath) || (is_readable($dirPath)))) {
- $this->_widgetsDir = $dirPath;
- return $this->_widget;
- } else {
- $this->_widgetsDir = false;
- throw new App_Widget_Manager_Exception('Illegal widgets dir path specified (' . $dirPath . ')');
- }
- }
- public function loadWidget($widgetName) {
- //var_dump($this->_widgetsDir . '/' . $widgetName);
- if (!$this->_widgetsDir) {
- throw new App_Widget_Manager_Exception('Illegal widgets dir path specified (' . $dirPath . ')');
- } else {
- if ((!is_dir($this->_widgetsDir . '/' . $widgetName)) || (!is_readable($this->_widgetsDir . '/' . $widgetName))) {
- throw new App_Widget_Manager_Exception('Can\'t load (' . $widgetName . ')');
- } else {
- require_once ($this->_widgetsDir . '/' . $widgetName . '/' . $widgetName . '.php');
- $this->_widgetName = $widgetName;
- $this->_widget = new $this->_widgetName;
- //call_user_func($widgetName.'::'.$this->_defaultAction);
- return $this;
- }
- }
- }
- public function setView($viewsDir) {
- // var_dump(is_readable($this->_widgetsDir . '/views' . $viewsDir .'/')&&is_dir($this->_widgetsDir . '/views' . $viewsDir .'/'));
- if ((!is_dir($viewsDir)) || (!is_readable($viewsDir))) {
- throw new App_Widget_Manager_Exception('Illegal widgets view dir !');
- } else {
- $this->_viewsDir = $viewsDir;
- }
- //var_dump($this);
- return $this;
- }
- public function getView (){
- return $this->_viewsDir.'/'.$this->_widgetName;
- }
- /**
- * Массив конфигурационных значений для виджета
- * @param array $config
- * @example array ('viewsPath'=>'path/to/views_dir', 'defaultAction'=>'index')
- */
- public function configure($config) {
- if (!isset($config['viewsPath'])) {
- throw new App_Widget_Manager_Exception('You must specify path to folder with view scripts');
- } else {
- $this->setView($config['viewsPath']);
- }
- if (!isset($config['defaultAction'])) {
- $this->_defaultAction = 'index';
- } else {
- $this->_defaultAction = $config['defaultAction'];
- }
- return $this;
- //var_dump($this);
- }
- /**
- *
- * @param string $widgetName
- * @param array $options
- */
- public function setOptions($options) {
- if (is_array($options))
- $this->_options = $options;
- else
- throw new App_Widget_Manager_Exception('Options must be an array');
- return $this;
- }
- /**
- *
- * @param string $action - Текущий запрошенный action
- * @param array $vars - массив параметров, которые надо передать во view
- * @return App_Widget_Manager
- */
- public function run($action=null, $vars=null) {
- if (is_null($action)) {
- if (is_callable($this->_widgetName . '::' . $this->_defaultAction)) {
- call_user_func($this->_widgetName . '::' . $this->_defaultAction);
- $this->_actionName = $rhis->_defaultAction;
- } else {
- throw new App_Widget_Manager_Exception('Can\'t call ' . $this->_widgetName . '::' . $this->_defaultAction);
- }
- } elseif (!is_null($action)) {
- if (is_callable($this->_widgetName . '::' . $action)) {
- call_user_func($this->_widgetName . '::' . $action);
- $this->_actionName = $action;
- }
- elseif (is_callable($this->_widgetName . '::' . $this->_defaultAction)) {
- call_user_func($this->_widgetName . '::' . $this->_defaultAction);
- $this->_actionName = $this->_defaultAction;
- }
- else
- throw new App_Widget_Manager_Exception('Can\'t call ' . $this->_widgetName . '::' . $action);
- }
- $view = new Zend_View();
- // var_dump($this->getView());
- $view->setBasePath($this->getView());
- if (!is_null($vars)) {
- foreach ($vars as $key => $value) {
- $view->assign($key, $value);
- }
- }
- return $view->render($this->_actionName.'.phtml');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement