sfGeneratorManager.class.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 10135 2008-07-05 17:28:27Z FabianLange $
  16. */
  17. class sfGeneratorManager
  18. {
  19. protected
  20. $configuration = null;
  21. /**
  22. * Class constructor.
  23. *
  24. * @param sfProjectConfiguration $configuration A sfProjectConfiguration instance
  25. *
  26. * @see initialize()
  27. */
  28. public function __construct(sfProjectConfiguration $configuration)
  29. {
  30. $this->initialize($configuration);
  31. }
  32. /**
  33. * Initializes the sfGeneratorManager instance.
  34. *
  35. * @param sfProjectConfiguration $configuration A sfProjectConfiguration instance
  36. */
  37. public function initialize(sfProjectConfiguration $configuration)
  38. {
  39. $this->configuration = $configuration;
  40. }
  41. /**
  42. * Returns the current configuration instance.
  43. *
  44. * @return sfProjectConfiguration A sfProjectConfiguration instance
  45. */
  46. public function getConfiguration()
  47. {
  48. return $this->configuration;
  49. }
  50. public function save($path, $content)
  51. {
  52. $path = sfConfig::get('sf_module_cache_dir').DIRECTORY_SEPARATOR.$path;
  53. if (!is_dir(dirname($path)))
  54. {
  55. $current_umask = umask(0000);
  56. if (false === @mkdir(dirname($path), 0777, true))
  57. {
  58. throw new sfCacheException(sprintf('Failed to make cache directory "%s".', dirname($path)));
  59. }
  60. umask($current_umask);
  61. }
  62. if (false === $ret = @file_put_contents($path, $content))
  63. {
  64. throw new sfCacheException(sprintf('Failed to write cache file "%s".', $path));
  65. }
  66. return $ret;
  67. }
  68. /**
  69. * Generates classes and templates for a given generator class.
  70. *
  71. * @param string $generatorClass The generator class name
  72. * @param array $param An array of parameters
  73. *
  74. * @return string The cache for the configuration file
  75. */
  76. public function generate($generatorClass, $param)
  77. {
  78. $generator = new $generatorClass($this);
  79. return $generator->generate($param);
  80. }
  81. }