Source of file Config.php
Size: 6,301 Bytes - Last Modified: 2021-08-28T08:05:12+00:00
C:/Users/MAKS/Code/_PROJECTS/amqp-agent/src/Config.php
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 
                                Covered by 23 test(s):
                            7677 
 
                                Covered by 23 test(s):
                            78 
 
                                Covered by 1 test(s):
                            79 
 
                                Covered by 1 test(s):
                            80818283 
 
                                Covered by 23 test(s):
                            84 
 
                                Covered by 23 test(s):
                            8586 
 
                                Covered by 23 test(s):
                            87 
 
                                Covered by 23 test(s):
                            888990919293949596 
 
                                Covered by 5 test(s):
                            979899100101102103104105106107 
 
                                Covered by 1 test(s):
                            108 
 
                                Covered by 1 test(s):
                            109110111112113114115116 
 
                                Covered by 1 test(s):
                            117118119120121122123124125126 
 
                                Covered by 23 test(s):
                            127128 
 
                                Covered by 23 test(s):
                            129 
 
                                Covered by 23 test(s):
                            130 
 
                                Covered by 4 test(s):
                            131132133134 
 
                                Covered by 23 test(s):
                            135 
 
                                Covered by 23 test(s):
                            136137138139140141142143144145 
 
                                Covered by 1 test(s):
                            146147 
 
                                Covered by 1 test(s):
                            148149150151152153154155156157158 
 
                                Covered by 2 test(s):
                            159160 
 
                                Covered by 2 test(s):
                            161162163164165166167168169170171172 
 
                                Covered by 1 test(s):
                            173174 
 
                                Covered by 1 test(s):
                            175176177178179180181182183 
 
                                Covered by 1 test(s):
                            184185186187188189190191192 
 
                                Covered by 3 test(s):
                            193194195196197198199200201202 
 
                                Covered by 4 test(s):
                            203204 
 
                                Covered by 4 test(s):
                            205206 
 
                                Covered by 4 test(s):
                            207208209210211212213214215 
 
                                Covered by 2 test(s):
                            216217218219220221222223224225226227 
 
                                Covered by 2 test(s):
                            228 
 
                                Covered by 1 test(s):
                            229230 
 
                                Covered by 1 test(s):
                            231 
 
                                Covered by 1 test(s):
                            232 
 
                                Covered by 1 test(s):
                            233 
 
                                Covered by 1 test(s):
                            234 
 
                                Covered by 1 test(s):
                            235236237238239 
 
                                Covered by 1 test(s):
                            240241242 
 | <?php/** * @author Marwan Al-Soltany <MarwanAlsoltany@gmail.com> * @copyright Marwan Al-Soltany 2020 * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */declare(strict_types=1); namespace MAKS\AmqpAgent; use Exception; use MAKS\AmqpAgent\Helper\ArrayProxy; use MAKS\AmqpAgent\Exception\ConfigFileNotFoundException; /** * A class that turns the configuration file into an object. * * Example: * ``` * $config = new Config('path/to/some/config-file.php'); // specific config * $config = new Config(); // default config * ``` * * @since 1.0.0 * @property array $connectionOptions * @property array $channelOptions * @property array $queueOptions * @property array $exchangeOptions * @property array $bindOptions * @property array $qosOptions * @property array $waitOptions * @property array $messageOptions * @property array $publishOptions * @property array $consumeOptions * @property array $rpcConnectionOptions * @property string $rpcQueueName */final class Config {/**      * The default name of the configuration file.     * @var string     */public const DEFAULT_CONFIG_FILE_NAME = 'maks-amqp-agent-config'; /**      * The default path of the configuration file.     * @var string     */public const DEFAULT_CONFIG_FILE_PATH = __DIR__ . DIRECTORY_SEPARATOR . 'Config' . DIRECTORY_SEPARATOR . self::DEFAULT_CONFIG_FILE_NAME . '.php'; /**      * The multidimensional configuration array.     * @var array     */private $config; /**      * Configuration file path.     * @var string     */private $configPath; /**      * Config object constructor.     * @param string|null $configPath [optional] The path to AMQP Agent configuration file.     * @throws ConfigFileNotFoundException     */public function __construct(?string $configPath = null) { $configFile = realpath($configPath ?? self::DEFAULT_CONFIG_FILE_PATH); if (!$configFile || !file_exists($configFile)) { throw new ConfigFileNotFoundException( "AMQP Agent configuration file cloud not be found, check if the given path \"{$configPath}\" exists." ); } $this->config = include($configFile); $this->configPath = $configFile; $this->repair(); } /**      * Gets the the given key from the configuration array via public property access notation.     * @param string $key     * @return mixed     */public function __get(string $key) { return $this->config[$key]; } /**      * Sets the the given key in the configuration array via public property assignment notation.     * @param string $key     * @param mixed $value     * @return void     */public function __set(string $key, $value) { $this->config[$key] = $value; } /**      * Returns config file path if the object was casted to a string.     * @return string     */public function __toString() { return $this->configPath; } /**      * Repairs the config array if first-level of the passed array does not have all keys.     * @return void     */private function repair(): void { $config = require(self::DEFAULT_CONFIG_FILE_PATH); foreach ($config as $key => $value) { if (!array_key_exists($key, $this->config)) { $this->config[$key] = []; } } unset($config); } /**      * Checks whether a value exists in the configuration array via dot-notation representation.     * @since 1.2.2     * @param string $key The dotted key representation.     * @return bool True if key is set otherwise false.     */public function has(string $key): bool { $value = ArrayProxy::getArrayValueByKey($this->config, $key, null); return isset($value); } /**      * Gets a value of a key from the configuration array via dot-notation representation.     * @since 1.2.2     * @param string $key The dotted key representation.     * @return mixed The requested value or null.     */public function get(string $key) { $value = ArrayProxy::getArrayValueByKey($this->config, $key); return $value; } /**      * Sets a value of a key from the configuration array via dot-notation representation.     * @since 1.2.2     * @param string $key The dotted key representation.     * @param mixed $value The value to set.     * @return self     */public function set(string $key, $value) { ArrayProxy::setArrayValueByKey($this->config, $key, $value); return $this; } /**      * Returns the default configuration array.     * @return array     */public function getDefaultConfig(): array { return include(self::DEFAULT_CONFIG_FILE_PATH); } /**      * Returns the current configuration array.     * @return array     */public function getConfig(): array { return $this->config; } /**      * Sets a new configuration array to be used instead of the current.     * @param array $config     * @return self     */public function setConfig(array $config) { $this->config = $config; $this->repair(); return $this; } /**      * Returns the path of the configuration file.     * @return string     */public function getConfigPath(): string { return $this->configPath; } /**      * Sets the path of the configuration file and rebuilds the internal state of the object.     * @param string $configPath     * @return self     * @throws ConfigFileNotFoundException     */public function setConfigPath(string $configPath) { try { $this->config = include($configPath); $this->configPath = $configPath; $this->repair(); } catch (Exception $error) { throw new ConfigFileNotFoundException( "Something went wrong when trying to include the file and rebuild the configuration, check if the given path \"{$configPath}\" exists.", (int)$error->getCode(), $error ); } return $this; } } |