| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
class sfFactoryConfigHandler extends sfYamlConfigHandler |
|---|
| 23 |
{ |
|---|
| 24 |
|
|---|
| 25 |
* Executes this configuration handler. |
|---|
| 26 |
* |
|---|
| 27 |
* @param array An array of absolute filesystem path to a configuration file |
|---|
| 28 |
* |
|---|
| 29 |
* @return string Data to be written to a cache file |
|---|
| 30 |
* |
|---|
| 31 |
* @throws <b>sfConfigurationException</b> If a requested configuration file does not exist or is not readable |
|---|
| 32 |
* @throws <b>sfParseException</b> If a requested configuration file is improperly formatted |
|---|
| 33 |
*/ |
|---|
| 34 |
public function execute($configFiles) |
|---|
| 35 |
{ |
|---|
| 36 |
|
|---|
| 37 |
$myConfig = $this->parseYamls($configFiles); |
|---|
| 38 |
|
|---|
| 39 |
$myConfig = sfToolkit::arrayDeepMerge( |
|---|
| 40 |
isset($myConfig['default']) && is_array($myConfig['default']) ? $myConfig['default'] : array(), |
|---|
| 41 |
isset($myConfig['all']) && is_array($myConfig['all']) ? $myConfig['all'] : array(), |
|---|
| 42 |
isset($myConfig[sfConfig::get('sf_environment')]) && is_array($myConfig[sfConfig::get('sf_environment')]) ? $myConfig[sfConfig::get('sf_environment')] : array() |
|---|
| 43 |
); |
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
$includes = array(); |
|---|
| 47 |
$inits = array(); |
|---|
| 48 |
$instances = array(); |
|---|
| 49 |
|
|---|
| 50 |
|
|---|
| 51 |
$factories = array('controller', 'request', 'response', 'storage', 'user', 'view_cache'); |
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
foreach ($factories as $factory) |
|---|
| 55 |
{ |
|---|
| 56 |
|
|---|
| 57 |
$keys = $myConfig[$factory]; |
|---|
| 58 |
|
|---|
| 59 |
if (!isset($keys['class'])) |
|---|
| 60 |
{ |
|---|
| 61 |
|
|---|
| 62 |
$error = sprintf('Configuration file "%s" specifies category "%s" with missing class key', $configFiles[0], $factory); |
|---|
| 63 |
throw new sfParseException($error); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
$class = $keys['class']; |
|---|
| 67 |
|
|---|
| 68 |
if (isset($keys['file'])) |
|---|
| 69 |
{ |
|---|
| 70 |
|
|---|
| 71 |
$file = $this->replaceConstants($keys['file']); |
|---|
| 72 |
$file = $this->replacePath($file); |
|---|
| 73 |
|
|---|
| 74 |
if (!is_readable($file)) |
|---|
| 75 |
{ |
|---|
| 76 |
|
|---|
| 77 |
$error = sprintf('Configuration file "%s" specifies class "%s" with nonexistent or unreadable file "%s"', $configFiles[0], $class, $file); |
|---|
| 78 |
throw new sfParseException($error); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
$includes[] = sprintf("require_once('%s');", $file); |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
if (isset($keys['param'])) |
|---|
| 87 |
{ |
|---|
| 88 |
$parameters = array(); |
|---|
| 89 |
foreach ($keys['param'] as $key => $value) |
|---|
| 90 |
{ |
|---|
| 91 |
$parameters[$key] = $this->replaceConstants($value); |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
else |
|---|
| 95 |
{ |
|---|
| 96 |
$parameters = null; |
|---|
| 97 |
} |
|---|
| 98 |
$parameters = var_export($parameters, true); |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
switch ($factory) |
|---|
| 102 |
{ |
|---|
| 103 |
case 'controller': |
|---|
| 104 |
|
|---|
| 105 |
$instances[] = sprintf(" \$this->controller = sfController::newInstance(sfConfig::get('sf_factory_controller', '%s'));", $class); |
|---|
| 106 |
|
|---|
| 107 |
|
|---|
| 108 |
$inits[] = " \$this->controller->initialize(\$this);"; |
|---|
| 109 |
break; |
|---|
| 110 |
|
|---|
| 111 |
case 'request': |
|---|
| 112 |
|
|---|
| 113 |
$instances[] = sprintf(" \$this->request = sfRequest::newInstance(sfConfig::get('sf_factory_request', '%s'));", $class); |
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 |
$inits[] = sprintf(" \$this->request->initialize(\$this, sfConfig::get('sf_factory_request_parameters', %s), sfConfig::get('sf_factory_request_attributes', array()));", $parameters); |
|---|
| 117 |
break; |
|---|
| 118 |
|
|---|
| 119 |
case 'response': |
|---|
| 120 |
|
|---|
| 121 |
$instances[] = sprintf(" \$this->response = sfResponse::newInstance(sfConfig::get('sf_factory_response', '%s'));", $class); |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
$inits[] = sprintf(" \$this->response->initialize(\$this, sfConfig::get('sf_factory_response_parameters', %s));", $parameters); |
|---|
| 125 |
break; |
|---|
| 126 |
|
|---|
| 127 |
case 'storage': |
|---|
| 128 |
|
|---|
| 129 |
$instances[] = sprintf(" \$this->storage = sfStorage::newInstance(sfConfig::get('sf_factory_storage', '%s'));", $class); |
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
$inits[] = sprintf(" \$this->storage->initialize(\$this, sfConfig::get('sf_factory_storage_parameters', %s));", $parameters); |
|---|
| 133 |
break; |
|---|
| 134 |
|
|---|
| 135 |
case 'user': |
|---|
| 136 |
|
|---|
| 137 |
$instances[] = sprintf(" \$this->user = sfUser::newInstance(sfConfig::get('sf_factory_user', '%s'));", $class); |
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
$inits[] = sprintf(" \$this->user->initialize(\$this, sfConfig::get('sf_factory_user_parameters', %s));", $parameters); |
|---|
| 141 |
break; |
|---|
| 142 |
case 'view_cache': |
|---|
| 143 |
|
|---|
| 144 |
$inits[] = sprintf("\n if (sfConfig::get('sf_cache'))\n {\n". |
|---|
| 145 |
" \$this->viewCacheManager = new sfViewCacheManager();\n". |
|---|
| 146 |
" \$this->viewCacheManager->initialize(\$this, sfConfig::get('sf_factory_view_cache', '%s'), sfConfig::get('sf_factory_view_cache_parameters', %s));\n". |
|---|
| 147 |
" }\n", |
|---|
| 148 |
$class, $parameters); |
|---|
| 149 |
break; |
|---|
| 150 |
} |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
$retval = sprintf("<?php\n". |
|---|
| 155 |
"// auto-generated by sfFactoryConfigHandler\n". |
|---|
| 156 |
"// date: %s\n%s\n%s\n%s\n", |
|---|
| 157 |
date('Y/m/d H:i:s'), implode("\n", $includes), |
|---|
| 158 |
implode("\n", $instances), implode("\n", $inits)); |
|---|
| 159 |
|
|---|
| 160 |
return $retval; |
|---|
| 161 |
} |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|