1: <?php
2:
3: namespace PHPixie\BundleFramework;
4:
5: /**
6: * Bundle registry
7: */
8: abstract class Bundles implements \PHPixie\Bundles\Registry
9: {
10: /**
11: * @var Builder
12: */
13: protected $builder;
14:
15: /**
16: * @var array
17: */
18: protected $bundles;
19:
20: /**
21: * Constructor
22: * @param Builder $builder
23: */
24: public function __construct($builder)
25: {
26: $this->builder = $builder;
27: }
28:
29: /**
30: * @inheritdoc
31: */
32: public function bundles()
33: {
34: $this->requireBundles();
35: return $this->bundles;
36: }
37:
38: /**
39: * @inheritdoc
40: */
41: public function get($name, $isRequired = true)
42: {
43: $this->requireBundles();
44:
45: if(array_key_exists($name, $this->bundles)) {
46: return $this->bundles[$name];
47: }
48:
49: if(!$isRequired) {
50: return null;
51: }
52:
53: throw new \PHPixie\BundleFramework\Exception("Bundle '$name' does not exist");
54: }
55:
56: /**
57: * @return void
58: */
59: protected function requireBundles()
60: {
61: if($this->bundles !== null) {
62: return;
63: }
64:
65: $bundles = array();
66: foreach($this->buildBundles() as $bundle) {
67: $bundles[$bundle->name()] = $bundle;
68: }
69:
70: $this->bundles = $bundles;
71: }
72:
73: /**
74: * Build bundles
75: * @return array
76: */
77: abstract protected function buildBundles();
78: }