| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // +---------------------------------------------------------------------------+ |
|---|
| 4 | // | This file is part of the Agavi package. | |
|---|
| 5 | // | Copyright (c) 2005-2008 the Agavi Project. | |
|---|
| 6 | // | | |
|---|
| 7 | // | For the full copyright and license information, please view the LICENSE | |
|---|
| 8 | // | file that was distributed with this source code. You can also view the | |
|---|
| 9 | // | LICENSE file online at http://www.agavi.org/LICENSE.txt | |
|---|
| 10 | // | vi: set noexpandtab: | |
|---|
| 11 | // | Local Variables: | |
|---|
| 12 | // | indent-tabs-mode: t | |
|---|
| 13 | // | End: | |
|---|
| 14 | // +---------------------------------------------------------------------------+ |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * AgaviFactoryConfigHandler allows you to specify which factory implementation |
|---|
| 18 | * the system will use. |
|---|
| 19 | * |
|---|
| 20 | * @package agavi |
|---|
| 21 | * @subpackage config |
|---|
| 22 | * |
|---|
| 23 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 24 | * @author Dominik del Bondio <ddb@bitxtender.com> |
|---|
| 25 | * @copyright Authors |
|---|
| 26 | * @copyright The Agavi Project |
|---|
| 27 | * |
|---|
| 28 | * @since 0.9.0 |
|---|
| 29 | * |
|---|
| 30 | * @version $Id$ |
|---|
| 31 | */ |
|---|
| 32 | class AgaviFactoryConfigHandler extends AgaviConfigHandler |
|---|
| 33 | { |
|---|
| 34 | /** |
|---|
| 35 | * Execute this configuration handler. |
|---|
| 36 | * |
|---|
| 37 | * @param string An absolute filesystem path to a configuration file. |
|---|
| 38 | * @param string An optional context in which we are currently running. |
|---|
| 39 | * |
|---|
| 40 | * @return string Data to be written to a cache file. |
|---|
| 41 | * |
|---|
| 42 | * @throws <b>AgaviUnreadableException</b> If a requested configuration |
|---|
| 43 | * file does not exist or is not |
|---|
| 44 | * readable. |
|---|
| 45 | * @throws <b>AgaviParseException</b> If a requested configuration file is |
|---|
| 46 | * improperly formatted. |
|---|
| 47 | * |
|---|
| 48 | * @author David Zülke <dz@bitxtender.com> |
|---|
| 49 | * @author Dominik del Bondio <ddb@bitxtender.com> |
|---|
| 50 | * @since 0.9.0 |
|---|
| 51 | */ |
|---|
| 52 | public function execute($config, $context = null) |
|---|
| 53 | { |
|---|
| 54 | if($context == null) { |
|---|
| 55 | $context = ''; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | // parse the config file |
|---|
| 59 | $configurations = $this->orderConfigurations(AgaviConfigCache::parseConfig($config, true, $this->getValidationFile(), $this->parser)->configurations, AgaviConfig::get('core.environment'), $context); |
|---|
| 60 | |
|---|
| 61 | $data = array(); |
|---|
| 62 | |
|---|
| 63 | // The order of this initialisiation code is fixed, to not change |
|---|
| 64 | // name => required? |
|---|
| 65 | $factories = array( |
|---|
| 66 | 'execution_container' => array( |
|---|
| 67 | 'required' => true, |
|---|
| 68 | 'var' => null, |
|---|
| 69 | 'must_implement' => array( |
|---|
| 70 | ), |
|---|
| 71 | ), |
|---|
| 72 | |
|---|
| 73 | 'validation_manager' => array( |
|---|
| 74 | 'required' => true, |
|---|
| 75 | 'var' => null, |
|---|
| 76 | 'must_implement' => array( |
|---|
| 77 | ), |
|---|
| 78 | ), |
|---|
| 79 | |
|---|
| 80 | 'dispatch_filter' => array( |
|---|
| 81 | 'required' => true, |
|---|
| 82 | 'var' => null, |
|---|
| 83 | 'must_implement' => array( |
|---|
| 84 | 'AgaviIGlobalFilter', |
|---|
| 85 | ), |
|---|
| 86 | ), |
|---|
| 87 | |
|---|
| 88 | 'execution_filter' => array( |
|---|
| 89 | 'required' => true, |
|---|
| 90 | 'var' => null, |
|---|
| 91 | 'must_implement' => array( |
|---|
| 92 | 'AgaviIActionFilter', |
|---|
| 93 | ), |
|---|
| 94 | ), |
|---|
| 95 | |
|---|
| 96 | 'security_filter' => array( |
|---|
| 97 | 'required' => AgaviConfig::get('core.use_security', false), |
|---|
| 98 | 'var' => null, |
|---|
| 99 | 'must_implement' => array( |
|---|
| 100 | 'AgaviIActionFilter', |
|---|
| 101 | 'AgaviISecurityFilter', |
|---|
| 102 | ), |
|---|
| 103 | ), |
|---|
| 104 | |
|---|
| 105 | 'filter_chain' => array( |
|---|
| 106 | 'required' => true, |
|---|
| 107 | 'var' => null, |
|---|
| 108 | 'must_implement' => array( |
|---|
| 109 | ), |
|---|
| 110 | ), |
|---|
| 111 | |
|---|
| 112 | 'response' => array( |
|---|
| 113 | 'required' => true, |
|---|
| 114 | 'var' => null, |
|---|
| 115 | 'must_implement' => array( |
|---|
| 116 | ), |
|---|
| 117 | ), |
|---|
| 118 | |
|---|
| 119 | 'database_manager' => array( |
|---|
| 120 | 'required' => AgaviConfig::get('core.use_database', false), |
|---|
| 121 | 'var' => 'databaseManager', |
|---|
| 122 | 'must_implement' => array( |
|---|
| 123 | ), |
|---|
| 124 | ), |
|---|
| 125 | |
|---|
| 126 | 'database_manager', // startup() |
|---|
| 127 | |
|---|
| 128 | 'logger_manager' => array( |
|---|
| 129 | 'required' => AgaviConfig::get('core.use_logging', false), |
|---|
| 130 | 'var' => 'loggerManager', |
|---|
| 131 | 'must_implement' => array( |
|---|
| 132 | ), |
|---|
| 133 | ), |
|---|
| 134 | |
|---|
| 135 | 'logger_manager', // startup() |
|---|
| 136 | |
|---|
| 137 | 'translation_manager' => array( |
|---|
| 138 | 'required' => AgaviConfig::get('core.use_translation', false), |
|---|
| 139 | 'var' => 'translationManager', |
|---|
| 140 | 'must_implement' => array( |
|---|
| 141 | ), |
|---|
| 142 | ), |
|---|
| 143 | |
|---|
| 144 | 'request' => array( |
|---|
| 145 | 'required' => true, |
|---|
| 146 | 'var' => 'request', |
|---|
| 147 | 'must_implement' => array( |
|---|
| 148 | ), |
|---|
| 149 | ), |
|---|
| 150 | |
|---|
| 151 | 'routing' => array( |
|---|
| 152 | 'required' => true, |
|---|
| 153 | 'var' => 'routing', |
|---|
| 154 | 'must_implement' => array( |
|---|
| 155 | ), |
|---|
| 156 | ), |
|---|
| 157 | |
|---|
| 158 | 'controller' => array( |
|---|
| 159 | 'required' => true, |
|---|
| 160 | 'var' => 'controller', |
|---|
| 161 | 'must_implement' => array( |
|---|
| 162 | ), |
|---|
| 163 | ), |
|---|
| 164 | |
|---|
| 165 | 'storage' => array( |
|---|
| 166 | 'required' => true, |
|---|
| 167 | 'var' => 'storage', |
|---|
| 168 | 'must_implement' => array( |
|---|
| 169 | ), |
|---|
| 170 | ), |
|---|
| 171 | |
|---|
| 172 | 'storage', // startup() |
|---|
| 173 | |
|---|
| 174 | 'user' => array( |
|---|
| 175 | 'required' => true, |
|---|
| 176 | 'var' => 'user', |
|---|
| 177 | 'must_implement' => ( |
|---|
| 178 | AgaviConfig::get('core.use_security') |
|---|
| 179 | ? array( |
|---|
| 180 | 'AgaviISecurityUser', |
|---|
| 181 | ) |
|---|
| 182 | : array( |
|---|
| 183 | ) |
|---|
| 184 | ), |
|---|
| 185 | ), |
|---|
| 186 | |
|---|
| 187 | 'translation_manager', // startup() |
|---|
| 188 | |
|---|
| 189 | 'user', // startup() |
|---|
| 190 | |
|---|
| 191 | 'routing', // startup() |
|---|
| 192 | |
|---|
| 193 | 'request', // startup() |
|---|
| 194 | |
|---|
| 195 | 'controller', // startup() |
|---|
| 196 | ); |
|---|
| 197 | |
|---|
| 198 | foreach($configurations as $cfg) { |
|---|
| 199 | foreach($factories as $factory => $info) { |
|---|
| 200 | if($info['required'] && isset($cfg->$factory)) { |
|---|
| 201 | $data[$factory] = isset($data[$factory]) ? $data[$factory] : array('class' => null, 'params' => array()); |
|---|
| 202 | $data[$factory]['class'] = $cfg->$factory->getAttribute('class', $data[$factory]['class']); |
|---|
| 203 | $data[$factory]['params'] = $this->getItemParameters($cfg->$factory, $data[$factory]['params']); |
|---|
| 204 | } |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | $code = array(); |
|---|
| 209 | $shutdownSequence = array(); |
|---|
| 210 | |
|---|
| 211 | foreach($factories as $factory => $info) { |
|---|
| 212 | if(is_array($info)) { |
|---|
| 213 | if(!$info['required']) { |
|---|
| 214 | continue; |
|---|
| 215 | } |
|---|
| 216 | if(!isset($data[$factory]) || $data[$factory]['class'] === null) { |
|---|
| 217 | $error = 'Configuration file "%s" has missing or incomplete entry "%s"'; |
|---|
| 218 | $error = sprintf($error, $config, $factory); |
|---|
| 219 | throw new AgaviConfigurationException($error); |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | try { |
|---|
| 223 | $rc = new ReflectionClass($data[$factory]['class']); |
|---|
| 224 | } catch(ReflectionException $e) { |
|---|
| 225 | $error = 'Configuration file "%s" specifies unknown class "%s" for entry "%s"'; |
|---|
| 226 | $error = sprintf($error, $config, $data[$factory]['class'], $factory); |
|---|
| 227 | throw new AgaviConfigurationException($error); |
|---|
| 228 | } |
|---|
| 229 | foreach($info['must_implement'] as $interface) { |
|---|
| 230 | if(!$rc->implementsInterface($interface)) { |
|---|
| 231 | $error = 'Class "%s" for entry "%s" does not implement interface "%s" in configuration file "%s"'; |
|---|
| 232 | $error = sprintf($error, $data[$factory]['class'], $factory, $interface, $config); |
|---|
| 233 | throw new AgaviConfigurationException($error); |
|---|
| 234 | } |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | if($info['var'] !== null) { |
|---|
| 238 | // we have to make an instance |
|---|
| 239 | $code[] = sprintf( |
|---|
| 240 | '$this->%1$s = new %2$s();' . "\n" . '$this->%1$s->initialize($this, %3$s);', |
|---|
| 241 | $info['var'], |
|---|
| 242 | $data[$factory]['class'], |
|---|
| 243 | var_export($data[$factory]['params'], true) |
|---|
| 244 | ); |
|---|
| 245 | } else { |
|---|
| 246 | // it's a factory info |
|---|
| 247 | $code[] = sprintf( |
|---|
| 248 | '$this->factories[%1$s] = %2$s;', |
|---|
| 249 | var_export($factory, true), |
|---|
| 250 | var_export(array( |
|---|
| 251 | 'class' => $data[$factory]['class'], |
|---|
| 252 | 'parameters' => $data[$factory]['params'], |
|---|
| 253 | ), true) |
|---|
| 254 | ); |
|---|
| 255 | } |
|---|
| 256 | } else { |
|---|
| 257 | if($factories[$info]['required']) { |
|---|
| 258 | $code[] = sprintf('$this->%s->startup();', $factories[$info]['var']); |
|---|
| 259 | array_unshift($shutdownSequence, sprintf('$this->%s', $factories[$info]['var'])); |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | |
|---|
| 264 | $code[] = sprintf('$this->shutdownSequence = array(%s);', implode(",\n", $shutdownSequence)); |
|---|
| 265 | |
|---|
| 266 | return $this->generate($code); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | ?> |
|---|