/** * Loads the configuration file * * @param string $fileKey * @return array * @throws \Exception */ public function load($fileKey = null) { $path = $this->dirList->getPath(DirectoryList::CONFIG); $fileDriver = $this->driverPool->getDriver(DriverPool::FILE); $result = []; if ($fileKey) { $filePath = $path . '/' . $this->configFilePool->getPath($fileKey); if ($fileDriver->isExists($filePath)) { $result = (include $filePath); } } else { $configFiles = $this->configFilePool->getPaths(); $allFilesData = []; $result = []; foreach (array_keys($configFiles) as $fileKey) { $configFile = $path . '/' . $this->configFilePool->getPath($fileKey); if ($fileDriver->isExists($configFile)) { $fileData = (include $configFile); } else { continue; } $allFilesData[$configFile] = $fileData; if (!empty($fileData)) { $intersection = array_intersect_key($result, $fileData); if (!empty($intersection)) { $displayMessage = $this->findFilesWithKeys(array_keys($intersection), $allFilesData); throw new \Exception("Key collision! The following keys occur in multiple config files:" . PHP_EOL . $displayMessage); } $result = array_merge($result, $fileData); } } } return $result ?: []; }
/** * Retrieve cache states (enabled/disabled) information * * Access configuration file directly as it is not possible to re-include modified file under HHVM * @link https://github.com/facebook/hhvm/issues/1447 * * @return array * @SuppressWarnings(PHPMD.EvalExpression) */ protected function getCacheStates() { $configFilePool = new ConfigFilePool(); $configPath = Bootstrap::getInstance()->getAppTempDir() . '/' . DirectoryList::CONFIG . '/' . $configFilePool->getPath($configFilePool::APP_ENV); $configData = eval(str_replace('<?php', '', file_get_contents($configPath))); return $configData[State::CACHE_KEY]; }
/** * Loads the configuration file * * @param string $fileKey * @return array * @throws \Exception */ public function load($fileKey = null) { $path = $this->dirList->getPath(DirectoryList::CONFIG); if ($fileKey) { $result = @(include $path . '/' . $this->configFilePool->getPath($fileKey)); } else { $configFiles = $this->configFilePool->getPaths(); $result = []; foreach (array_keys($configFiles) as $fileKey) { $configFile = $path . '/' . $this->configFilePool->getPath($fileKey); $fileData = @(include $configFile); if (!empty($fileData)) { $intersection = array_intersect_key($result, $fileData); if (!empty($intersection)) { $displayList = ''; foreach (array_keys($intersection) as $key) { $displayList .= $key . PHP_EOL; } throw new \Exception("Key collision! The following keys occur in multiple config files:" . PHP_EOL . $displayList); } $result = array_merge($result, $fileData); } } } return $result ?: []; }
public function testSaveConfigOverride() { $configFiles = [ConfigFilePool::APP_CONFIG => 'test_conf.php', 'test_key' => 'test2_conf.php']; $testSetExisting = [ConfigFilePool::APP_CONFIG => ['foo' => 'bar', 'key' => 'value', 'baz' => ['test' => 'value', 'test1' => 'value1']]]; $testSetUpdate = [ConfigFilePool::APP_CONFIG => ['baz' => ['test' => 'value2']]]; $testSetExpected = [ConfigFilePool::APP_CONFIG => ['foo' => 'bar', 'key' => 'value', 'baz' => ['test' => 'value2']]]; $this->deploymentConfig->expects($this->once())->method('resetData'); $this->configFilePool->expects($this->once())->method('getPaths')->willReturn($configFiles); $this->dirWrite->expects($this->any())->method('isExist')->willReturn(true); $this->reader->expects($this->once())->method('load')->willReturn($testSetExisting[ConfigFilePool::APP_CONFIG]); $this->formatter->expects($this->once())->method('format')->with($testSetExpected[ConfigFilePool::APP_CONFIG])->willReturn([]); $this->dirWrite->expects($this->once())->method('writeFile')->with('test_conf.php', []); $this->object->saveConfig($testSetUpdate, true); }
/** * Saves config * * @param array $data * @param bool $override * @return void */ public function saveConfig(array $data, $override = false) { $paths = $this->configFilePool->getPaths(); foreach ($data as $fileKey => $config) { if (isset($paths[$fileKey])) { if ($this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->isExist($paths[$fileKey])) { $currentData = $this->reader->load($fileKey); if ($override) { $config = array_merge($currentData, $config); } else { $config = array_replace_recursive($currentData, $config); } } $contents = $this->formatter->format($config); try { $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile($paths[$fileKey], $contents); } catch (FileSystemException $e) { throw new FileSystemException(new Phrase('Deployment config file %1 is not writable.', [$paths[$fileKey]])); } if (function_exists('opcache_invalidate')) { opcache_invalidate($this->filesystem->getDirectoryRead(DirectoryList::CONFIG)->getAbsolutePath($paths[$fileKey])); } } } $this->deploymentConfig->resetData(); }
/** * @expectedException \Magento\Framework\Exception\FileSystemException * @expectedExceptionMessage Deployment config file env.php is not writable. */ public function testSaveConfigException() { $this->configFilePool->method('getPaths')->willReturn([ConfigFilePool::APP_ENV => 'env.php']); $exception = new FileSystemException(new Phrase('error when writing file config file')); $this->dirWrite->method('writeFile')->willThrowException($exception); $this->object->saveConfig([ConfigFilePool::APP_ENV => ['key' => 'value']]); }
/** * Saves config * * @param array $data * @param bool $override * @return void */ public function saveConfig(array $data, $override = false) { $paths = $this->configFilePool->getPaths(); foreach ($data as $fileKey => $config) { if (isset($paths[$fileKey])) { if ($this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->isExist($paths[$fileKey])) { $currentData = $this->reader->load($paths[$fileKey]); if ($override) { $config = array_merge($currentData, $config); } else { $config = array_replace_recursive($currentData, $config); } } $contents = $this->formatter->format($config); $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile($paths[$fileKey], $contents); } } $this->deploymentConfig->resetData(); }
/** * Returns path to env.php file * * @return string * @throws \Exception */ private function getEnvPath() { $deploymentConfig = $this->directoryList->getPath(DirectoryList::CONFIG); $configPool = new ConfigFilePool(); $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV); return $envPath; }
/** * @expectedException \Exception * @expectedExceptionMessage File config key does not exist. */ public function testGetPathException() { $fileKey = 'not_existing'; $this->configFilePool->getPath($fileKey); }