123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- abstract class sfBaseTask extends sfCommandApplicationTask
- {
- protected
- $configuration = null;
-
- protected function doRun(sfCommandManager $commandManager, $options)
- {
- $event = $this->dispatcher->filter(new sfEvent($this, 'command.filter_options', array('command_manager' => $commandManager)), $options);
- $options = $event->getReturnValue();
- $this->process($commandManager, $options);
- $event = new sfEvent($this, 'command.pre_command', array('arguments' => $commandManager->getArgumentValues(), 'options' => $commandManager->getOptionValues()));
- $this->dispatcher->notifyUntil($event);
- if ($event->isProcessed())
- {
- return $event->getReturnValue();
- }
- $this->checkProjectExists();
- $application = $commandManager->getArgumentSet()->hasArgument('application') ? $commandManager->getArgumentValue('application') : ($commandManager->getOptionSet()->hasOption('application') ? $commandManager->getOptionValue('application') : null);
- $env = $commandManager->getOptionSet()->hasOption('env') ? $commandManager->getOptionValue('env') : 'test';
- if (true === $application)
- {
- $application = $this->getFirstApplication();
- if ($commandManager->getOptionSet()->hasOption('application'))
- {
- $commandManager->setOption($commandManager->getOptionSet()->getOption('application'), $application);
- }
- }
- $this->configuration = $this->createConfiguration($application, $env);
- if (!is_null($this->commandApplication) && !$this->commandApplication->withTrace())
- {
- sfConfig::set('sf_logging_enabled', false);
- }
- $ret = $this->execute($commandManager->getArgumentValues(), $commandManager->getOptionValues());
- $this->dispatcher->notify(new sfEvent($this, 'command.post_command'));
- return $ret;
- }
-
- public function getFilesystem()
- {
- if (!isset($this->filesystem))
- {
- if (is_null($this->commandApplication) || $this->commandApplication->isVerbose())
- {
- $this->filesystem = new sfFilesystem($this->dispatcher, $this->formatter);
- }
- else
- {
- $this->filesystem = new sfFilesystem();
- }
- }
- return $this->filesystem;
- }
-
- public function checkProjectExists()
- {
- if (!file_exists('symfony'))
- {
- throw new sfException('You must be in a symfony project directory.');
- }
- }
-
- public function checkAppExists($app)
- {
- if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app))
- {
- throw new sfException(sprintf('Application "%s" does not exist', $app));
- }
- }
-
- public function checkModuleExists($app, $module)
- {
- if (!is_dir(sfConfig::get('sf_apps_dir').'/'.$app.'/modules/'.$module))
- {
- throw new sfException(sprintf('Module "%s/%s" does not exist.', $app, $module));
- }
- }
-
- protected function createConfiguration($application, $env)
- {
- if (!is_null($application))
- {
- $this->checkAppExists($application);
- require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
- $configuration = ProjectConfiguration::getApplicationConfiguration($application, $env, true, null, $this->dispatcher);
- }
- else
- {
- if (file_exists(sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php'))
- {
- require_once sfConfig::get('sf_config_dir').'/ProjectConfiguration.class.php';
- $configuration = new ProjectConfiguration(null, $this->dispatcher);
- }
- else
- {
- $configuration = new sfProjectConfiguration(getcwd(), $this->dispatcher);
- }
- if (!is_null($env))
- {
- sfConfig::set('sf_environment', $env);
- }
- $autoloader = sfSimpleAutoload::getInstance(sfConfig::get('sf_cache_dir').'/project_autoload.cache');
- $autoloader->addFiles(sfFinder::type('file')->prune('symfony')->follow_link()->name('*.php')->in(sfConfig::get('sf_lib_dir')));
- $autoloader->register();
- }
- return $configuration;
- }
-
- protected function getFirstApplication()
- {
- if (count($dirs = sfFinder::type('dir')->ignore_version_control()->maxdepth(0)->follow_link()->relative()->in(sfConfig::get('sf_apps_dir'))))
- {
- return $dirs[0];
- }
- return null;
- }
- }
|