1. sfGenerator.class.php
  2. /** * sfGenerator is the abstract base class for all generators. * * @package symfony * @subpackage generator * @author Fabien Potencier * @version SVN: $Id: sfGenerator.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $ */
  3. abstract class sfGenerator
  4. {
  5. protected
  6. $generatorClass = '',
  7. $generatorManager = null,
  8. $generatedModuleName = '',
  9. $theme = 'default',
  10. $moduleName = '';
  11. /**
  12. * Class constructor.
  13. *
  14. * @see initialize()
  15. */
  16. public function __construct(sfGeneratorManager $generatorManager)
  17. {
  18. $this->initialize($generatorManager);
  19. }
  20. /**
  21. * Initializes the current sfGenerator instance.
  22. *
  23. * @param sfGeneratorManager $generatorManager A sfGeneratorManager instance
  24. */
  25. public function initialize(sfGeneratorManager $generatorManager)
  26. {
  27. $this->generatorManager = $generatorManager;
  28. }
  29. /**
  30. * Generates classes and templates.
  31. *
  32. * @param array $params An array of parameters
  33. *
  34. * @return string The cache for the configuration file
  35. */
  36. abstract public function generate($params = array());
  37. /**
  38. * Generates PHP files for a given module name.
  39. *
  40. * @param string $generatedModuleName The name of module name to generate
  41. * @param array $files A list of template files to generate
  42. */
  43. protected function generatePhpFiles($generatedModuleName, $files = array())
  44. {
  45. foreach ($files as $file)
  46. {
  47. $this->getGeneratorManager()->save($generatedModuleName.'/'.$file, $this->evalTemplate($file));
  48. }
  49. }
  50. /**
  51. * Evaluates a template file.
  52. *
  53. * @param string $templateFile The template file path
  54. *
  55. * @return string The evaluated template
  56. */
  57. protected function evalTemplate($templateFile)
  58. {
  59. $templateFile = $this->generatorManager->getConfiguration()->getGeneratorTemplate($this->getGeneratorClass(), $this->getTheme(), $templateFile);
  60. // eval template file
  61. ob_start();
  62. require($templateFile);
  63. $content = ob_get_clean();
  64. // replace [?php and ?]
  65. return $this->replacePhpMarks($content);
  66. }
  67. /**
  68. * Replaces PHP marks by <?php ?>.
  69. *
  70. * @param string $text The PHP code
  71. *
  72. * @return string The converted PHP code
  73. */
  74. protected function replacePhpMarks($text)
  75. {
  76. // replace [?php and ?]
  77. return str_replace(array('[?php', '[?=', '?]'), array('<?php', '<?php echo', '?>'), $text);
  78. }
  79. /**
  80. * Gets the generator class.
  81. *
  82. * @return string The generator class
  83. */
  84. public function getGeneratorClass()
  85. {
  86. return $this->generatorClass;
  87. }
  88. /**
  89. * Sets the generator class.
  90. *
  91. * @param string $generatorClass The generator class
  92. */
  93. public function setGeneratorClass($generatorClass)
  94. {
  95. $this->generatorClass = $generatorClass;
  96. }
  97. /**
  98. * Gets the sfGeneratorManager instance.
  99. *
  100. * @return string The sfGeneratorManager instance
  101. */
  102. protected function getGeneratorManager()
  103. {
  104. return $this->generatorManager;
  105. }
  106. /**
  107. * Gets the module name of the generated module.
  108. *
  109. * @return string The module name
  110. */
  111. public function getGeneratedModuleName()
  112. {
  113. return $this->generatedModuleName;
  114. }
  115. /**
  116. * Sets the module name of the generated module.
  117. *
  118. * @param string $moduleName The module name
  119. */
  120. public function setGeneratedModuleName($moduleName)
  121. {
  122. $this->generatedModuleName = $moduleName;
  123. }
  124. /**
  125. * Gets the module name.
  126. *
  127. * @return string The module name
  128. */
  129. public function getModuleName()
  130. {
  131. return $this->moduleName;
  132. }
  133. /**
  134. * Sets the module name.
  135. *
  136. * @param string $moduleName The module name
  137. */
  138. public function setModuleName($moduleName)
  139. {
  140. $this->moduleName = $moduleName;
  141. }
  142. /**
  143. * Gets the theme name.
  144. *
  145. * @return string The theme name
  146. */
  147. public function getTheme()
  148. {
  149. return $this->theme;
  150. }
  151. /**
  152. * Sets the theme name.
  153. *
  154. * @param string $theme The theme name
  155. */
  156. public function setTheme($theme)
  157. {
  158. $this->theme = $theme;
  159. }
  160. }

Debug toolbar