1 <?php
2
3 namespace Docolight\Agno;
4
5 use Yii;
6 use CWebModule;
7 use Docotory\Traits\HasFactories;
8 use Docolight\Agno\Traits\HasAutoload;
9 use Docolight\Agno\Traits\HasAssetsUrl;
10
11 12 13 14 15
16 abstract class AgnoModule extends CWebModule
17 {
18 use HasFactories, HasAssetsUrl, HasAutoload;
19
20 21 22 23 24
25 public $availableFactories = [];
26
27 28 29 30 31
32 public $bindings = [];
33
34 35 36
37 public function init()
38 {
39 parent::init();
40
41 $this->loadPsr();
42 $this->loadAutoload();
43 $this->registerBindings();
44 $this->registerFacadeAccessor();
45 $this->registerAvailableFactories();
46 }
47
48 49 50 51 52
53 public function registerBindings()
54 {
55 foreach ($this->bindings as $binding) {
56 list($definition, $concrete) = $binding;
57
58 Yii::app()->container->singleton($definition, $concrete);
59 }
60 }
61
62 63 64
65 protected function registerFacadeAccessor()
66 {
67
68
69 $that = $this;
70
71 Yii::app()->container->singleton($this->getFacadeAccessor(), function ($container) use ($that) {
72 return $that;
73 });
74
75 }
76
77 78 79
80 protected function registerAvailableFactories()
81 {
82 if ($this->availableFactories) {
83
84 foreach ($this->availableFactories as $alias => $factory) {
85 Yii::app()->container->singleton($alias, $factory);
86 }
87
88
89 $this->registerFactories($this->availableFactories);
90 }
91 }
92
93 94 95 96 97
98 protected function getFacadeAccessor()
99 {
100 return $this->getId();
101 }
102
103 104 105 106 107 108 109
110 public function __get($type)
111 {
112 if ($this->hasFactory($type)) {
113 return $this->factory($type);
114 }
115
116 return parent::__get($type);
117 }
118
119 120 121 122 123 124 125 126
127 public function __call($name, $parameters)
128 {
129 if ($this->hasFactory('base')) {
130 $base = $this->factory('base');
131
132 if (method_exists($base, $name)) {
133 return call_user_func_array([$base, $name], $parameters);
134 }
135 }
136 }
137 }
138