/package/app/app/symfony/generator/sfGenerator.class.php

https://github.com/richhl/kalturaCE · PHP · 234 lines · 92 code · 28 blank · 114 comment · 1 complexity · 5b01fe988a44ab1cd445211a190f554f MD5 · raw file

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