1: <?php
2:
3: namespace Charcoal\App\Module;
4:
5: use \InvalidArgumentException;
6:
7: // Dependencies from PSR-7 (HTTP Messaging)
8: use \Psr\Http\Message\RequestInterface;
9: use \Psr\Http\Message\ResponseInterface;
10:
11: // Dependencies from PSR-3 (Logger)
12: use \Psr\Log\LoggerAwareInterface;
13: use \Psr\Log\LoggerAwareTrait;
14:
15: // Dependency from 'charcoal-config'
16: use \Charcoal\Config\ConfigurableInterface;
17:
18: // Intra-module ('charcoal-app') dependencies
19: use \Charcoal\App\AppAwareInterface;
20: use \Charcoal\App\AppAwareTrait;
21: use \Charcoal\App\App;
22: use \Charcoal\App\AppConfig;
23: use \Charcoal\App\AppInterface;
24: use \Charcoal\App\Module\ModuleManager;
25: use \Charcoal\App\Route\RouteManager;
26:
27: /**
28: *
29: */
30: abstract class AbstractModule implements
31: AppAwareInterface,
32: ConfigurableInterface,
33: LoggerAwareInterface,
34: ModuleInterface
35: {
36: use AppAwareTrait;
37: use LoggerAwareTrait;
38:
39: /**
40: * @var ConfigInterface $config
41: */
42: protected $config;
43:
44: /**
45: * @var RouteManager
46: */
47: protected $routeManager;
48:
49: /**
50: * Return a new AbstractModule object.
51: *
52: * @param array $data Module dependencies.
53: */
54: public function __construct(array $data)
55: {
56: $this->setLogger($data['logger']);
57:
58: if (!isset($data['app'])) {
59: $data['app'] = App::instance();
60: }
61: $this->setApp($data['app']);
62: }
63:
64: /**
65: * Set the module's config
66: *
67: * @param \Charcoal\Config\ConfigInterface|array $config The module configuration.
68: * @return AbstractModule
69: */
70: public function setConfig($config)
71: {
72: $this->config = $config;
73:
74: return $this;
75: }
76:
77: /**
78: * Retrieve the module's configuration container, or one of its entry.
79: *
80: * If a key is provided, return the configuration key value instead of the full object.
81: *
82: * @param string $key Optional. If provided, the config key value will be returned, instead of the full object.
83: * @throws InvalidArgumentException If a config has not been defined.
84: * @return \Charcoal\Config\ConfigInterface|mixed
85: */
86: public function config($key = null)
87: {
88: if ($this->config === null) {
89: throw new InvalidArgumentException(
90: 'Configuration not set.'
91: );
92: }
93:
94: if ($key !== null) {
95: return $this->config->get($key);
96: } else {
97: return $this->config;
98: }
99: }
100:
101: /**
102: * Setup the module's dependencies.
103: *
104: * @return AbstractModule
105: */
106: public function setup()
107: {
108: $this->setupRoutes();
109:
110: return $this;
111: }
112:
113: /**
114: * Set up the module's routes, via a RouteManager
115: *
116: * @return AbstractModule Chainable
117: */
118: public function setupRoutes()
119: {
120: if (!isset($this->routeManager)) {
121: $config = $this->config();
122: $routes = (isset($config['routes']) ? $config['routes'] : [] );
123:
124: $this->routeManager = new RouteManager([
125: 'config' => $routes,
126: 'app' => $this->app(),
127: 'logger' => $this->logger
128: ]);
129:
130: $this->routeManager->setupRoutes();
131: }
132:
133: return $this;
134: }
135: }
136: