Пример #1
0
 /**
  * Factory pattern used to instanciate a proper decorator for the given object extended from t41\View\ViewObject
  *
  * @param t41\View\ViewObject $object disabled for now because of legacy problems
  * @param array $params
  * @return t41\View\AbstractDecorator
  */
 public static function factory($object, array $params = null)
 {
     $decoratorData = $object->getDecorator();
     if (is_null($params) && isset($decoratorData['params'])) {
         $params = $decoratorData['params'];
     } elseif (!is_null($params) && isset($decoratorData['params'])) {
         $params = array_merge($params, $decoratorData['params']);
     }
     // class name without the first component of the namespace
     $class = substr(get_class($object), strpos(get_class($object), '\\') + 1);
     if (View::getViewType() != Adapter\WebAdapter::ID) {
         $decoratorData['name'] = 'Default';
     }
     if (!isset($decoratorData['name']) || trim($decoratorData['name']) == '') {
         $decoratorData['name'] = 'Default';
     }
     // decorator name
     $deconame = View::getContext() . ucfirst($decoratorData['name']);
     foreach (self::$_paths as $library) {
         $file = $library['path'] . str_replace('\\', '/', $class) . '/' . $deconame . '.php';
         if (file_exists($file)) {
             require_once $file;
             $fullclassname = '\\' . $library['ns'] . '\\' . $class . '\\' . $deconame;
             if (class_exists($fullclassname)) {
                 try {
                     $decorator = new $fullclassname($object, $params);
                 } catch (Exception $e) {
                     throw new Exception("Error instanciating '{$fullclassname}' decorator.", $e->getCode(), $e);
                 }
                 return $decorator;
             }
         }
     }
     throw new Exception("The decorator class '{$class}' doesn't exist or was not found");
 }