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

Debug toolbar