File size: 3.24Kb
<?php
namespace {
class Phpfox_Config_Container
{
/**
* @var array
*/
private $bag = [];
public function __construct()
{
$this->bag = include PHPFOX_DIR .'include/package.config.php';
}
/**
* @param string $section
* @param string $item
*
* @return mixed|null
*/
public function get($section, $item = null)
{
if (!isset($this->bag[$section])) {
return null;
}
if (!$item) {
return $this->bag[$section];
}
if (!isset($this->bag[$section][$item])) {
return null;
}
return $this->bag[$section][$item];
}
public function merge($data)
{
foreach ($data as $k => $v) {
if (!isset($this->bag[$k])) {
$this->bag[$k] = [];
}
if (!is_array($v)) {
$this->bag[$k] = $v;
} else {
$this->bag[$k] = array_merge($this->bag[$k], $v);
}
}
}
}
class Phpfox_Service_Container
{
/**
* @var array
*/
private $bag = [];
const SECTION = 'services';
/**
* @param string $key
*
* @return mixed|null
*/
public function get($key)
{
$key = str_replace('phpfox.', '', str_replace('_', '.',
strtolower($key)));
return isset($this->bag[$key]) ? $this->bag[$key]
: $this->bag[$key] = $this->build($key);
}
/**
* @param string $key
*
* @return mixed|null
* @throws \InvalidArgumentException
*/
public function build($key)
{
$scheme = Phpfox::getConfig(self::SECTION, $key);
$class = null;
if(!$scheme){
$scheme = $this->search($key);
}
if(is_string($scheme)){
return new $scheme;
}
if(is_array($scheme)){
$class = array_shift($scheme);
}
if($class){
if(!class_exists($class)){
throw new \InvalidArgumentException("Unexpected class {$class}");
}
return call_user_func_array([new $class,
'factory'], $scheme);
}
if(!$class){
throw new \InvalidArgumentException("Unexpected service {$key}");
}
if(!class_exists($class)){
throw new \InvalidArgumentException("Unexpected class {$class}");
}
return (new \ReflectionClass($class))->newInstanceArgs($scheme);
}
/**
* @param $key
* @return array|string|null
*/
public function search($key)
{
$candidates = [];
// is phpfox.
if(substr($key, 0,7) == 'phpfox.'){
$candidates[] = substr($key, 7);
}
}
public function __sleep()
{
return ['section'];
}
}
}