sfSymfonyCommandApplication.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * sfSymfonyCommandApplication manages the symfony CLI.
  11. *
  12. * @package symfony
  13. * @subpackage command
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id: sfSymfonyCommandApplication.class.php 20053 2009-07-09 12:49:20Z nicolas $
  16. */
  17. class sfSymfonyCommandApplication extends sfCommandApplication
  18. {
  19. protected $taskFiles = array();
  20. /**
  21. * Configures the current symfony command application.
  22. */
  23. public function configure()
  24. {
  25. if (!isset($this->options['symfony_lib_dir']))
  26. {
  27. throw new sfInitializationException('You must pass a "symfony_lib_dir" option.');
  28. }
  29. $configurationFile = getcwd().'/config/ProjectConfiguration.class.php';
  30. if (is_readable($configurationFile))
  31. {
  32. require_once $configurationFile;
  33. $configuration = new ProjectConfiguration(getcwd(), $this->dispatcher);
  34. }
  35. else
  36. {
  37. $configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
  38. }
  39. // application
  40. $this->setName('symfony');
  41. $this->setVersion(SYMFONY_VERSION);
  42. $this->loadTasks($configuration);
  43. }
  44. /**
  45. * Runs the current application.
  46. *
  47. * @param mixed $options The command line options
  48. *
  49. * @return integer 0 if everything went fine, or an error code
  50. */
  51. public function run($options = null)
  52. {
  53. $this->handleOptions($options);
  54. $arguments = $this->commandManager->getArgumentValues();
  55. if (!isset($arguments['task']))
  56. {
  57. $arguments['task'] = 'list';
  58. $this->commandOptions .= $arguments['task'];
  59. }
  60. $this->currentTask = $this->getTaskToExecute($arguments['task']);
  61. if ($this->currentTask instanceof sfCommandApplicationTask)
  62. {
  63. $this->currentTask->setCommandApplication($this);
  64. }
  65. $ret = $this->currentTask->runFromCLI($this->commandManager, $this->commandOptions);
  66. $this->currentTask = null;
  67. return $ret;
  68. }
  69. /**
  70. * Loads all available tasks.
  71. *
  72. * Looks for tasks in the symfony core, the current project and all project plugins.
  73. *
  74. * @param sfProjectConfiguration $configuration The project configuration
  75. */
  76. protected function loadTasks(sfProjectConfiguration $configuration)
  77. {
  78. // Symfony core tasks
  79. $dirs = array(sfConfig::get('sf_symfony_lib_dir').'/task');
  80. // Plugin tasks
  81. foreach ($configuration->getPluginPaths() as $path)
  82. {
  83. if (is_dir($taskPath = $path.'/lib/task'))
  84. {
  85. $dirs[] = $taskPath;
  86. }
  87. }
  88. // project tasks
  89. $dirs[] = sfConfig::get('sf_lib_dir').'/task';
  90. $finder = sfFinder::type('file')->name('*Task.class.php');
  91. foreach ($finder->in($dirs) as $file)
  92. {
  93. $this->taskFiles[basename($file, '.class.php')] = $file;
  94. }
  95. // register local autoloader for tasks
  96. spl_autoload_register(array($this, 'autoloadTask'));
  97. // require tasks
  98. foreach ($this->taskFiles as $task => $file)
  99. {
  100. // forces autoloading of each task class
  101. class_exists($task, true);
  102. }
  103. // unregister local autoloader
  104. spl_autoload_unregister(array($this, 'autoloadTask'));
  105. }
  106. /**
  107. * Autoloads a task class
  108. *
  109. * @param string $class The task class name
  110. *
  111. * @return Boolean
  112. */
  113. public function autoloadTask($class)
  114. {
  115. if (isset($this->taskFiles[$class]))
  116. {
  117. require_once $this->taskFiles[$class];
  118. return true;
  119. }
  120. return false;
  121. }
  122. /**
  123. * @see sfCommandApplication
  124. */
  125. public function getLongVersion()
  126. {
  127. return sprintf('%s version %s (%s)', $this->getName(), $this->formatter->format($this->getVersion(), 'INFO'), sfConfig::get('sf_symfony_lib_dir'))."\n";
  128. }
  129. }