|
| 1 | +<?php |
| 2 | +namespace phpbu\App\Adapter; |
| 3 | + |
| 4 | +use phpbu\App\Adapter; |
| 5 | +use phpbu\App\Configuration; |
| 6 | +use phpbu\App\Exception; |
| 7 | +use phpbu\App\Util as AppUtil; |
| 8 | + |
| 9 | +/** |
| 10 | + * PHPVars Adapter |
| 11 | + * |
| 12 | + * @package phpbu |
| 13 | + * @subpackage App |
| 14 | + * @author Sebastian Feldmann <sebastian@phpbu.de> |
| 15 | + * @copyright Sebastian Feldmann <sebastian@phpbu.de> |
| 16 | + * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
| 17 | + * @link http://phpbu.de/ |
| 18 | + * @since Class available since Release 4.0.1 |
| 19 | + */ |
| 20 | +class PHPVars implements Adapter |
| 21 | +{ |
| 22 | + /** |
| 23 | + * Path to the config file |
| 24 | + * |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + private $file; |
| 28 | + |
| 29 | + /** |
| 30 | + * Configuration |
| 31 | + * |
| 32 | + * @var array |
| 33 | + */ |
| 34 | + private $config; |
| 35 | + |
| 36 | + /** |
| 37 | + * Setup the adapter. |
| 38 | + * |
| 39 | + * @param array $conf |
| 40 | + * @return void |
| 41 | + * @throws \phpbu\App\Exception |
| 42 | + */ |
| 43 | + public function setup(array $conf) |
| 44 | + { |
| 45 | + $path = AppUtil\Arr::getValue($conf, 'file', 'config.php'); |
| 46 | + $this->file = AppUtil\Path::toAbsolutePath($path, Configuration::getWorkingDirectory()); |
| 47 | + $this->load(); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Load config file to local file. |
| 52 | + * |
| 53 | + * @throws \phpbu\App\Exception |
| 54 | + */ |
| 55 | + private function load() |
| 56 | + { |
| 57 | + if (!file_exists($this->file)) { |
| 58 | + throw new Exception('config file not found'); |
| 59 | + } |
| 60 | + $this->config = require $this->file; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Return a value for a given path. |
| 65 | + * |
| 66 | + * @param string $path |
| 67 | + * @return string |
| 68 | + * @throws \phpbu\App\Exception |
| 69 | + */ |
| 70 | + public function getValue(string $path) : string |
| 71 | + { |
| 72 | + return (string) constant($path); |
| 73 | + } |
| 74 | +} |
0 commit comments