1: <?php
2:
3: namespace Charcoal\App\Module;
4:
5:
6: use \Psr\Log\LoggerAwareInterface;
7: use \Psr\Log\LoggerAwareTrait;
8:
9:
10: use \Charcoal\Factory\FactoryInterface;
11:
12:
13: use \Charcoal\Config\ConfigurableInterface;
14: use \Charcoal\Config\ConfigurableTrait;
15:
16:
17: use \Charcoal\App\AppAwareInterface;
18: use \Charcoal\App\AppAwareTrait;
19:
20: 21: 22:
23: class ModuleManager implements
24: AppAwareInterface,
25: ConfigurableInterface,
26: LoggerAwareInterface
27: {
28: use AppAwareTrait;
29: use ConfigurableTrait;
30: use LoggerAwareTrait;
31:
32: 33: 34:
35: private $modules = [];
36:
37: 38: 39:
40: private $moduleFactory;
41:
42: 43: 44: 45: 46:
47: public function __construct(array $data)
48: {
49: $this->setLogger($data['logger']);
50: $this->setConfig($data['config']);
51: $this->setApp($data['app']);
52: $this->setModuleFactory($data['module_factory']);
53: }
54:
55: 56: 57: 58:
59: protected function setModuleFactory(FactoryInterface $factory)
60: {
61: $this->moduleFactory = $factory;
62: return $this;
63: }
64:
65: 66: 67:
68: protected function moduleFactory()
69: {
70: return $this->moduleFactory;
71: }
72:
73: 74: 75: 76:
77: public function setModules(array $modules)
78: {
79: foreach ($modules as $moduleIdent => $moduleConfig) {
80: $this->addModule($moduleIdent, $moduleConfig);
81: }
82: return $this;
83: }
84:
85: 86: 87: 88: 89:
90: public function addModule($moduleIdent, array $moduleConfig)
91: {
92: $this->modules[$moduleIdent] = $moduleConfig;
93: return $this;
94: }
95:
96: 97: 98:
99: public function setupModules()
100: {
101: $modules = $this->config();
102:
103: foreach ($modules as $moduleIdent => $moduleConfig) {
104: if ($moduleConfig === false || (isset($moduleConfig['active']) && !$moduleConfig['active'])) {
105: continue;
106: }
107:
108: $module = $this->moduleFactory()->create($moduleIdent);
109: $module->setup();
110: }
111: }
112: }
113: