Maslosoft Embedi 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
<?php
/**
* This software package is licensed under `AGPL, Commercial` license[s].
*
* @package maslosoft/embedi
* @license AGPL, Commercial
*
* @copyright Copyright (c) Peter Maselkowski <pmaselkowski@gmail.com>
*
*/
namespace Maslosoft\EmbeDi\Adapters;
use Exception;
use Maslosoft\EmbeDi\Interfaces\AdapterInterface;
use Yii;
/**
* YiiAdapter
*
* @author Piotr Maselkowski <pmaselkowski at gmail.com>
*/
class YiiAdapter implements AdapterInterface
{
public function __construct()
{
if (!class_exists('Yii', false))
{
throw new Exception(sprintf('Adapter `%s` requires `Yii`', __CLASS__));
}
}
public function getConfig($class, $instanceId, $presetId = null)
{
$app = Yii::app();
if (empty($app))
{
return false;
}
$config = $app->getComponents(false);
if (!empty($config[$instanceId]))
{
if (!empty($presetId) && empty($config[$instanceId][$presetId]))
{
// Preset is provided, but no configuration for preset found, skip
return false;
}
if (!empty($presetId))
{
$config = $config[$instanceId][$presetId];
}
else
{
$config = $config[$instanceId];
}
if (is_object($config))
{
return (new YiiEmbeDi())->export($config);
}
// No class defined, skip
if (empty($config['class']))
{
return false;
}
// Direct class
if ($config['class'] == $class)
{
return $config;
}
// Subclass
$info = new \ReflectionClass($class);
if ($info->isSubclassOf($config['class']))
{
return $config;
}
// Interface
if ($info->implementsInterface($config['class']))
{
return $config;
}
}
return false;
}
}
API documentation generated by ApiGen