TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
core/Classes/Configuration/ConfigurationManager.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Configuration;
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
37 {
41  protected $defaultConfigurationFile = 'typo3/sysext/core/Configuration/DefaultConfiguration.php';
42 
46  protected $localConfigurationFile = 'typo3conf/LocalConfiguration.php';
47 
51  protected $additionalConfigurationFile = 'typo3conf/AdditionalConfiguration.php';
52 
56  protected $factoryConfigurationFile = 'typo3/sysext/core/Configuration/FactoryConfiguration.php';
57 
61  protected $additionalFactoryConfigurationFile = 'typo3conf/AdditionalFactoryConfiguration.php';
62 
66  protected $pathTypo3Conf = PATH_typo3conf;
67 
75  'EXT/extConf',
76  'EXTCONF',
77  'DB',
78  'SYS/caching/cacheConfigurations',
79  ];
80 
86  public function getDefaultConfiguration()
87  {
88  return require $this->getDefaultConfigurationFileLocation();
89  }
90 
99  {
100  return PATH_site . $this->defaultConfigurationFile;
101  }
102 
108  public function getLocalConfiguration()
109  {
110  return require $this->getLocalConfigurationFileLocation();
111  }
112 
121  {
122  return PATH_site . $this->localConfigurationFile;
123  }
124 
133  {
134  return PATH_site . $this->additionalConfigurationFile;
135  }
136 
143  {
144  return PATH_site . $this->factoryConfigurationFile;
145  }
146 
153  {
155  }
156 
163  public function updateLocalConfiguration(array $configurationToMerge)
164  {
165  $newLocalConfiguration = $this->getLocalConfiguration();
166  ArrayUtility::mergeRecursiveWithOverrule($newLocalConfiguration, $configurationToMerge);
167  $this->writeLocalConfiguration($newLocalConfiguration);
168  }
169 
176  public function getDefaultConfigurationValueByPath($path)
177  {
179  }
180 
187  public function getLocalConfigurationValueByPath($path)
188  {
189  return ArrayUtility::getValueByPath($this->getLocalConfiguration(), $path);
190  }
191 
199  public function getConfigurationValueByPath($path)
200  {
201  $defaultConfiguration = $this->getDefaultConfiguration();
202  ArrayUtility::mergeRecursiveWithOverrule($defaultConfiguration, $this->getLocalConfiguration());
203  return ArrayUtility::getValueByPath($defaultConfiguration, $path);
204  }
205 
213  public function setLocalConfigurationValueByPath($path, $value)
214  {
215  $result = false;
216  if ($this->isValidLocalConfigurationPath($path)) {
217  $localConfiguration = $this->getLocalConfiguration();
218  $localConfiguration = ArrayUtility::setValueByPath($localConfiguration, $path, $value);
219  $result = $this->writeLocalConfiguration($localConfiguration);
220  }
221  return $result;
222  }
223 
230  public function setLocalConfigurationValuesByPathValuePairs(array $pairs)
231  {
232  $localConfiguration = $this->getLocalConfiguration();
233  foreach ($pairs as $path => $value) {
234  if ($this->isValidLocalConfigurationPath($path)) {
235  $localConfiguration = ArrayUtility::setValueByPath($localConfiguration, $path, $value);
236  }
237  }
238  return $this->writeLocalConfiguration($localConfiguration);
239  }
240 
247  public function removeLocalConfigurationKeysByPath(array $keys)
248  {
249  $result = false;
250  $localConfiguration = $this->getLocalConfiguration();
251  foreach ($keys as $path) {
252  // Remove key if path is within LocalConfiguration
253  if (ArrayUtility::isValidPath($localConfiguration, $path)) {
254  $result = true;
255  $localConfiguration = ArrayUtility::removeByPath($localConfiguration, $path);
256  }
257  }
258  if ($result) {
259  $this->writeLocalConfiguration($localConfiguration);
260  }
261  return $result;
262  }
263 
270  public function canWriteConfiguration()
271  {
272  $fileLocation = $this->getLocalConfigurationFileLocation();
273  return @is_writable(file_exists($fileLocation) ? $fileLocation : $this->pathTypo3Conf);
274  }
275 
283  public function exportConfiguration()
284  {
285  if (@is_file($this->getLocalConfigurationFileLocation())) {
286  $localConfiguration = $this->getLocalConfiguration();
287  if (is_array($localConfiguration)) {
288  $defaultConfiguration = $this->getDefaultConfiguration();
289  ArrayUtility::mergeRecursiveWithOverrule($defaultConfiguration, $localConfiguration);
290  $GLOBALS['TYPO3_CONF_VARS'] = $defaultConfiguration;
291  } else {
292  throw new \UnexpectedValueException('LocalConfiguration invalid.', 1349272276);
293  }
294  if (@is_file($this->getAdditionalConfigurationFileLocation())) {
296  }
297  } else {
298  // No LocalConfiguration (yet), load DefaultConfiguration only
299  $GLOBALS['TYPO3_CONF_VARS'] = $this->getDefaultConfiguration();
300  }
301  }
302 
311  public function writeLocalConfiguration(array $configuration)
312  {
314  if (!$this->canWriteConfiguration()) {
315  throw new \RuntimeException(
316  $localConfigurationFile . ' is not writable.', 1346323822
317  );
318  }
319  $configuration = ArrayUtility::sortByKeyRecursive($configuration);
320  $result = GeneralUtility::writeFile(
322  '<?php' . LF .
323  'return ' .
326  ) .
327  ';' . LF,
328  true
329  );
330 
331  GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive($localConfigurationFile);
332 
333  return $result;
334  }
335 
344  public function writeAdditionalConfiguration(array $additionalConfigurationLines)
345  {
347  PATH_site . $this->additionalConfigurationFile,
348  '<?php' . LF .
349  implode(LF, $additionalConfigurationLines) . LF
350  );
351  }
352 
363  {
364  if (file_exists($this->getLocalConfigurationFileLocation())) {
365  throw new \RuntimeException(
366  'LocalConfiguration.php exists already',
367  1364836026
368  );
369  }
370  $localConfigurationArray = require $this->getFactoryConfigurationFileLocation();
371  $additionalFactoryConfigurationFileLocation = $this->getAdditionalFactoryConfigurationFileLocation();
372  if (file_exists($additionalFactoryConfigurationFileLocation)) {
373  $additionalFactoryConfigurationArray = require $additionalFactoryConfigurationFileLocation;
375  $localConfigurationArray,
376  $additionalFactoryConfigurationArray
377  );
378  }
379  $this->writeLocalConfiguration($localConfigurationArray);
380  }
381 
388  protected function isValidLocalConfigurationPath($path)
389  {
390  // Early return for white listed paths
391  foreach ($this->whiteListedLocalConfigurationPaths as $whiteListedPath) {
392  if (GeneralUtility::isFirstPartOfStr($path, $whiteListedPath)) {
393  return true;
394  }
395  }
396  return ArrayUtility::isValidPath($this->getDefaultConfiguration(), $path);
397  }
398 }
static renumberKeysToAvoidLeapsIfKeysAreAllNumeric(array $array=[], $level=0)
static isFirstPartOfStr($str, $partStr)
static getValueByPath(array $array, $path, $delimiter= '/')
static writeFile($file, $content, $changePermissions=false)
static removeByPath(array $array, $path, $delimiter= '/')
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=true, $includeEmptyValues=true, $enableUnsetFeature=true)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
static makeInstance($className,...$constructorArguments)
static setValueByPath(array $array, $path, $value, $delimiter= '/')
static isValidPath(array $array, $path, $delimiter= '/')