sebastianfeldmann /
phpbu
| 1 | <?php |
||
| 2 | namespace phpbu\App\Adapter; |
||
| 3 | |||
| 4 | use Dotenv\Dotenv as DotenvLib; |
||
| 5 | use phpbu\App\Adapter; |
||
| 6 | use phpbu\App\Configuration; |
||
| 7 | use phpbu\App\Util as AppUtil; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Dotenv Adapter |
||
| 11 | * |
||
| 12 | * @package phpbu |
||
| 13 | * @subpackage App |
||
| 14 | * @author Sebastian Feldmann <[email protected]> |
||
| 15 | * @copyright Sebastian Feldmann <[email protected]> |
||
| 16 | * @license https://opensource.org/licenses/MIT The MIT License (MIT) |
||
| 17 | * @link http://phpbu.de/ |
||
| 18 | * @since Class available since Release 4.0.0 |
||
| 19 | */ |
||
| 20 | class Dotenv implements Adapter |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * Path to the .env file |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | private $file; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Actual dot env reader |
||
| 31 | * |
||
| 32 | * @var \Dotenv\Dotenv |
||
| 33 | */ |
||
| 34 | private $dotenv; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Setup the adapter. |
||
| 38 | * |
||
| 39 | * @param array $conf |
||
| 40 | * @return void |
||
| 41 | */ |
||
| 42 | 2 | public function setup(array $conf) |
|
| 43 | { |
||
| 44 | 2 | $path = AppUtil\Arr::getValue($conf, 'file', '.env'); |
|
| 45 | 2 | $this->file = AppUtil\Path::toAbsolutePath($path, Configuration::getWorkingDirectory()); |
|
| 46 | 2 | ||
| 47 | 2 | // dotenv version 4 and higher |
|
| 48 | 2 | if (method_exists('Dotenv\\Dotenv','createImmutable')) { |
|
| 49 | $this->dotenv = DotenvLib::createImmutable(dirname($this->file), basename($this->file)); |
||
| 50 | } else { |
||
| 51 | $this->dotenv = DotenvLib::create(dirname($this->file), basename($this->file)); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 52 | } |
||
| 53 | $this->dotenv->load(); |
||
| 54 | } |
||
| 55 | |||
| 56 | 1 | /** |
|
| 57 | * Return a value for a given path. |
||
| 58 | 1 | * |
|
| 59 | * @param string $path |
||
| 60 | * @return string |
||
| 61 | */ |
||
| 62 | public function getValue(string $path) : string |
||
| 63 | { |
||
| 64 | return (string) getenv($path); |
||
| 65 | } |
||
| 66 | } |
||
| 67 |