1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This software package is licensed under `AGPL, Commercial` license[s]. |
5
|
|
|
* |
6
|
|
|
* @package maslosoft/embedi |
7
|
|
|
* @license AGPL, Commercial |
8
|
|
|
* |
9
|
|
|
* @copyright Copyright (c) Peter Maselkowski <[email protected]> |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Maslosoft\EmbeDi\Adapters; |
14
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
use Maslosoft\EmbeDi\Interfaces\AdapterInterface; |
17
|
|
|
use Yii; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* YiiAdapter |
21
|
|
|
* |
22
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
23
|
|
|
*/ |
24
|
|
|
class YiiAdapter implements AdapterInterface |
25
|
|
|
{ |
26
|
|
|
|
27
|
7 |
|
public function __construct() |
28
|
|
|
{ |
29
|
7 |
|
if (!class_exists('Yii', false)) |
30
|
7 |
|
{ |
31
|
|
|
throw new Exception(sprintf('Adapter `%s` requires `Yii`', __CLASS__)); |
32
|
|
|
} |
33
|
7 |
|
} |
34
|
|
|
|
35
|
6 |
|
public function getConfig($class, $instanceId, $presetId = null) |
36
|
|
|
{ |
37
|
6 |
|
$app = Yii::app(); |
38
|
6 |
|
if (empty($app)) |
39
|
6 |
|
{ |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
6 |
|
$config = $app->getComponents(false); |
43
|
6 |
|
if (!empty($config[$instanceId])) |
44
|
6 |
|
{ |
45
|
5 |
|
if (!empty($presetId) && empty($config[$instanceId][$presetId])) |
46
|
5 |
|
{ |
47
|
|
|
// Preset is provided, but no configuration for preset found, skip |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
5 |
|
if (!empty($presetId)) |
51
|
5 |
|
{ |
52
|
|
|
$config = $config[$instanceId][$presetId]; |
53
|
|
|
} |
54
|
|
|
else |
55
|
|
|
{ |
56
|
5 |
|
$config = $config[$instanceId]; |
57
|
|
|
} |
58
|
5 |
|
if (is_object($config)) |
59
|
5 |
|
{ |
60
|
3 |
|
return (new YiiEmbeDi())->export($config); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// No class defined, skip |
64
|
2 |
|
if (empty($config['class'])) |
65
|
2 |
|
{ |
66
|
|
|
return false; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
// Direct class |
70
|
2 |
|
if ($config['class'] == $class) |
71
|
2 |
|
{ |
72
|
2 |
|
return $config; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Subclass |
76
|
|
|
$info = new \ReflectionClass($class); |
77
|
|
|
if ($info->isSubclassOf($config['class'])) |
78
|
|
|
{ |
79
|
|
|
return $config; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Interface |
83
|
|
|
if ($info->implementsInterface($config['class'])) |
84
|
|
|
{ |
85
|
|
|
return $config; |
86
|
|
|
} |
87
|
|
|
} |
88
|
1 |
|
return false; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|