Source of file Config.php
Size: 4,337 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Config/Config.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Config; use Bluz\Common\Collection; /** * Config * * @package Bluz\Config * @author Anton Shevchuk * @link https://github.com/bluzphp/framework/wiki/Config */ class Config { /** * @var array configuration data */ protected $config; /** * @var array modules configuration data */ protected $modules; /** * @var string path to configuration files */ protected $path; /** * @var string environment */ protected $environment; /** * Set path to configuration files * * @param string $path * * @return void * @throws ConfigException */ public function setPath($path) { if (!is_dir($path)) { throw new ConfigException('Configuration directory is not exists'); } $this->path = rtrim($path, '/'); } /** * Set application environment * * @param string $environment * * @return void */ public function setEnvironment($environment) { $this->environment = $environment; } /** * Load configuration * * @return void * @throws ConfigException */ public function init() { if (!$this->path) { throw new ConfigException('Configuration directory is not setup'); } $this->config = $this->loadFiles($this->path . '/configs/default'); if ($this->environment) { $customConfig = $this->loadFiles($this->path . '/configs/' . $this->environment); $this->config = array_replace_recursive($this->config, $customConfig); } } /** * Load configuration file * * @param string $path * * @return array * @throws ConfigException */ protected function loadFile($path) { if (!is_file($path) && !is_readable($path)) { throw new ConfigException("Configuration file `$path` not found"); } return include $path; } /** * Load configuration files to array * * @param string $path * * @return array * @throws ConfigException */ protected function loadFiles($path) { $config = []; if (!is_dir($path)) { throw new ConfigException("Configuration directory `$path` not found"); } $iterator = new \GlobIterator( $path . '/*.php', \FilesystemIterator::KEY_AS_FILENAME | \FilesystemIterator::CURRENT_AS_PATHNAME ); foreach ($iterator as $name => $file) { $name = substr($name, 0, -4); $config[$name] = $this->loadFile($file); } return $config; } /** * Return configuration by key * * @param array $keys * * @return array|mixed * @throws ConfigException */ public function getData(...$keys) { // configuration is missed if (is_null($this->config)) { throw new ConfigException('System configuration is missing'); } if (!count($keys)) { return $this->config; } return Collection::get($this->config, ...$keys); } /** * Return module configuration by section * * @param string $module * @param string $section * * @return mixed * @throws \Bluz\Config\ConfigException */ public function getModuleData($module, $section = null) { if (!isset($this->modules[$module])) { $this->modules[$module] = $this->loadFile( $this->path . '/modules/' . $module . '/config.php' ); if (is_null($this->config)) { $this->init(); } if (isset($this->config["module.$module"])) { $this->modules[$module] = array_replace_recursive( $this->modules[$module], $this->config["module.$module"] ); } } if (!is_null($section)) { return $this->modules[$module][$section] ?? null; } return $this->modules[$module]; } } |