1: <?php
2:
3: /**
4: * Picon Framework
5: * http://code.google.com/p/picon-framework/
6: *
7: * Copyright (C) 2011-2012 Martin Cassidy <martin.cassidy@webquub.com>
8:
9: * Picon Framework is free software: you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License as published by
11: * the Free Software Foundation, either version 3 of the License, or
12: * (at your option) any later version.
13:
14: * Picon Framework is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * General Public License for more details.
18:
19: * You should have received a copy of the GNU General Public License
20: * along with Picon Framework. If not, see <http://www.gnu.org/licenses/>.
21: * */
22:
23: namespace picon;
24: require_once("AutoLoader.php");
25: require_once('PiconErrorHandler.php');
26:
27: /**
28: * ApplicationInitialiser works like a bootstrap. This is the first step
29: * in the Picon application and performs the following:
30: *
31: * <ul><li>Registers handlers for auto auto loading classes and error handling</li>
32: * <li>Loads, validates and pareses the xml configuration into a config object</li>
33: * <li>Loads the page map</li>
34: * <li>Instantiates and injects resources</li></ul>
35: *
36: * @author Martin Cassidy
37: * @package core
38: */
39: class ApplicationInitializer
40: {
41: const CONFIG_RESOURCE_NAME = 'picon_config';
42:
43: private $autoLoader;
44: private $errorHandler;
45:
46: private static $dirs = array();
47:
48: public function __construct()
49: {
50: $this->autoLoader = new AutoLoader();
51: $this->errorHandler = new PiconErrorHandler;
52: }
53:
54: /**
55: * Add a directory to the class auto load scanner for a particular namespace
56: * @param String $directory The directory to scan
57: * @param String $namespace The namespace (optional, if your class is in
58: * the default namespace leave this blank)
59: */
60: public function addScannedDirectory($directory, $namespace = 'default')
61: {
62: $this->autoLoader->addScannedDirectory($directory, $namespace);
63: }
64:
65: /**
66: * Initialise the application
67: */
68: public function initialise()
69: {
70: $config = null;
71: if(CacheManager::resourceExists(self::CONFIG_RESOURCE_NAME, CacheManager::APPLICATION_SCOPE))
72: {
73: $config = CacheManager::loadResource(self::CONFIG_RESOURCE_NAME, CacheManager::APPLICATION_SCOPE);
74: }
75: else
76: {
77: $config = ConfigLoader::load(CONFIG_FILE);
78: CacheManager::saveResource(self::CONFIG_RESOURCE_NAME, $config, CacheManager::APPLICATION_SCOPE);
79: }
80: PiconApplication::get()->getConfigLoadListener()->onConfigLoaded($config);
81:
82: $loader = ContextLoaderFactory::getLoader($config);
83: $context = $loader->load($config);
84: $injector = new Injector($context);
85:
86: foreach($context->getResources() as $resource)
87: {
88: $injector->inject($resource);
89: }
90: foreach($context->getResources() as $resource)
91: {
92: if($resource instanceof InitializingBean)
93: {
94: $resource->afterPropertiesSet();
95: }
96: }
97:
98: PiconApplication::get()->getContextLoadListener()->onContextLoaded($context);
99:
100: PageMap::get()->initialise();
101: }
102:
103: /**
104: * Runs a require_once() on all the php files in the given directory
105: * and invokes itself on any sub directories
106: * @param String $directory the working directory
107: */
108: public static function loadAssets($directory)
109: {
110: if(in_array($directory, self::$dirs))
111: {
112: return;
113: }
114: $d = dir($directory);
115: while (false !== ($entry = $d->read()))
116: {
117: if(preg_match("/\s*.php{1}$/", $entry))
118: {
119: require_once($directory."/".$entry);
120: }
121: if(is_dir($directory."/".$entry) && !preg_match("/^.{1}.?$/", $entry))
122: {
123: self::loadAssets($directory."/".$entry);
124: }
125: }
126: $d->close();
127: array_push(self::$dirs, $directory);
128: }
129:
130: }
131:
132: ?>
133: