sfBaseTask.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 16171 2009-03-11 08:04:47Z fabien $
  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') : ($commandManager->getOptionSet()->hasOption('application') ? $commandManager->getOptionValue('application') : null);
  37. $env = $commandManager->getOptionSet()->hasOption('env') ? $commandManager->getOptionValue('env') : 'test';
  38. if (true === $application)
  39. {
  40. $application = $this->getFirstApplication();
  41. if ($commandManager->getOptionSet()->hasOption('application'))
  42. {
  43. $commandManager->setOption($commandManager->getOptionSet()->getOption('application'), $application);
  44. }
  45. }
  46. $this->configuration = $this->createConfiguration($application, $env);
  47. if (!is_null($this->commandApplication) && !$this->commandApplication->withTrace())
  48. {
  49. sfConfig::set('sf_logging_enabled', false);
  50. }
  51. $ret = $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues());
  52. $this->dispatcher->notify(new sfEvent($this, 'command.post_command'));
  53. return $ret;
  54. }
  55. /**
  56. * Returns the filesystem instance.
  57. *
  58. * @return sfFilesystem A sfFilesystem instance
  59. */
  60. public function getFilesystem()
  61. {
  62. if (!isset($this->filesystem))
  63. {
  64. if (is_null($this->commandApplication) || $this->commandApplication->isVerbose())
  65. {
  66. $this->filesystem = new sfFilesystem($this->dispatcher, $this->formatter);
  67. }
  68. else
  69. {
  70. $this->filesystem = new sfFilesystem();
  71. }
  72. }
  73. return $this->filesystem;
  74. }
  75. /**
  76. * Checks if the current directory is a symfony project directory.
  77. *
  78. * @return true if the current directory is a symfony project directory, false otherwise
  79. */
  80. public function checkProjectExists()
  81. {
  82. if (!file_exists('symfony'))
  83. {
  84. throw new sfException('You must be in a symfony project directory.');
  85. }
  86. }
  87. /**
  88. * Checks if an application exists.
  89. *
  90. * @param string $app The application name
  91. *
  92. * @return bool true if the application exists, false otherwise
  93. */
  94. public function checkAppExists($app)
  95. {
  96. if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app))
  97. {
  98. throw new sfException(sprintf('Application "%s" does not exist', $app));
  99. }
  100. }
  101. /**
  102. * Checks if a module exists.
  103. *
  104. * @param string $app The application name
  105. * @param string $module The module name
  106. *
  107. * @return bool true if the module exists, false otherwise
  108. */
  109. public function checkModuleExists($app, $module)
  110. {
  111. if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app.'/modules/'.$module))
  112. {
  113. throw new sfException(sprintf('Module "%s/%s" does not exist.', $app, $module));
  114. }
  115. }
  116. /**
  117. * Creates a configuration object.
  118. *
  119. * @param string $application The application name
  120. * @param string $env The environment name
  121. *
  122. * @return sfProjectConfiguration A sfProjectConfiguration instance
  123. */
  124. protected function createConfiguration($application, $env)
  125. {
  126. if (!is_null($application))
  127. {
  128. $this->checkAppExists($application);
  129. require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
  130. $configuration = ProjectConfiguration::getApplicationConfiguration($application, $env, true, null, $this->dispatcher);
  131. }
  132. else
  133. {
  134. if (file_exists(sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php'))
  135. {
  136. require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
  137. $configuration = new ProjectConfiguration(null, $this->dispatcher);
  138. }
  139. else
  140. {
  141. $configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
  142. }
  143. if (!is_null($env))
  144. {
  145. sfConfig::set('sf_environment', $env);
  146. }
  147. $autoloader = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
  148. $autoloader->addFiles(sfFinder::type('file')->prune('symfony')->follow_link()->name('*.php')->in(sfConfig::get('sf_lib_dir')));
  149. $autoloader->register();
  150. }
  151. return $configuration;
  152. }
  153. /**
  154. * Returns the first application in apps.
  155. *
  156. * @return string The Application name
  157. */
  158. protected function getFirstApplication()
  159. {
  160. if (count($dirs = sfFinder::type('dir')->ignore_version_control()->maxdepth(0)->follow_link()->relative()->in(sfConfig::get('sf_apps_dir'))))
  161. {
  162. return $dirs[0];
  163. }
  164. return null;
  165. }
  166. }