sfBaseTask.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * Base class for all symfony tasks.
  11. *
  12. * @package symfony
  13. * @subpackage task
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfBaseTask.class.php 16558 2009-03-24 18:17:58Z Kris.Wallsmith $
  16. */
  17. abstract class sfBaseTask extends sfCommandApplicationTask
  18. {
  19. protected
  20. $configuration = null;
  21. /**
  22. * @see sfTask
  23. */
  24. protected function doRun(sfCommandManager $commandManager, $options)
  25. {
  26. $event = $this->dispatcher->filter(new sfEvent($this, 'command.filter_options', array('command_manager' => $commandManager)), $options);
  27. $options = $event->getReturnValue();
  28. $this->process($commandManager, $options);
  29. $event = new sfEvent($this, 'command.pre_command', array('arguments' => $commandManager->getArgumentValues(), 'options' => $commandManager->getOptionValues()));
  30. $this->dispatcher->notifyUntil($event);
  31. if ($event->isProcessed())
  32. {
  33. return $event->getReturnValue();
  34. }
  35. $this->checkProjectExists();
  36. $application = $commandManager->getArgumentSet()->hasArgument('application') ? $commandManager->getArgumentValue('application') : null;
  37. $env = $commandManager->getOptionSet()->hasOption('env') ? $commandManager->getOptionValue('env') : 'test';
  38. if (!is_null($application))
  39. {
  40. $this->checkAppExists($application);
  41. require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
  42. $this->configuration = ProjectConfiguration::getApplicationConfiguration($application, $env, true, null, $this->dispatcher);
  43. }
  44. else
  45. {
  46. if (file_exists(sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php'))
  47. {
  48. require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
  49. $this->configuration = new ProjectConfiguration(null, $this->dispatcher);
  50. }
  51. else
  52. {
  53. $this->configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
  54. }
  55. }
  56. $autoloader = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
  57. foreach ($this->configuration->getModelDirs() as $dir)
  58. {
  59. $autoloader->addDirectory($dir);
  60. }
  61. if (!is_null($this->commandApplication) && !$this->commandApplication->withTrace())
  62. {
  63. sfConfig::set('sf_logging_enabled', false);
  64. }
  65. $ret = $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues());
  66. $this->dispatcher->notify(new sfEvent($this, 'command.post_command'));
  67. return $ret;
  68. }
  69. /**
  70. * Returns the filesystem instance.
  71. *
  72. * @return sfFilesystem A sfFilesystem instance
  73. */
  74. public function getFilesystem()
  75. {
  76. if (!isset($this->filesystem))
  77. {
  78. if (is_null($this->commandApplication) || $this->commandApplication->isVerbose())
  79. {
  80. $this->filesystem = new sfFilesystem($this->dispatcher, $this->formatter);
  81. }
  82. else
  83. {
  84. $this->filesystem = new sfFilesystem();
  85. }
  86. }
  87. return $this->filesystem;
  88. }
  89. /**
  90. * Checks if the current directory is a symfony project directory.
  91. *
  92. * @return true if the current directory is a symfony project directory, false otherwise
  93. */
  94. public function checkProjectExists()
  95. {
  96. if (!file_exists('symfony'))
  97. {
  98. throw new sfException('You must be in a symfony project directory.');
  99. }
  100. }
  101. /**
  102. * Checks if an application exists.
  103. *
  104. * @param string $app The application name
  105. *
  106. * @return bool true if the application exists, false otherwise
  107. */
  108. public function checkAppExists($app)
  109. {
  110. if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app))
  111. {
  112. throw new sfException(sprintf('Application "%s" does not exist', $app));
  113. }
  114. }
  115. /**
  116. * Checks if a module exists.
  117. *
  118. * @param string $app The application name
  119. * @param string $module The module name
  120. *
  121. * @return bool true if the module exists, false otherwise
  122. */
  123. public function checkModuleExists($app, $module)
  124. {
  125. if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app.'/modules/'.$module))
  126. {
  127. throw new sfException(sprintf('Module "%s/%s" does not exist.', $app, $module));
  128. }
  129. }
  130. }