1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30:
31: abstract class AbstractContextLoader
32: {
33: const CONTEXT_RESOURCE_NAME = 'application_context';
34:
35: private $resourceMap;
36: private $resources = array();
37:
38: public function load(Config $config)
39: {
40: $this->resourceMap = CacheManager::loadResource(self::CONTEXT_RESOURCE_NAME, CacheManager::APPLICATION_SCOPE);
41:
42: if($this->resourceMap==null)
43: {
44: ApplicationInitializer::loadAssets(ASSETS_DIRECTORY);
45: $this->resourceMap = $this->loadResourceMap($this->getClasses());
46: CacheManager::saveResource(self::CONTEXT_RESOURCE_NAME, $this->resourceMap, CacheManager::APPLICATION_SCOPE);
47: }
48:
49: $this->createResources();
50:
51: $this->loadDataSources($config->getDataSources());
52: return new ApplicationContext($this->resources);
53: }
54:
55: protected abstract function loadResourceMap($classes);
56:
57: protected abstract function loadDataSources($sourceConfig);
58:
59: private function createResources()
60: {
61: foreach($this->resourceMap as $name => $class)
62: {
63: $this->pushToResourceMap($name, new $class());
64: }
65: }
66:
67: 68: 69: 70: 71:
72: protected function pushToResourceMap($resourceName, $resource)
73: {
74: if(array_key_exists($resourceName, $this->resources))
75: {
76: throw new \DuplicateResourceException(sprintf("The resource %s already exists.", $resourceName));
77: }
78: $this->resources[$resourceName] = $resource;
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88: 89:
90: protected function getResourceName(\Annotation $annotation, $className)
91: {
92: $name = $annotation->name;
93:
94: if($name=="")
95: {
96: return strtolower(substr($className, 0, 1)).substr($className,1,strlen($className));
97: }
98: else
99: {
100: return $name;
101: }
102: }
103:
104: public static function getClasses()
105: {
106: return get_declared_classes();
107: }
108:
109: public function createDataSources()
110: {
111:
112: }
113: }
114:
115: ?>
116: