sfGenerator.class.php 4.3 KB

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