@@ -4,10 +4,10 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface ConfigReaderInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Read the contents of the Config file. |
|
| 9 | - * |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - public function read(string $filename): array; |
|
| 7 | + /** |
|
| 8 | + * Read the contents of the Config file. |
|
| 9 | + * |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + public function read(string $filename): array; |
|
| 13 | 13 | } |
@@ -6,10 +6,10 @@ |
||
| 6 | 6 | |
| 7 | 7 | interface ConfigurableInterface |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Set the ConfigFactory instance. |
|
| 11 | - * |
|
| 12 | - * @param \FigTree\Config\ConfigFactory $configFactory |
|
| 13 | - */ |
|
| 14 | - public function setConfigFactory(ConfigFactory $configFactory); |
|
| 9 | + /** |
|
| 10 | + * Set the ConfigFactory instance. |
|
| 11 | + * |
|
| 12 | + * @param \FigTree\Config\ConfigFactory $configFactory |
|
| 13 | + */ |
|
| 14 | + public function setConfigFactory(ConfigFactory $configFactory); |
|
| 15 | 15 | } |
@@ -8,34 +8,34 @@ |
||
| 8 | 8 | |
| 9 | 9 | interface ConfigInterface extends ArrayAccess, JsonSerializable, Stringable |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Get the name of the underlying Config file. |
|
| 13 | - * |
|
| 14 | - * @return string |
|
| 15 | - */ |
|
| 16 | - public function getFileName(): string; |
|
| 11 | + /** |
|
| 12 | + * Get the name of the underlying Config file. |
|
| 13 | + * |
|
| 14 | + * @return string |
|
| 15 | + */ |
|
| 16 | + public function getFileName(): string; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * Convert the object into JSON. |
|
| 20 | - * |
|
| 21 | - * @param integer $options |
|
| 22 | - * @param integer $depth |
|
| 23 | - * |
|
| 24 | - * @return string |
|
| 25 | - */ |
|
| 26 | - public function toJson(int $options = 0, int $depth = 512): string; |
|
| 18 | + /** |
|
| 19 | + * Convert the object into JSON. |
|
| 20 | + * |
|
| 21 | + * @param integer $options |
|
| 22 | + * @param integer $depth |
|
| 23 | + * |
|
| 24 | + * @return string |
|
| 25 | + */ |
|
| 26 | + public function toJson(int $options = 0, int $depth = 512): string; |
|
| 27 | 27 | |
| 28 | - /** |
|
| 29 | - * Convert the object into a string. |
|
| 30 | - * |
|
| 31 | - * @return string |
|
| 32 | - */ |
|
| 33 | - public function toString(): string; |
|
| 28 | + /** |
|
| 29 | + * Convert the object into a string. |
|
| 30 | + * |
|
| 31 | + * @return string |
|
| 32 | + */ |
|
| 33 | + public function toString(): string; |
|
| 34 | 34 | |
| 35 | - /** |
|
| 36 | - * Create a ConfigReader instance. |
|
| 37 | - * |
|
| 38 | - * @return \FigTree\Config\Contracts\ConfigReaderInterface |
|
| 39 | - */ |
|
| 40 | - public function createReader(): ConfigReaderInterface; |
|
| 35 | + /** |
|
| 36 | + * Create a ConfigReader instance. |
|
| 37 | + * |
|
| 38 | + * @return \FigTree\Config\Contracts\ConfigReaderInterface |
|
| 39 | + */ |
|
| 40 | + public function createReader(): ConfigReaderInterface; |
|
| 41 | 41 | } |
@@ -4,38 +4,38 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface ConfigFactoryInterface |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Get the directories to search for a Config file. |
|
| 9 | - * |
|
| 10 | - * @return array |
|
| 11 | - */ |
|
| 12 | - public function getDirectories(): array; |
|
| 7 | + /** |
|
| 8 | + * Get the directories to search for a Config file. |
|
| 9 | + * |
|
| 10 | + * @return array |
|
| 11 | + */ |
|
| 12 | + public function getDirectories(): array; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * Add a directory to search for a Config file. |
|
| 16 | - * |
|
| 17 | - * @param string $directory |
|
| 18 | - * |
|
| 19 | - * @return $this |
|
| 20 | - */ |
|
| 21 | - public function addDirectory(string $directory): ConfigFactoryInterface; |
|
| 14 | + /** |
|
| 15 | + * Add a directory to search for a Config file. |
|
| 16 | + * |
|
| 17 | + * @param string $directory |
|
| 18 | + * |
|
| 19 | + * @return $this |
|
| 20 | + */ |
|
| 21 | + public function addDirectory(string $directory): ConfigFactoryInterface; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Search for a Config file from the list of applicable directories. |
|
| 25 | - * Returns null if it could not be found. |
|
| 26 | - * |
|
| 27 | - * @param string $fileName |
|
| 28 | - * |
|
| 29 | - * @return \FigTree\Config\Contracts\ConfigInterface|null |
|
| 30 | - */ |
|
| 31 | - public function get(string $fileName): ?ConfigInterface; |
|
| 23 | + /** |
|
| 24 | + * Search for a Config file from the list of applicable directories. |
|
| 25 | + * Returns null if it could not be found. |
|
| 26 | + * |
|
| 27 | + * @param string $fileName |
|
| 28 | + * |
|
| 29 | + * @return \FigTree\Config\Contracts\ConfigInterface|null |
|
| 30 | + */ |
|
| 31 | + public function get(string $fileName): ?ConfigInterface; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * Create a Config instance. |
|
| 35 | - * |
|
| 36 | - * @param string $path |
|
| 37 | - * |
|
| 38 | - * @return \FigTree\Config\Contracts\ConfigInterface |
|
| 39 | - */ |
|
| 40 | - public function create(string $path): ConfigInterface; |
|
| 33 | + /** |
|
| 34 | + * Create a Config instance. |
|
| 35 | + * |
|
| 36 | + * @param string $path |
|
| 37 | + * |
|
| 38 | + * @return \FigTree\Config\Contracts\ConfigInterface |
|
| 39 | + */ |
|
| 40 | + public function create(string $path): ConfigInterface; |
|
| 41 | 41 | } |
@@ -6,8 +6,8 @@ discard block |
||
| 6 | 6 | use RuntimeException; |
| 7 | 7 | use FigTree\Exceptions\Contracts\SevereExceptionInterface; |
| 8 | 8 | use FigTree\Exceptions\Concerns\{ |
| 9 | - HasSeverity, |
|
| 10 | - SetsLocation |
|
| 9 | + HasSeverity, |
|
| 10 | + SetsLocation |
|
| 11 | 11 | }; |
| 12 | 12 | |
| 13 | 13 | /** |
@@ -15,22 +15,22 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class InvalidConfigFileException extends RuntimeException implements SevereExceptionInterface |
| 17 | 17 | { |
| 18 | - use HasSeverity; |
|
| 19 | - use SetsLocation; |
|
| 18 | + use HasSeverity; |
|
| 19 | + use SetsLocation; |
|
| 20 | 20 | |
| 21 | - protected int $severity = E_ERROR; |
|
| 21 | + protected int $severity = E_ERROR; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Exception thrown when a Config file is invalid or fails to parse. |
|
| 25 | - * |
|
| 26 | - * @param string $filename The path to the Config file. |
|
| 27 | - * @param int $code The Exception code. |
|
| 28 | - * @param \Throwable $previous The previous throwable used for the exception chaining. |
|
| 29 | - */ |
|
| 30 | - public function __construct(string $filename, int $code = 0, Throwable $previous = null) |
|
| 31 | - { |
|
| 32 | - $message = sprintf('Config file %s could not be parsed.', $filename); |
|
| 23 | + /** |
|
| 24 | + * Exception thrown when a Config file is invalid or fails to parse. |
|
| 25 | + * |
|
| 26 | + * @param string $filename The path to the Config file. |
|
| 27 | + * @param int $code The Exception code. |
|
| 28 | + * @param \Throwable $previous The previous throwable used for the exception chaining. |
|
| 29 | + */ |
|
| 30 | + public function __construct(string $filename, int $code = 0, Throwable $previous = null) |
|
| 31 | + { |
|
| 32 | + $message = sprintf('Config file %s could not be parsed.', $filename); |
|
| 33 | 33 | |
| 34 | - parent::__construct($message, $code, $previous); |
|
| 35 | - } |
|
| 34 | + parent::__construct($message, $code, $previous); |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -6,8 +6,8 @@ discard block |
||
| 6 | 6 | use RuntimeException; |
| 7 | 7 | use FigTree\Exceptions\Contracts\SevereExceptionInterface; |
| 8 | 8 | use FigTree\Exceptions\Concerns\{ |
| 9 | - HasSeverity, |
|
| 10 | - SetsLocation |
|
| 9 | + HasSeverity, |
|
| 10 | + SetsLocation |
|
| 11 | 11 | }; |
| 12 | 12 | |
| 13 | 13 | /** |
@@ -15,22 +15,22 @@ discard block |
||
| 15 | 15 | */ |
| 16 | 16 | class ReadOnlyException extends RuntimeException implements SevereExceptionInterface |
| 17 | 17 | { |
| 18 | - use HasSeverity; |
|
| 19 | - use SetsLocation; |
|
| 18 | + use HasSeverity; |
|
| 19 | + use SetsLocation; |
|
| 20 | 20 | |
| 21 | - protected int $severity = E_ERROR; |
|
| 21 | + protected int $severity = E_ERROR; |
|
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Exception thrown when attempting to modify a Config value. |
|
| 25 | - * |
|
| 26 | - * @param string|int $property The key of the value for which an attempt was made to modify the value. |
|
| 27 | - * @param int $code The Exception code. |
|
| 28 | - * @param \Throwable $previous The previous throwable used for the exception chaining. |
|
| 29 | - */ |
|
| 30 | - public function __construct($property, int $code = 0, Throwable $previous = null) |
|
| 31 | - { |
|
| 32 | - $message = sprintf('Cannot modify config property %s.', $property); |
|
| 23 | + /** |
|
| 24 | + * Exception thrown when attempting to modify a Config value. |
|
| 25 | + * |
|
| 26 | + * @param string|int $property The key of the value for which an attempt was made to modify the value. |
|
| 27 | + * @param int $code The Exception code. |
|
| 28 | + * @param \Throwable $previous The previous throwable used for the exception chaining. |
|
| 29 | + */ |
|
| 30 | + public function __construct($property, int $code = 0, Throwable $previous = null) |
|
| 31 | + { |
|
| 32 | + $message = sprintf('Cannot modify config property %s.', $property); |
|
| 33 | 33 | |
| 34 | - parent::__construct($message, $code, $previous); |
|
| 35 | - } |
|
| 34 | + parent::__construct($message, $code, $previous); |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -6,15 +6,15 @@ |
||
| 6 | 6 | |
| 7 | 7 | class ConfigFactory extends AbstractConfigFactory |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Create a Config instance. |
|
| 11 | - * |
|
| 12 | - * @param string $path |
|
| 13 | - * |
|
| 14 | - * @return \FigTree\Config\Contracts\ConfigInterface |
|
| 15 | - */ |
|
| 16 | - public function create(string $path): ConfigInterface |
|
| 17 | - { |
|
| 18 | - return new Config($path); |
|
| 19 | - } |
|
| 9 | + /** |
|
| 10 | + * Create a Config instance. |
|
| 11 | + * |
|
| 12 | + * @param string $path |
|
| 13 | + * |
|
| 14 | + * @return \FigTree\Config\Contracts\ConfigInterface |
|
| 15 | + */ |
|
| 16 | + public function create(string $path): ConfigInterface |
|
| 17 | + { |
|
| 18 | + return new Config($path); |
|
| 19 | + } |
|
| 20 | 20 | } |
@@ -3,111 +3,111 @@ |
||
| 3 | 3 | namespace FigTree\Config; |
| 4 | 4 | |
| 5 | 5 | use FigTree\Exceptions\{ |
| 6 | - InvalidDirectoryException, |
|
| 7 | - InvalidPathException, |
|
| 8 | - UnreadablePathException, |
|
| 6 | + InvalidDirectoryException, |
|
| 7 | + InvalidPathException, |
|
| 8 | + UnreadablePathException, |
|
| 9 | 9 | }; |
| 10 | 10 | use FigTree\Config\Contracts\{ |
| 11 | - ConfigInterface, |
|
| 12 | - ConfigFactoryInterface, |
|
| 11 | + ConfigInterface, |
|
| 12 | + ConfigFactoryInterface, |
|
| 13 | 13 | }; |
| 14 | 14 | |
| 15 | 15 | abstract class AbstractConfigFactory implements ConfigFactoryInterface |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Config file directories. |
|
| 19 | - * |
|
| 20 | - * @var array |
|
| 21 | - */ |
|
| 22 | - protected $directories = []; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Cached Config objects. |
|
| 26 | - * |
|
| 27 | - * @var array |
|
| 28 | - */ |
|
| 29 | - protected $configs = []; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Get the directories to search for a Config file. |
|
| 33 | - * |
|
| 34 | - * @return array |
|
| 35 | - */ |
|
| 36 | - public function getDirectories(): array |
|
| 37 | - { |
|
| 38 | - return $this->directories; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Add a directory to search for a Config file. |
|
| 43 | - * |
|
| 44 | - * @param string $directory |
|
| 45 | - * |
|
| 46 | - * @return $this |
|
| 47 | - */ |
|
| 48 | - public function addDirectory(string $directory): ConfigFactory |
|
| 49 | - { |
|
| 50 | - $dir = $this->resolveDirectory($directory); |
|
| 51 | - |
|
| 52 | - $this->directories[] = $directory; |
|
| 53 | - |
|
| 54 | - return $this; |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Search for a Config file from the list of applicable directories. |
|
| 59 | - * Returns null if it could not be found. |
|
| 60 | - * |
|
| 61 | - * @param string $fileName |
|
| 62 | - * |
|
| 63 | - * @return \FigTree\Config\Contracts\ConfigInterface|null |
|
| 64 | - */ |
|
| 65 | - public function get(string $fileName): ?ConfigInterface |
|
| 66 | - { |
|
| 67 | - if (key_exists($fileName, $this->configs)) { |
|
| 68 | - return $this->configs[$fileName]; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - foreach ($this->directories as $directory) { |
|
| 72 | - $fullPath = $this->resolveFile($directory, $fileName . '.php'); |
|
| 73 | - |
|
| 74 | - if (!empty($fullPath)) { |
|
| 75 | - return ($this->configs[$fileName] = $this->create($fullPath)); |
|
| 76 | - } |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - return null; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - protected function resolveDirectory(string $directory): string |
|
| 83 | - { |
|
| 84 | - $dir = realpath($directory); |
|
| 85 | - |
|
| 86 | - if (empty($dir)) { |
|
| 87 | - throw new InvalidPathException($directory); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - if (!is_dir($dir)) { |
|
| 91 | - throw new InvalidDirectoryException($directory); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if (!is_readable($dir)) { |
|
| 95 | - throw new UnreadablePathException(sprintf('Directory %s is not readable.', $dir)); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - return $dir; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - protected function resolveFile(string $directory, string $file) |
|
| 102 | - { |
|
| 103 | - $prefix = $directory . DIRECTORY_SEPARATOR; |
|
| 104 | - |
|
| 105 | - $path = realpath($prefix . $file); |
|
| 106 | - |
|
| 107 | - if (empty($path) || !is_file($path) || !is_readable($path)) { |
|
| 108 | - return null; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return $path; |
|
| 112 | - } |
|
| 17 | + /** |
|
| 18 | + * Config file directories. |
|
| 19 | + * |
|
| 20 | + * @var array |
|
| 21 | + */ |
|
| 22 | + protected $directories = []; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Cached Config objects. |
|
| 26 | + * |
|
| 27 | + * @var array |
|
| 28 | + */ |
|
| 29 | + protected $configs = []; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Get the directories to search for a Config file. |
|
| 33 | + * |
|
| 34 | + * @return array |
|
| 35 | + */ |
|
| 36 | + public function getDirectories(): array |
|
| 37 | + { |
|
| 38 | + return $this->directories; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Add a directory to search for a Config file. |
|
| 43 | + * |
|
| 44 | + * @param string $directory |
|
| 45 | + * |
|
| 46 | + * @return $this |
|
| 47 | + */ |
|
| 48 | + public function addDirectory(string $directory): ConfigFactory |
|
| 49 | + { |
|
| 50 | + $dir = $this->resolveDirectory($directory); |
|
| 51 | + |
|
| 52 | + $this->directories[] = $directory; |
|
| 53 | + |
|
| 54 | + return $this; |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Search for a Config file from the list of applicable directories. |
|
| 59 | + * Returns null if it could not be found. |
|
| 60 | + * |
|
| 61 | + * @param string $fileName |
|
| 62 | + * |
|
| 63 | + * @return \FigTree\Config\Contracts\ConfigInterface|null |
|
| 64 | + */ |
|
| 65 | + public function get(string $fileName): ?ConfigInterface |
|
| 66 | + { |
|
| 67 | + if (key_exists($fileName, $this->configs)) { |
|
| 68 | + return $this->configs[$fileName]; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + foreach ($this->directories as $directory) { |
|
| 72 | + $fullPath = $this->resolveFile($directory, $fileName . '.php'); |
|
| 73 | + |
|
| 74 | + if (!empty($fullPath)) { |
|
| 75 | + return ($this->configs[$fileName] = $this->create($fullPath)); |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + return null; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + protected function resolveDirectory(string $directory): string |
|
| 83 | + { |
|
| 84 | + $dir = realpath($directory); |
|
| 85 | + |
|
| 86 | + if (empty($dir)) { |
|
| 87 | + throw new InvalidPathException($directory); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + if (!is_dir($dir)) { |
|
| 91 | + throw new InvalidDirectoryException($directory); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if (!is_readable($dir)) { |
|
| 95 | + throw new UnreadablePathException(sprintf('Directory %s is not readable.', $dir)); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + return $dir; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + protected function resolveFile(string $directory, string $file) |
|
| 102 | + { |
|
| 103 | + $prefix = $directory . DIRECTORY_SEPARATOR; |
|
| 104 | + |
|
| 105 | + $path = realpath($prefix . $file); |
|
| 106 | + |
|
| 107 | + if (empty($path) || !is_file($path) || !is_readable($path)) { |
|
| 108 | + return null; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return $path; |
|
| 112 | + } |
|
| 113 | 113 | } |
@@ -69,7 +69,7 @@ discard block |
||
| 69 | 69 | } |
| 70 | 70 | |
| 71 | 71 | foreach ($this->directories as $directory) { |
| 72 | - $fullPath = $this->resolveFile($directory, $fileName . '.php'); |
|
| 72 | + $fullPath = $this->resolveFile($directory, $fileName.'.php'); |
|
| 73 | 73 | |
| 74 | 74 | if (!empty($fullPath)) { |
| 75 | 75 | return ($this->configs[$fileName] = $this->create($fullPath)); |
@@ -100,9 +100,9 @@ discard block |
||
| 100 | 100 | |
| 101 | 101 | protected function resolveFile(string $directory, string $file) |
| 102 | 102 | { |
| 103 | - $prefix = $directory . DIRECTORY_SEPARATOR; |
|
| 103 | + $prefix = $directory.DIRECTORY_SEPARATOR; |
|
| 104 | 104 | |
| 105 | - $path = realpath($prefix . $file); |
|
| 105 | + $path = realpath($prefix.$file); |
|
| 106 | 106 | |
| 107 | 107 | if (empty($path) || !is_file($path) || !is_readable($path)) { |
| 108 | 108 | return null; |
@@ -3,28 +3,28 @@ |
||
| 3 | 3 | namespace FigTree\Config; |
| 4 | 4 | |
| 5 | 5 | use FigTree\Config\Contracts\{ |
| 6 | - ConfigReaderInterface, |
|
| 6 | + ConfigReaderInterface, |
|
| 7 | 7 | }; |
| 8 | 8 | |
| 9 | 9 | class Config extends AbstractConfig |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Construct an instance of Config. |
|
| 13 | - * |
|
| 14 | - * @param string $fileName |
|
| 15 | - */ |
|
| 16 | - public function __construct(string $fileName) |
|
| 17 | - { |
|
| 18 | - $this->setFileName($fileName); |
|
| 19 | - } |
|
| 11 | + /** |
|
| 12 | + * Construct an instance of Config. |
|
| 13 | + * |
|
| 14 | + * @param string $fileName |
|
| 15 | + */ |
|
| 16 | + public function __construct(string $fileName) |
|
| 17 | + { |
|
| 18 | + $this->setFileName($fileName); |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Create a ConfigReader instance. |
|
| 23 | - * |
|
| 24 | - * @return \FigTree\Config\Contracts\ConfigReaderInterface |
|
| 25 | - */ |
|
| 26 | - public function createReader(): ConfigReaderInterface |
|
| 27 | - { |
|
| 28 | - return new ConfigReader(); |
|
| 29 | - } |
|
| 21 | + /** |
|
| 22 | + * Create a ConfigReader instance. |
|
| 23 | + * |
|
| 24 | + * @return \FigTree\Config\Contracts\ConfigReaderInterface |
|
| 25 | + */ |
|
| 26 | + public function createReader(): ConfigReaderInterface |
|
| 27 | + { |
|
| 28 | + return new ConfigReader(); |
|
| 29 | + } |
|
| 30 | 30 | } |