TYPO3  7.6
cms-composer-installers/Classes/TYPO3/CMS/Composer/Plugin/Config.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Composer\Plugin;
3 
7 class Config {
8 
9  const RELATIVE_PATHS = 1;
10 
14  public static $defaultConfig = array(
15  'web-dir' => '.',
16  'backend-dir' => '{$web-dir}/typo3',
17  'config-dir' => '{$web-dir}/typo3conf',
18  'temporary-dir' => '{$web-dir}/typo3temp',
19  'cache-dir' => '{$temporary-dir}/Cache',
20  'cms-package-dir' => 'typo3_src',
21  'composer-mode' => true,
22  );
23 
27  protected $config;
28 
32  protected $baseDir;
33 
34 
38  public function __construct($baseDir = null) {
39  $this->baseDir = $baseDir;
40  // load defaults
41  $this->config = static::$defaultConfig;
42  }
43 
49  public function merge(array $config) {
50  // Override defaults with given config
51  if (!empty($config['typo3/cms']) && is_array($config['typo3/cms'])) {
52  foreach ($config['typo3/cms'] as $key => $val) {
53  $this->config[$key] = $val;
54  }
55  }
56  }
57 
66  public function get($key, $flags = 0) {
67  switch ($key) {
68  case 'web-dir':
69  case 'backend-dir':
70  case 'configuration-dir':
71  case 'temporary-dir':
72  case 'cache-dir':
73  case 'cms-package-dir':
74  $val = rtrim($this->process($this->config[$key], $flags), '/\\');
75  return ($flags & self::RELATIVE_PATHS == 1) ? $val : $this->realpath($val);
76  default:
77  if (!isset($this->config[$key])) {
78  return NULL;
79  }
80  return $this->process($this->config[$key], $flags);
81  }
82  }
83 
88  public function all($flags = 0) {
89  $all = array();
90  foreach (array_keys($this->config) as $key) {
91  $all['config'][$key] = $this->get($key, $flags);
92  }
93 
94  return $all;
95  }
96 
100  public function raw() {
101  return array(
102  'config' => $this->config,
103  );
104  }
105 
112  public function has($key) {
113  return array_key_exists($key, $this->config);
114  }
115 
123  protected function process($value, $flags) {
124  $config = $this;
125 
126  if (!is_string($value)) {
127  return $value;
128  }
129 
130  return preg_replace_callback('#\{\$(.+)\}#',
131  function ($match) use ($config, $flags) {
132  return $config->get($match[1], $flags);
133  },
134  $value);
135  }
136 
145  protected function realpath($path) {
146  if (substr($path, 0, 1) === '/' || substr($path, 1, 1) === ':') {
147  return $path;
148  }
149 
150  return $this->baseDir . '/' . $path;
151  }
152 
156  public function getBaseDir() {
157  return $this->baseDir;
158  }
159 
164  static public function load(\Composer\Composer $composer) {
165  static $config;
166  if ($config === NULL) {
167  $baseDir = static::extractBaseDir($composer->getConfig());
168  $config = new static($baseDir);
169  $rootPackageExtraConfig = $composer->getPackage()->getExtra();
170  if (is_array($rootPackageExtraConfig)) {
171  $config->merge($rootPackageExtraConfig);
172  }
173  $config->merge(
174  array(
175  'typo3/cms' => array(
176  'vendor-dir' => $composer->getConfig()->get('vendor-dir')
177  )
178  )
179  );
180  }
181  return $config;
182  }
183 
188  static protected function extractBaseDir(\Composer\Config $config) {
189  $reflectionClass = new \ReflectionClass($config);
190  $reflectionProperty = $reflectionClass->getProperty('baseDir');
191  $reflectionProperty->setAccessible(true);
192  return $reflectionProperty->getValue($config);
193  }
194 
195 }