: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in
use Noodlehaus\Exception\FileNotFoundException;
use Noodlehaus\Exception\UnsupportedFormatException;
use Noodlehaus\Exception\EmptyDirectoryException;
use Noodlehaus\Parser\ParserInterface;
use Noodlehaus\Writer\WriterInterface;
* Configuration reader and writer for PHP.
* @author Jesus A. Domingo <jesus.domingo@gmail.com>
* @author Hassan Khan <contact@hassankhan.me>
* @author Filip Š <projects@filips.si>
* @link https://github.com/noodlehaus/config
class Config extends AbstractConfig
* All formats supported by Config.
protected $supportedParsers = [
'Noodlehaus\Parser\Json',
'Noodlehaus\Parser\Yaml',
'Noodlehaus\Parser\Properties',
'Noodlehaus\Parser\Serialize'
* All formats supported by Config.
protected $supportedWriters = [
'Noodlehaus\Writer\Json',
'Noodlehaus\Writer\Yaml',
'Noodlehaus\Writer\Properties',
'Noodlehaus\Writer\Serialize'
* Static method for loading a Config instance.
* @param string|array $values Filenames or string with configuration
* @param ParserInterface $parser Configuration parser
* @param bool $string Enable loading from string
public static function load($values, $parser = null, $string = false)
return new static($values, $parser, $string);
* Loads a Config instance.
* @param string|array $values Filenames or string with configuration
* @param ParserInterface $parser Configuration parser
* @param bool $string Enable loading from string
public function __construct($values, ParserInterface $parser = null, $string = false)
$this->loadFromString($values, $parser);
$this->loadFromFile($values, $parser);
parent::__construct($this->data);
* Loads configuration from file.
* @param string|array $path Filenames or directories with configuration
* @param ParserInterface $parser Configuration parser
* @throws EmptyDirectoryException If `$path` is an empty directory
protected function loadFromFile($path, ParserInterface $parser = null)
$paths = $this->getValidPath($path);
foreach ($paths as $path) {
$parts = explode('.', $info['basename']);
$extension = array_pop($parts);
// Skip the `dist` extension
if ($extension === 'dist') {
$extension = array_pop($parts);
$parser = $this->getParser($extension);
$this->data = array_replace_recursive($this->data, $parser->parseFile($path));
// Try to load file using specified parser
$this->data = array_replace_recursive($this->data, $parser->parseFile($path));
* Writes configuration to file.
* @param string $filename Filename to save configuration to
* @param WriterInterface $writer Configuration writer
* @throws WriteException if the data could not be written to the file
public function toFile($filename, WriterInterface $writer = null)
$info = pathinfo($filename);
$parts = explode('.', $info['basename']);
$extension = array_pop($parts);
// Skip the `dist` extension
if ($extension === 'dist') {
$extension = array_pop($parts);
$writer = $this->getWriter($extension);
$writer->toFile($this->all(), $filename);
// Try to load file using specified writer
$writer->toFile($this->all(), $filename);
* Loads configuration from string.
* @param string $configuration String with configuration
* @param ParserInterface $parser Configuration parser
protected function loadFromString($configuration, ParserInterface $parser)
$this->data = array_replace_recursive($this->data, $parser->parseString($configuration));
* Writes configuration to string.
* @param WriterInterface $writer Configuration writer
* @param boolean $pretty Encode pretty
public function toString(WriterInterface $writer, $pretty = true)
return $writer->toString($this->all(), $pretty);
* Gets a parser for a given file extension.
* @param string $extension
* @return Noodlehaus\Parser\ParserInterface
* @throws UnsupportedFormatException If `$extension` is an unsupported file format
protected function getParser($extension)
foreach ($this->supportedParsers as $parser) {
if (in_array($extension, $parser::getSupportedExtensions())) {
// If none exist, then throw an exception
throw new UnsupportedFormatException('Unsupported configuration format');
* Gets a writer for a given file extension.
* @param string $extension
* @return Noodlehaus\Writer\WriterInterface
* @throws UnsupportedFormatException If `$extension` is an unsupported file format
protected function getWriter($extension)
foreach ($this->supportedWriters as $writer) {
if (in_array($extension, $writer::getSupportedExtensions())) {
// If none exist, then throw an exception
throw new UnsupportedFormatException('Unsupported configuration format'.$extension);
* @throws FileNotFoundException If a file is not found at `$path`
protected function getPathFromArray($path)
foreach ($path as $unverifiedPath) {
// Check if `$unverifiedPath` is optional
// If it exists, then it's added to the list
// If it doesn't, it throws an exception which we catch
if ($unverifiedPath[0] !== '?') {
$paths = array_merge($paths, $this->getValidPath($unverifiedPath));
$optionalPath = ltrim($unverifiedPath, '?');
$paths = array_merge($paths, $this->getValidPath($optionalPath));
} catch (FileNotFoundException $e) {
// If `$unverifiedPath` is optional, then skip it
if ($unverifiedPath[0] === '?') {
// Otherwise rethrow the exception
* Checks `$path` to see if it is either an array, a directory, or a file.
* @param string|array $path
* @throws EmptyDirectoryException If `$path` is an empty directory
* @throws FileNotFoundException If a file is not found at `$path`
protected function getValidPath($path)
return $this->getPathFromArray($path);
// If `$path` is a directory
$paths = glob($path . '/*.*');
throw new EmptyDirectoryException("Configuration directory: [$path] is empty");
// If `$path` is not a file, throw an exception
if (!file_exists($path)) {
throw new FileNotFoundException("Configuration file: [$path] cannot be found");