1: <?php
2:
3: namespace PHPixie\DefaultBundle;
4:
5: /**
6: * Factory
7: * @package PHPixie
8: */
9: abstract class Builder
10: {
11: /**
12: * BundleFramework builder
13: * @param \PHPixie\BundleFramework\Builder $frameworkBuilder
14: */
15: protected $frameworkBuilder;
16:
17: /**
18: * @var array
19: */
20: protected $instances = array();
21:
22: /**
23: * Constructor
24: * @param \PHPixie\BundleFramework\Builder $frameworkBuilder
25: */
26: public function __construct($frameworkBuilder)
27: {
28: $this->frameworkBuilder = $frameworkBuilder;
29: }
30:
31: /**
32: * BundleFramework builder
33: * @return \PHPixie\BundleFramework\Builder
34: */
35: public function frameworkBuilder()
36: {
37: return $this->frameworkBuilder;
38: }
39:
40: /**
41: * Framework components
42: * @return \PHPixie\BundleFramework\Components
43: */
44: public function components()
45: {
46: return $this->frameworkBuilder->components();
47: }
48:
49: /**
50: * Configuration options passed to the bundle.
51: *
52: * Defined in /assets/config/bundles/<bundleName>
53: * @return \PHPixie\Slice\Data
54: */
55: public function config()
56: {
57: return $this->instance('config');
58: }
59:
60: /**
61: * Configuration options supplied with the bundlee
62: *
63: * Defined in <bundle root>/assets/config/
64: * @return \PHPixie\Slice\Data|null
65: */
66: public function bundleConfig()
67: {
68: return $this->instance('bundleConfig');
69: }
70:
71: /**
72: * Processor used for HTTP requests
73: * @return \PHPixie\Processors\Processor|null
74: */
75: public function httpProcessor()
76: {
77: return $this->instance('httpProcessor');
78: }
79:
80: /**
81: * Route resolver for routing HTTP requests
82: * @return \PHPixie\Route\Resolvers\Resolver|null
83: */
84: public function routeResolver()
85: {
86: return $this->instance('routeResolver');
87: }
88:
89: /**
90: * Filesystem locator for templates
91: * @return \PHPixie\Filesystem\Locators\Locator|null
92: */
93: public function templateLocator()
94: {
95: return $this->instance('templateLocator');
96: }
97:
98: /**
99: * Orm config data
100: * @return \PHPixie\Slice\Data|null
101: */
102: public function ormConfig()
103: {
104: return $this->instance('ormConfig');
105: }
106:
107: /**
108: * ORM wrapper
109: * @return \PHPixie\ORM\Wrappers|null
110: */
111: public function ormWrappers()
112: {
113: return $this->instance('ormWrappers');
114: }
115:
116: /**
117: * Root directory of the bundle
118: * @return \PHPixie\Filesystem\Root|null
119: */
120: public function filesystemRoot()
121: {
122: return $this->instance('filesystemRoot');
123: }
124:
125: /**
126: * Bundle assets directory
127: * @return \PHPixie\Filesystem\Root|null
128: */
129: public function assetsRoot()
130: {
131: return $this->instance('assetsRoot');
132: }
133:
134: /**
135: * User repositories for auth component
136: * @return \PHPixie\Auth\Repositories|null
137: */
138: public function authRepositories()
139: {
140: return $this->instance('authRepositories');
141: }
142:
143: /**
144: * Bundle web directory
145: * @return \PHPixie\Filesystem\Root|null
146: */
147: public function webRoot()
148: {
149: return $this->instance('webRoot');
150: }
151:
152: /**
153: * Returns a single instance by name
154: * @param string $name
155: * @return mixed
156: */
157: protected function instance($name)
158: {
159: if(!array_key_exists($name, $this->instances)) {
160: $method = 'build'.ucfirst($name);
161: $this->instances[$name] = $this->$method();
162: }
163:
164: return $this->instances[$name];
165: }
166:
167: /**
168: * @return \PHPixie\Processors\Processor|null
169: */
170: protected function buildHttpProcessor()
171: {
172: return null;
173: }
174:
175: /**
176: * @return \PHPixie\Slice\Data
177: */
178: protected function buildConfig()
179: {
180: return $this->components()->bundles()->config(
181: $this->bundleName()
182: );
183: }
184:
185: /**
186: * @return \PHPixie\Slice\Data|null
187: */
188: protected function buildBundleConfig()
189: {
190: $assetsRoot = $this->assetsRoot();
191: if($assetsRoot === null) {
192: return null;
193: }
194:
195: return $this->components()->config()->directory(
196: $assetsRoot->path(),
197: 'config'
198: );
199: }
200:
201: /**
202: * @return \PHPixie\Route\Resolvers\Resolver|null
203: */
204: protected function buildRouteResolver()
205: {
206: $config = $this->bundleConfig();
207: if($config === null) {
208: return null;
209: }
210:
211: $configData = $config->slice('routeResolver');
212: if($configData->get('type') === null) {
213: return null;
214: }
215:
216: return $this->components()->route()->buildResolver($configData);
217: }
218:
219: /**
220: * @return \PHPixie\Filesystem\Locators\Locator|null
221: */
222: protected function buildTemplateLocator()
223: {
224: $config = $this->bundleConfig();
225:
226: if($config === null) {
227: return null;
228: }
229:
230: $configData = $config->slice('templateLocator');
231: if($configData->get('type') === null) {
232: return null;
233: }
234:
235: return $this->components()->filesystem()->buildLocator(
236: $configData,
237: $this->assetsRoot()
238: );
239: }
240:
241: /**
242: * @return \PHPixie\Filesystem\Root|null
243: */
244: protected function buildFilesystemRoot()
245: {
246: $directory = $this->getRootDirectory();
247:
248: if($directory === null) {
249: return null;
250: }
251:
252: return $this->components()->filesystem()->root(
253: $directory
254: );
255: }
256:
257: /**
258: * @return \PHPixie\Slice\Data|null
259: */
260: protected function buildOrmConfig()
261: {
262: $config = $this->bundleConfig();
263: if($config === null) {
264: return null;
265: }
266:
267: return $config->slice('orm');
268: }
269:
270: /**
271: * @return \PHPixie\ORM\Wrappers|null
272: */
273: protected function buildOrmWrappers()
274: {
275: return null;
276: }
277:
278: /**
279: * @return \PHPixie\Auth\Repositories|null
280: */
281: protected function buildAuthRepositories()
282: {
283: return null;
284: }
285:
286: /**
287: * @return \PHPixie\Filesystem\Root|null
288: */
289: protected function buildWebRoot()
290: {
291: return $this->buildPathRoot('web');
292: }
293:
294: /**
295: * @return \PHPixie\Filesystem\Root|null
296: */
297: protected function buildAssetsRoot()
298: {
299: return $this->buildPathRoot('assets');
300: }
301:
302: /**
303: * @return \PHPixie\Filesystem\Root|null
304: */
305: protected function buildPathRoot($path)
306: {
307: $filesystemRoot = $this->filesystemRoot();
308: if($filesystemRoot === null) {
309: return null;
310: }
311:
312: $directory = $this->filesystemRoot()->path($path);
313:
314: if(!is_dir($directory)) {
315: return null;
316: }
317:
318: return $this->components()->filesystem()->root(
319: $directory
320: );
321: }
322:
323: /**
324: * @return \PHPixie\Filesystem\Root|null
325: */
326: protected function getRootDirectory()
327: {
328: return null;
329: }
330:
331: /**
332: * Bundle name
333: * @return string
334: */
335: abstract public function bundleName();
336: }