[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phame/skins/ -> PhameSkinSpecification.php (source)

   1  <?php
   2  
   3  final class PhameSkinSpecification {
   4  
   5    const TYPE_ADVANCED   = 'advanced';
   6    const TYPE_BASIC      = 'basic';
   7  
   8    private $type;
   9    private $rootDirectory;
  10    private $skinClass;
  11    private $phutilLibraries = array();
  12    private $name;
  13    private $config;
  14  
  15    public static function loadAllSkinSpecifications() {
  16      static $specs;
  17  
  18      if ($specs === null) {
  19        $paths = PhabricatorEnv::getEnvConfig('phame.skins');
  20        $base  = dirname(phutil_get_library_root('phabricator'));
  21  
  22        $specs = array();
  23  
  24        foreach ($paths as $path) {
  25          $path = Filesystem::resolvePath($path, $base);
  26          foreach (Filesystem::listDirectory($path) as $skin_directory) {
  27            $skin_path = $path.DIRECTORY_SEPARATOR.$skin_directory;
  28  
  29            if (!is_dir($skin_path)) {
  30              continue;
  31            }
  32            $spec = self::loadSkinSpecification($skin_path);
  33            if (!$spec) {
  34              continue;
  35            }
  36  
  37            $name = trim($skin_directory, DIRECTORY_SEPARATOR);
  38  
  39            $spec->setName($name);
  40  
  41            if (isset($specs[$name])) {
  42              $that_dir = $specs[$name]->getRootDirectory();
  43              $this_dir = $spec->getRootDirectory();
  44              throw new Exception(
  45                "Two skins have the same name ('{$name}'), in '{$this_dir}' and ".
  46                "'{$that_dir}'. Rename one or adjust your 'phame.skins' ".
  47                "configuration.");
  48            }
  49  
  50            $specs[$name] = $spec;
  51          }
  52        }
  53      }
  54  
  55      return $specs;
  56    }
  57  
  58    public static function loadOneSkinSpecification($name) {
  59      $paths = PhabricatorEnv::getEnvConfig('phame.skins');
  60      $base  = dirname(phutil_get_library_root('phabricator'));
  61      foreach ($paths as $path) {
  62        $path = Filesystem::resolvePath($path, $base);
  63        $skin_path = $path.DIRECTORY_SEPARATOR.$name;
  64        if (is_dir($skin_path)) {
  65          $spec = self::loadSkinSpecification($skin_path);
  66          if ($spec) {
  67            $spec->setName($name);
  68            return $spec;
  69          }
  70        }
  71      }
  72      return null;
  73    }
  74  
  75    public static function loadSkinSpecification($path) {
  76  
  77      $config_path = $path.DIRECTORY_SEPARATOR.'skin.json';
  78      $config = array();
  79      if (Filesystem::pathExists($config_path)) {
  80        $config = Filesystem::readFile($config_path);
  81        $config = json_decode($config, true);
  82        if (!is_array($config)) {
  83          throw new Exception(
  84            "Skin configuration file '{$config_path}' is not a valid JSON file.");
  85        }
  86        $type = idx($config, 'type', self::TYPE_BASIC);
  87      } else {
  88        $type = self::TYPE_BASIC;
  89      }
  90  
  91      $spec = new PhameSkinSpecification();
  92      $spec->setRootDirectory($path);
  93      $spec->setConfig($config);
  94  
  95      switch ($type) {
  96        case self::TYPE_BASIC:
  97          $spec->setSkinClass('PhameBasicTemplateBlogSkin');
  98          break;
  99        case self::TYPE_ADVANCED:
 100          $spec->setSkinClass($config['class']);
 101          $spec->addPhutilLibrary($path.DIRECTORY_SEPARATOR.'src');
 102          break;
 103        default:
 104          throw new Exception('Unknown skin type!');
 105      }
 106  
 107      $spec->setType($type);
 108  
 109      return $spec;
 110    }
 111  
 112    public function setConfig(array $config) {
 113      $this->config = $config;
 114      return $this;
 115    }
 116  
 117    public function getConfig($key, $default = null) {
 118      return idx($this->config, $key, $default);
 119    }
 120  
 121    public function setName($name) {
 122      $this->name = $name;
 123      return $this;
 124    }
 125  
 126    public function getName() {
 127      return $this->getConfig('name', $this->name);
 128    }
 129  
 130    public function setRootDirectory($root_directory) {
 131      $this->rootDirectory = $root_directory;
 132      return $this;
 133    }
 134  
 135    public function getRootDirectory() {
 136      return $this->rootDirectory;
 137    }
 138  
 139    public function setType($type) {
 140      $this->type = $type;
 141      return $this;
 142    }
 143  
 144    public function getType() {
 145      return $this->type;
 146    }
 147  
 148    public function setSkinClass($skin_class) {
 149      $this->skinClass = $skin_class;
 150      return $this;
 151    }
 152  
 153    public function getSkinClass() {
 154      return $this->skinClass;
 155    }
 156  
 157    public function addPhutilLibrary($library) {
 158      $this->phutilLibraries[] = $library;
 159      return $this;
 160    }
 161  
 162    public function buildSkin(AphrontRequest $request) {
 163      foreach ($this->phutilLibraries as $library) {
 164        phutil_load_library($library);
 165      }
 166  
 167      return newv($this->getSkinClass(), array($request, $this));
 168    }
 169  
 170  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1