sfGeneratorManager.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * sfGeneratorManager helps generate classes, views and templates for scaffolding, admin interface, ...
  11. *
  12. * @package symfony
  13. * @subpackage generator
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfGeneratorManager.class.php 12264 2008-10-20 09:41:28Z fabien $
  16. */
  17. class sfGeneratorManager
  18. {
  19. protected
  20. $configuration = null,
  21. $basePath = null;
  22. /**
  23. * Class constructor.
  24. *
  25. * @param sfProjectConfiguration $configuration A sfProjectConfiguration instance
  26. * @param string $basePath The base path for file generation
  27. *
  28. * @see initialize()
  29. */
  30. public function __construct(sfProjectConfiguration $configuration, $basePath = null)
  31. {
  32. $this->configuration = $configuration;
  33. $this->basePath = $basePath;
  34. }
  35. /**
  36. * Initializes the sfGeneratorManager instance.
  37. *
  38. * @param sfProjectConfiguration $configuration A sfProjectConfiguration instance
  39. * @deprecated
  40. */
  41. public function initialize(sfProjectConfiguration $configuration)
  42. {
  43. // DEPRECATED
  44. }
  45. /**
  46. * Returns the current configuration instance.
  47. *
  48. * @return sfProjectConfiguration A sfProjectConfiguration instance
  49. */
  50. public function getConfiguration()
  51. {
  52. return $this->configuration;
  53. }
  54. /**
  55. * Gets the base path to use when generating files.
  56. *
  57. * @return string The base path
  58. */
  59. public function getBasePath()
  60. {
  61. if (is_null($this->basePath))
  62. {
  63. // for BC
  64. $this->basePath = sfConfig::get('sf_module_cache_dir');
  65. }
  66. return $this->basePath;
  67. }
  68. /**
  69. * Sets the base path to use when generating files.
  70. *
  71. * @param string $basePath The base path
  72. */
  73. public function setBasePath($basePath)
  74. {
  75. $this->basePath = $basePath;
  76. }
  77. /**
  78. * Saves some content.
  79. *
  80. * @param string $path The relative path
  81. * @param string $content The content
  82. */
  83. public function save($path, $content)
  84. {
  85. $path = $this->getBasePath().DIRECTORY_SEPARATOR.$path;
  86. if (!is_dir(dirname($path)))
  87. {
  88. $current_umask = umask(0000);
  89. if (false === @mkdir(dirname($path), 0777, true))
  90. {
  91. throw new sfCacheException(sprintf('Failed to make cache directory "%s".', dirname($path)));
  92. }
  93. umask($current_umask);
  94. }
  95. if (false === $ret = @file_put_contents($path, $content))
  96. {
  97. throw new sfCacheException(sprintf('Failed to write cache file "%s".', $path));
  98. }
  99. return $ret;
  100. }
  101. /**
  102. * Generates classes and templates for a given generator class.
  103. *
  104. * @param string $generatorClass The generator class name
  105. * @param array $param An array of parameters
  106. *
  107. * @return string The cache for the configuration file
  108. */
  109. public function generate($generatorClass, $param)
  110. {
  111. $generator = new $generatorClass($this);
  112. return $generator->generate($param);
  113. }
  114. }