Maslosoft Cli Shared API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/cli-shared
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*/
namespace Maslosoft\Cli\Shared;
use Maslosoft\Cli\Shared\Adapters\Internal\PhpRuntimeAdapter;
use Maslosoft\Cli\Shared\Adapters\YamlAdapter;
use Maslosoft\Cli\Shared\Interfaces\ConfigAdapterInterface;
/**
* DRAFT
* This should read config.yml on cli, and write it to `generated` folder as php file.
* From php this should only read php config. Should allow uniform property-like access
*/
class ConfigReader
{
/**
*
* @var ConfigAdapterInterface
*/
private $_adapter = null;
private $_phpConfig;
private $_srcConfig;
public function __construct($basename, ConfigAdapterInterface $adapter = null)
{
$this->_basename = $basename;
$this->_adapter = $adapter;
$this->_srcConfig = null;
if (php_sapi_name() == 'cli')
{
$this->_srcConfig = $this->getAdapter()->read($this->_basename);
}
if (!empty($this->_srcConfig))
{
// Source config in found, write it to php config for later use and better performance
$this->_phpConfig = $this->_srcConfig;
}
else
{
$this->_phpConfig = (new PhpRuntimeAdapter($this))->read($this->_basename);
}
}
/**
* Get confguration as php array
* @return mixed[]
*/
public function toArray()
{
if (empty($this->_phpConfig))
{
return [];
}
return (array) $this->_phpConfig;
}
public function __destruct()
{
if (!empty($this->_srcConfig))
{
(new PhpRuntimeAdapter($this))->write($this->_basename, $this->_phpConfig);
}
}
/**
*
*/
public function getAdapter()
{
if (null === $this->_adapter)
{
$this->_adapter = new YamlAdapter();
}
return $this->_adapter;
}
public function setAdapter(ConfigAdapterInterface $adapter)
{
$this->_adapter = $adapter;
}
}
API documentation generated by ApiGen