/** * 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 ?: []; }
/** * 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 testGetPaths() { $expected['new_key'] = 'new_config.php'; $expected[ConfigFilePool::APP_CONFIG] = 'config.php'; $expected[ConfigFilePool::APP_ENV] = 'env.php'; $this->assertEquals($expected, $this->configFilePool->getPaths()); }
/** * 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(); }
/** * 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(); }