1: <?php
2:
3: namespace PHPixie\BundleFramework;
4:
5: use PHPixie\Processors\Processor;
6: use PHPixie\Slice\Data;
7:
8: /**
9: * Framework configuration
10: */
11: class Configuration implements \PHPixie\Framework\Configuration
12: {
13: /**
14: * @var Builder
15: */
16: protected $builder;
17:
18: /**
19: * @var array
20: */
21: protected $instances = array();
22:
23: /**
24: * Constructor
25: * @param Builder $builder
26: */
27: public function __construct($builder)
28: {
29: $this->builder = $builder;
30: }
31:
32: /**
33: * Bundles configuration
34: * @return Data
35: */
36: public function bundlesConfig()
37: {
38: return $this->instance('bundlesConfig');
39: }
40:
41: /**
42: * @inheritdoc
43: */
44: public function databaseConfig()
45: {
46: return $this->instance('databaseConfig');
47: }
48:
49: /**
50: * @inheritdoc
51: */
52: public function httpConfig()
53: {
54: return $this->instance('httpConfig');
55: }
56:
57: /**
58: * @inheritdoc
59: */
60: public function templateConfig()
61: {
62: return $this->instance('templateConfig');
63: }
64:
65: /**
66: * @inheritdoc
67: */
68: public function filesystemRoot()
69: {
70: return $this->builder->assets()->root();
71: }
72:
73: /**
74: * ORM configuration merger
75: * @return Configuration\ORM
76: */
77: public function orm()
78: {
79: return $this->instance('orm');
80: }
81:
82: /**
83: * @inheritdoc
84: */
85: public function ormConfig()
86: {
87: return $this->orm()->configData();
88: }
89:
90: /**
91: * @inheritdoc
92: */
93: public function ormWrappers()
94: {
95: return $this->orm()->wrappers();
96: }
97:
98: /**
99: * @inheritdoc
100: */
101: public function authConfig()
102: {
103: return $this->instance('authConfig');
104: }
105:
106: /**
107: * @inheritdoc
108: */
109: public function authRepositories()
110: {
111: $components = $this->builder->components();
112: return $components->bundles()->auth();
113: }
114:
115: /**
116: * @inheritdoc
117: */
118: public function httpProcessor()
119: {
120: return $this->instance('httpProcessor');
121: }
122:
123: /**
124: * @inheritdoc
125: */
126: public function httpRouteResolver()
127: {
128: return $this->instance('httpRouteResolver');
129: }
130:
131: /**
132: * @inheritdoc
133: */
134: public function templateLocator()
135: {
136: return $this->instance('templateLocator');
137: }
138:
139: /**
140: * @param string $name
141: * @return mixed
142: */
143: protected function instance($name)
144: {
145: if(!array_key_exists($name, $this->instances)) {
146: $method = 'build'.ucfirst($name);
147: $this->instances[$name] = $this->$method();
148: }
149:
150: return $this->instances[$name];
151: }
152:
153: /**
154: * @return Data
155: */
156: protected function buildBundlesConfig()
157: {
158: return $this->configStorage()->arraySlice('bundles');
159: }
160:
161: /**
162: * @return Data
163: */
164: protected function buildDatabaseConfig()
165: {
166: return $this->configStorage()->arraySlice('database');
167: }
168:
169: /**
170: * @return Data
171: */
172: protected function buildHttpConfig()
173: {
174: return $this->configStorage()->arraySlice('http');
175: }
176:
177: /**
178: * @return Data
179: */
180: protected function buildTemplateConfig()
181: {
182: return $this->configStorage()->arraySlice('template');
183: }
184:
185: /**
186: * @return Data
187: */
188: protected function buildAuthConfig()
189: {
190: return $this->configStorage()->arraySlice('auth');
191: }
192:
193: /**
194: * @return Configuration\ORM
195: */
196: protected function buildOrm()
197: {
198: $components = $this->builder->components();
199:
200: return new Configuration\ORM(
201: $components->slice(),
202: $components->bundles()->orm()
203: );
204: }
205:
206: /**
207: * @return Processor
208: */
209: protected function buildHttpProcessor()
210: {
211: $components = $this->builder->components();
212:
213: return $components->httpProcessors()->attributeRegistryDispatcher(
214: $components->bundles()->httpProcessors(),
215: 'bundle'
216: );
217: }
218:
219: /**
220: * @return \PHPixie\Route\Resolvers\Resolver
221: */
222: protected function buildHttpRouteResolver()
223: {
224: $components = $this->builder->components();
225:
226: return $components->route()->buildResolver(
227: $this->configStorage()->arraySlice('http.resolver'),
228: $components->bundles()->routeResolvers()
229: );
230: }
231:
232: /**
233: * @return Configuration\FilesystemLocator\Template
234: */
235: protected function buildTemplateLocator()
236: {
237: $components = $this->builder->components();
238: $bundleLocators = $components->bundles()->templateLocators();
239:
240: $overridesLocator = null;
241:
242: $overridesConfig = $this->configStorage()->arraySlice('template.locator');
243: if($overridesConfig->get('type') !== null) {
244: $overridesLocator = $components->filesystem()->buildLocator(
245: $overridesConfig,
246: $bundleLocators
247: );
248: }
249:
250: return new Configuration\FilesystemLocator\Template(
251: $bundleLocators,
252: $this->builder->assets(),
253: $overridesLocator
254: );
255: }
256:
257: /**
258: * @return \PHPixie\Config\Storages\Type\Directory
259: */
260: protected function configStorage()
261: {
262: return $this->builder->assets()->configStorage();
263: }
264: }
265: