 Maslosoft Embedi API
	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  93  94  95  96  97  98  99 100 101 <?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 Maslosoft\EmbeDi\Interfaces\AdapterInterface;
/**
 * ArrayAdapter
 *
 * @author Piotr Maselkowski <pmaselkowski at gmail.com>
 */
class ArrayAdapter implements AdapterInterface
{
    private $config = [];
    /**
     * Configuration source for later use
     * Config should have keys of component id and values of config.
     * Example:
     * ```
     * [
     *      'logger' => [
     *          'class' => Monolog\Logger\Logger,
     *      ],
     *      'mangan' => [
     *          '@logger' => 'logger'
     *      ]
     * ]
     * ```
     * Attributes starting with `@` denotes that link to other
     * config component should be used. In example above, mangan field `logger`
     * will be configured with monolog logger.
     *
     * @param array $config
     */
    public function __construct($config)
    {
        $this->config = $config;
    }
    public function getConfig($class, $instanceId, $presetId = null)
    {
        if (isset($this->config[$instanceId]))
        {
            if (!empty($presetId) && empty($this->config[$instanceId][$presetId]))
            {
                // Preset is provided, but no configuration for preset found, skip
                return false;
            }
            $config = $this->config[$instanceId];
            if (!empty($presetId))
            {
                // Use preset
                $config = $config[$presetId];
            }
            if (is_object($config))
            {
                return (new YiiEmbeDi())->export($config);
            }
            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
