| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
abstract class sfGenerator |
|---|
| 20 |
{ |
|---|
| 21 |
protected |
|---|
| 22 |
$generatorClass = '', |
|---|
| 23 |
$generatorManager = null, |
|---|
| 24 |
$generatedModuleName = '', |
|---|
| 25 |
$theme = 'default', |
|---|
| 26 |
$moduleName = ''; |
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
* Initializes the current sfGenerator instance. |
|---|
| 30 |
* |
|---|
| 31 |
* @param sfGeneratorManager A sfGeneratorManager instance |
|---|
| 32 |
*/ |
|---|
| 33 |
public function initialize($generatorManager) |
|---|
| 34 |
{ |
|---|
| 35 |
$this->generatorManager = $generatorManager; |
|---|
| 36 |
} |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
* Generates classes and templates. |
|---|
| 40 |
* |
|---|
| 41 |
* @param array An array of parameters |
|---|
| 42 |
* |
|---|
| 43 |
* @return string The cache for the configuration file |
|---|
| 44 |
*/ |
|---|
| 45 |
abstract public function generate($params = array()); |
|---|
| 46 |
|
|---|
| 47 |
|
|---|
| 48 |
* Generates PHP files for a given module name. |
|---|
| 49 |
* |
|---|
| 50 |
* @param string The name of module name to generate |
|---|
| 51 |
* @param array A list of template files to generate |
|---|
| 52 |
* @param array A list of configuration files to generate |
|---|
| 53 |
*/ |
|---|
| 54 |
protected function generatePhpFiles($generatedModuleName, $templateFiles = array(), $configFiles = array()) |
|---|
| 55 |
{ |
|---|
| 56 |
|
|---|
| 57 |
$retval = $this->evalTemplate('actions/actions.class.php'); |
|---|
| 58 |
|
|---|
| 59 |
|
|---|
| 60 |
$this->getGeneratorManager()->getCache()->set('actions.class.php', $generatedModuleName.DIRECTORY_SEPARATOR.'actions', $retval); |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
foreach ($templateFiles as $template) |
|---|
| 64 |
{ |
|---|
| 65 |
|
|---|
| 66 |
$retval = $this->evalTemplate('templates/'.$template); |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
$this->getGeneratorManager()->getCache()->set($template, $generatedModuleName.DIRECTORY_SEPARATOR.'templates', $retval); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 |
foreach ($configFiles as $config) |
|---|
| 74 |
{ |
|---|
| 75 |
|
|---|
| 76 |
$retval = $this->evalTemplate('config/'.$config); |
|---|
| 77 |
|
|---|
| 78 |
|
|---|
| 79 |
$this->getGeneratorManager()->getCache()->set($config, $generatedModuleName.DIRECTORY_SEPARATOR.'config', $retval); |
|---|
| 80 |
} |
|---|
| 81 |
} |
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
* Evaluates a template file. |
|---|
| 85 |
* |
|---|
| 86 |
* @param string The template file path |
|---|
| 87 |
* |
|---|
| 88 |
* @return string The evaluated template |
|---|
| 89 |
*/ |
|---|
| 90 |
protected function evalTemplate($templateFile) |
|---|
| 91 |
{ |
|---|
| 92 |
$templateFile = sfLoader::getGeneratorTemplate($this->getGeneratorClass(), $this->getTheme(), $templateFile); |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
ob_start(); |
|---|
| 96 |
require($templateFile); |
|---|
| 97 |
$content = ob_get_clean(); |
|---|
| 98 |
|
|---|
| 99 |
|
|---|
| 100 |
$content = $this->replacePhpMarks($content); |
|---|
| 101 |
|
|---|
| 102 |
$retval = "<?php\n". |
|---|
| 103 |
"// auto-generated by ".$this->getGeneratorClass()."\n". |
|---|
| 104 |
"// date: %s\n?>\n%s"; |
|---|
| 105 |
$retval = sprintf($retval, date('Y/m/d H:i:s'), $content); |
|---|
| 106 |
|
|---|
| 107 |
return $retval; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 |
* Replaces PHP marks by <?php ?>. |
|---|
| 112 |
* |
|---|
| 113 |
* @param string The PHP code |
|---|
| 114 |
* |
|---|
| 115 |
* @return string The converted PHP code |
|---|
| 116 |
*/ |
|---|
| 117 |
protected function replacePhpMarks($text) |
|---|
| 118 |
{ |
|---|
| 119 |
|
|---|
| 120 |
return str_replace(array('[?php', '[?=', '?]'), array('<?php', '<?php echo', '?>'), $text); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
|
|---|
| 124 |
* Gets the generator class. |
|---|
| 125 |
* |
|---|
| 126 |
* @return string The generator class |
|---|
| 127 |
*/ |
|---|
| 128 |
public function getGeneratorClass() |
|---|
| 129 |
{ |
|---|
| 130 |
return $this->generatorClass; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
* Sets the generator class. |
|---|
| 135 |
* |
|---|
| 136 |
* @param string The generator class |
|---|
| 137 |
*/ |
|---|
| 138 |
public function setGeneratorClass($generator_class) |
|---|
| 139 |
{ |
|---|
| 140 |
$this->generatorClass = $generator_class; |
|---|
| 141 |
} |
|---|
| 142 |
|
|---|
| 143 |
|
|---|
| 144 |
* Gets the sfGeneratorManager instance. |
|---|
| 145 |
* |
|---|
| 146 |
* @return string The sfGeneratorManager instance |
|---|
| 147 |
*/ |
|---|
| 148 |
protected function getGeneratorManager() |
|---|
| 149 |
{ |
|---|
| 150 |
return $this->generatorManager; |
|---|
| 151 |
} |
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
* Gets the module name of the generated module. |
|---|
| 155 |
* |
|---|
| 156 |
* @return string The module name |
|---|
| 157 |
*/ |
|---|
| 158 |
public function getGeneratedModuleName() |
|---|
| 159 |
{ |
|---|
| 160 |
return $this->generatedModuleName; |
|---|
| 161 |
} |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
* Sets the module name of the generated module. |
|---|
| 165 |
* |
|---|
| 166 |
* @param string The module name |
|---|
| 167 |
*/ |
|---|
| 168 |
public function setGeneratedModuleName($module_name) |
|---|
| 169 |
{ |
|---|
| 170 |
$this->generatedModuleName = $module_name; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
|
|---|
| 174 |
* Gets the module name. |
|---|
| 175 |
* |
|---|
| 176 |
* @return string The module name |
|---|
| 177 |
*/ |
|---|
| 178 |
public function getModuleName() |
|---|
| 179 |
{ |
|---|
| 180 |
return $this->moduleName; |
|---|
| 181 |
} |
|---|
| 182 |
|
|---|
| 183 |
|
|---|
| 184 |
* Sets the module name. |
|---|
| 185 |
* |
|---|
| 186 |
* @param string The module name |
|---|
| 187 |
*/ |
|---|
| 188 |
public function setModuleName($module_name) |
|---|
| 189 |
{ |
|---|
| 190 |
$this->moduleName = $module_name; |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
* Gets the theme name. |
|---|
| 195 |
* |
|---|
| 196 |
* @return string The theme name |
|---|
| 197 |
*/ |
|---|
| 198 |
public function getTheme() |
|---|
| 199 |
{ |
|---|
| 200 |
return $this->theme; |
|---|
| 201 |
} |
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 |
* Sets the theme name. |
|---|
| 205 |
* |
|---|
| 206 |
* @param string The theme name |
|---|
| 207 |
*/ |
|---|
| 208 |
public function setTheme($theme) |
|---|
| 209 |
{ |
|---|
| 210 |
$this->theme = $theme; |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
|
|---|
| 214 |
* Calls methods defined via the sfMixer class. |
|---|
| 215 |
* |
|---|
| 216 |
* @param string The method name |
|---|
| 217 |
* @param array The method arguments |
|---|
| 218 |
* |
|---|
| 219 |
* @return mixed The returned value of the called method |
|---|
| 220 |
* |
|---|
| 221 |
* @see sfMixer |
|---|
| 222 |
*/ |
|---|
| 223 |
public function __call($method, $arguments) |
|---|
| 224 |
{ |
|---|
| 225 |
if (!$callable = sfMixer::getCallable('sfGenerator:'.$method)) |
|---|
| 226 |
{ |
|---|
| 227 |
throw new sfException(sprintf('Call to undefined method sfGenerator::%s', $method)); |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
array_unshift($arguments, $this); |
|---|
| 231 |
|
|---|
| 232 |
return call_user_func_array($callable, $arguments); |
|---|
| 233 |
} |
|---|
| 234 |
} |
|---|
| 235 |
|
|---|