add return value for console commands

This commit is contained in:
Nicolas Le Goff
2012-02-10 19:17:05 +01:00
parent 6f837c7407
commit 7f0c006730
15 changed files with 178 additions and 94 deletions

View File

@@ -39,7 +39,7 @@ class module_console_aboutAuthors extends Command
{ {
$output->writeln(file_get_contents(__DIR__ . '/../../../../AUTHORS')); $output->writeln(file_get_contents(__DIR__ . '/../../../../AUTHORS'));
return; return 0;
} }
} }

View File

@@ -39,7 +39,7 @@ class module_console_aboutLicense extends Command
{ {
$output->writeln(file_get_contents(__DIR__ . '/../../../../LICENSE')); $output->writeln(file_get_contents(__DIR__ . '/../../../../LICENSE'));
return; return 0;
} }
} }

View File

@@ -66,17 +66,17 @@ class module_console_fileConfigCheck extends Command
$output->writeln(""); $output->writeln("");
$this->env = $env; $this->env = $env;
$this->initTests(); $this->initTests($output);
$this->prepareTests($output); $this->prepareTests($output);
$this->runTests($output); $this->runTests($output);
} }
exit(1); return 0;
} }
private function initTests() private function initTests(OutputInterface $output)
{ {
$spec = new Core\Configuration\Application(); $spec = new Core\Configuration\Application();
$parser = new Core\Configuration\Parser\Yaml(); $parser = new Core\Configuration\Parser\Yaml();
@@ -86,7 +86,8 @@ class module_console_fileConfigCheck extends Command
if (!$this->configuration->isInstalled()) if (!$this->configuration->isInstalled())
{ {
exit(sprintf("\nPhraseanet is not installed\n")); $output->writeln(sprintf("\nPhraseanet is not installed\n"));
return 1;
} }
} }
@@ -109,12 +110,14 @@ class module_console_fileConfigCheck extends Command
$previous->getMessage() : 'Unknown.' $previous->getMessage() : 'Unknown.'
) )
); );
exit(sprintf("\nConfig check test suite can not continue please correct FATAL error and relaunch.\n")); $output->writeln(sprintf("\nConfig check test suite can not continue please correct FATAL error and relaunch.\n"));
return 1;
} }
} }
private function runTests(OutputInterface $output) private function runTests(OutputInterface $output)
{ {
$nbErrors = 0;
foreach ($this->testSuite as $test) foreach ($this->testSuite as $test)
{ {
try try
@@ -123,6 +126,7 @@ class module_console_fileConfigCheck extends Command
} }
catch (\Exception $e) catch (\Exception $e)
{ {
$nbErrors++;
$previous = $e->getPrevious(); $previous = $e->getPrevious();
$output->writeln(sprintf( $output->writeln(sprintf(
@@ -134,6 +138,7 @@ class module_console_fileConfigCheck extends Command
); );
} }
} }
return (int) ($nbErrors > 0);
} }
private function checkParse(OutputInterface $output) private function checkParse(OutputInterface $output)

View File

@@ -25,7 +25,6 @@ use Alchemy\Phrasea\Core;
*/ */
class module_console_fileEnsureProductionSetting extends Command class module_console_fileEnsureProductionSetting extends Command
{ {
protected $hasErrors = false;
const ALERT = 1; const ALERT = 1;
const ERROR = 0; const ERROR = 0;
@@ -61,16 +60,16 @@ class module_console_fileEnsureProductionSetting extends Command
$output->writeln("============================="); $output->writeln("=============================");
$output->writeln(""); $output->writeln("");
$this->initTests(); $this->initTests($output);
$this->prepareTests($output); $this->prepareTests($output);
$this->runTests($output); $this->runTests($output);
exit(1); return 0;
} }
private function initTests() private function initTests(OutputInterface $output)
{ {
$spec = new Core\Configuration\Application(); $spec = new Core\Configuration\Application();
$parser = new Core\Configuration\Parser\Yaml(); $parser = new Core\Configuration\Parser\Yaml();
@@ -80,7 +79,8 @@ class module_console_fileEnsureProductionSetting extends Command
if (!$this->configuration->isInstalled()) if (!$this->configuration->isInstalled())
{ {
exit(sprintf("\nPhraseanet is not installed\n")); $output->writeln(sprintf("\nPhraseanet is not installed\n"));
return 1;
} }
} }
@@ -103,7 +103,8 @@ class module_console_fileEnsureProductionSetting extends Command
$previous->getMessage() : 'Unknown.' $previous->getMessage() : 'Unknown.'
) )
); );
exit(sprintf("\nCheck test suite can not continue please correct FATAL error and relaunch.\n")); $output->writeln(sprintf("\nCheck test suite can not continue please correct FATAL error and relaunch.\n"));
return 1;
} }
} }
@@ -136,6 +137,8 @@ class module_console_fileEnsureProductionSetting extends Command
$output->writeln("<info>Your production settings are setted correctly !</info>"); $output->writeln("<info>Your production settings are setted correctly !</info>");
$output->writeln(""); $output->writeln("");
} }
return (int) ($nbErrors > 0);
} }
private function checkParse(OutputInterface $output) private function checkParse(OutputInterface $output)

View File

@@ -1,4 +1,5 @@
<?php <?php
/* /*
* This file is part of Phraseanet * This file is part of Phraseanet
* *
@@ -40,17 +41,24 @@ class module_console_schedulerStart extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
if(!setup::is_installed()) if (!setup::is_installed())
{ {
throw new RuntimeException('Phraseanet is not set up'); $output->writeln('Phraseanet is not set up');
return 1;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php'; require_once __DIR__ . '/../../../../lib/bootstrap.php';
$scheduler = new task_Scheduler();
$scheduler->run($output, true); try
{
return; $scheduler = new task_Scheduler();
$scheduler->run($output, true);
}
catch (\Exception $e)
{
return 1;
}
} }
} }

View File

@@ -24,6 +24,7 @@ use Symfony\Component\Console\Command\Command;
class module_console_schedulerState extends Command class module_console_schedulerState extends Command
{ {
public function __construct($name = null) public function __construct($name = null)
{ {
parent::__construct($name); parent::__construct($name);
@@ -35,26 +36,37 @@ class module_console_schedulerState extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
if(!setup::is_installed()) if (!setup::is_installed())
{ {
throw new RuntimeException('Phraseanet is not set up'); $output->writeln('Phraseanet is not set up');
return 1;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php'; require_once __DIR__ . '/../../../../lib/bootstrap.php';
$appbox = appbox::get_instance(); try
$task_manager = new task_manager($appbox); {
$appbox = appbox::get_instance();
$state = $task_manager->get_scheduler_state(); $task_manager = new task_manager($appbox);
$state = $task_manager->get_scheduler_state();
if ($state['schedstatus'] == 'started') if ($state['schedstatus'] == 'started')
$output->writeln(sprintf( {
'Scheduler is %s on pid %d' $output->writeln(sprintf(
, $state['schedstatus'] 'Scheduler is %s on pid %d'
, $state['schedpid'] , $state['schedstatus']
)); , $state['schedpid']
else ));
$output->writeln(sprintf('Scheduler is %s', $state['schedstatus'])); }
else
{
$output->writeln(sprintf('Scheduler is %s', $state['schedstatus']));
}
return 0;
}
catch(\Exception $e)
{
return 1;
}
return; return;
} }

View File

@@ -33,19 +33,28 @@ class module_console_schedulerStop extends Command
return $this; return $this;
} }
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
if(!setup::is_installed()) if (!setup::is_installed())
{ {
throw new RuntimeException('Phraseanet is not set up'); $output->writeln('Phraseanet is not set up');
return 1;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php'; require_once __DIR__ . '/../../../../lib/bootstrap.php';
$appbox = appbox::get_instance(); try
$task_manager = new task_manager($appbox); {
$appbox = appbox::get_instance();
$task_manager->set_sched_status(task_manager::STATUS_SCHED_TOSTOP); $task_manager = new task_manager($appbox);
$task_manager->set_sched_status(task_manager::STATUS_SCHED_TOSTOP);
return 0;
}
catch (\Exception $e)
{
return 1;
}
return; return;
} }

View File

@@ -45,7 +45,8 @@ class module_console_systemBackupDB extends Command
{ {
if (!setup::is_installed()) if (!setup::is_installed())
{ {
throw new RuntimeException('Phraseanet is not set up'); $output->writeln('Argument must be an Id.');
return 1;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php'; require_once __DIR__ . '/../../../../lib/bootstrap.php';
@@ -54,14 +55,16 @@ class module_console_systemBackupDB extends Command
$appbox = appbox::get_instance(); $appbox = appbox::get_instance();
$this->dump_base($appbox, $input, $output); $ok = true;
$ok = $this->dump_base($appbox, $input, $output) && $ok;
foreach ($appbox->get_databoxes() as $databox) foreach ($appbox->get_databoxes() as $databox)
{ {
$this->dump_base($databox, $input, $output); $ok = $this->dump_base($databox, $input, $output) && $ok;
} }
return; return (int) !$ok;
} }
protected function dump_base(base $base, InputInterface $input, OutputInterface $output) protected function dump_base(base $base, InputInterface $input, OutputInterface $output)
@@ -91,11 +94,17 @@ class module_console_systemBackupDB extends Command
system($command); system($command);
if (file_exists($filename) && filesize($filename) > 0) if (file_exists($filename) && filesize($filename) > 0)
{
$output->writeln('OK'); $output->writeln('OK');
return true;
}
else else
{
$output->writeln('<error>Failed</error>'); $output->writeln('<error>Failed</error>');
return false;
}
return;
} }
} }

View File

@@ -46,8 +46,8 @@ class module_console_systemClearCache extends Command
->in(array( ->in(array(
__DIR__ . '/../../../../tmp/cache_minify/' __DIR__ . '/../../../../tmp/cache_minify/'
, __DIR__ . '/../../../../tmp/cache_twig/' , __DIR__ . '/../../../../tmp/cache_twig/'
)) ));
;
$count = 1; $count = 1;
foreach ($finder as $file) foreach ($finder as $file)
{ {
@@ -63,8 +63,8 @@ class module_console_systemClearCache extends Command
, __DIR__ . '/../../../../tmp/cache_twig' , __DIR__ . '/../../../../tmp/cache_twig'
)) ))
->exclude('.git') ->exclude('.git')
->exclude('.svn') ->exclude('.svn');
;
foreach ($finder as $file) foreach ($finder as $file)
{ {
$dirs[$file->getPathname()] = $file->getPathname(); $dirs[$file->getPathname()] = $file->getPathname();
@@ -93,7 +93,7 @@ class module_console_systemClearCache extends Command
$output->write('Finished !', true); $output->write('Finished !', true);
return; return 0;
} }
} }

View File

@@ -42,15 +42,17 @@ class module_console_systemConfigCheck extends Command
$output->writeln('<error>YOU MUST ENABLE GETTEXT SUPPORT TO USE PHRASEANET</error>'); $output->writeln('<error>YOU MUST ENABLE GETTEXT SUPPORT TO USE PHRASEANET</error>');
$output->writeln('Canceled'); $output->writeln('Canceled');
return; return 1;
} }
$ok = true;
if (setup::is_installed()) if (setup::is_installed())
{ {
$registry = registry::get_instance(); $registry = registry::get_instance();
$output->writeln(_('*** CHECK BINARY CONFIGURATION ***')); $output->writeln(_('*** CHECK BINARY CONFIGURATION ***'));
$this->processConstraints(setup::check_binaries($registry), $output); $ok = $this->processConstraints(setup::check_binaries($registry), $output) && $ok;
$output->writeln(""); $output->writeln("");
} }
else else
@@ -58,51 +60,65 @@ class module_console_systemConfigCheck extends Command
$registry = new Setup_Registry(); $registry = new Setup_Registry();
} }
$output->writeln(_('*** FILESYSTEM CONFIGURATION ***')); $output->writeln(_('*** FILESYSTEM CONFIGURATION ***'));
$this->processConstraints(setup::check_writability($registry), $output); $ok = $this->processConstraints(setup::check_writability($registry), $output) && $ok;
$output->writeln(""); $output->writeln("");
$output->writeln(_('*** CHECK CACHE OPCODE ***')); $output->writeln(_('*** CHECK CACHE OPCODE ***'));
$this->processConstraints(setup::check_cache_opcode(), $output); $ok = $this->processConstraints(setup::check_cache_opcode(), $output) && $ok;
$output->writeln(""); $output->writeln("");
$output->writeln(_('*** CHECK CACHE SERVER ***')); $output->writeln(_('*** CHECK CACHE SERVER ***'));
$this->processConstraints(setup::check_cache_server(), $output); $ok = $this->processConstraints(setup::check_cache_server(), $output) && $ok;
$output->writeln(""); $output->writeln("");
$output->writeln(_('*** CHECK PHP CONFIGURATION ***')); $output->writeln(_('*** CHECK PHP CONFIGURATION ***'));
$this->processConstraints(setup::check_php_configuration(), $output); $ok = $this->processConstraints(setup::check_php_configuration(), $output) && $ok;
$output->writeln(""); $output->writeln("");
$output->writeln(_('*** CHECK PHP EXTENSIONS ***')); $output->writeln(_('*** CHECK PHP EXTENSIONS ***'));
$this->processConstraints(setup::check_php_extension(), $output); $ok = $this->processConstraints(setup::check_php_extension(), $output) && $ok;
$output->writeln(""); $output->writeln("");
$output->writeln(_('*** CHECK PHRASEA ***')); $output->writeln(_('*** CHECK PHRASEA ***'));
$this->processConstraints(setup::check_phrasea(), $output); $ok = $this->processConstraints(setup::check_phrasea(), $output) && $ok;
$output->writeln(""); $output->writeln("");
$output->writeln(_('*** CHECK SYSTEM LOCALES ***')); $output->writeln(_('*** CHECK SYSTEM LOCALES ***'));
$this->processConstraints(setup::check_system_locales(), $output); $ok = $this->processConstraints(setup::check_system_locales(), $output) && $ok;
$output->writeln(""); $output->writeln("");
$output->write('Finished !', true); $output->write('Finished !', true);
return; return (int)!$ok;
} }
protected function processConstraints(Setup_ConstraintsIterator $constraints, OutputInterface &$output) protected function processConstraints(Setup_ConstraintsIterator $constraints, OutputInterface &$output)
{ {
$hasError = false;
foreach ($constraints as $constraint) foreach ($constraints as $constraint)
{ {
$this->processConstraint($constraint, $output); if (!$hasError && !$this->processConstraint($constraint, $output))
{
$hasError = true;
}
} }
return !$hasError;
} }
protected function processConstraint(Setup_Constraint $constraint, OutputInterface &$output) protected function processConstraint(Setup_Constraint $constraint, OutputInterface &$output)
{ {
$ok = true;
if ($constraint->is_ok()) if ($constraint->is_ok())
{
$output->writeln("\t\t<info>" . $constraint->get_message() . '</info>'); $output->writeln("\t\t<info>" . $constraint->get_message() . '</info>');
}
elseif ($constraint->is_blocker()) elseif ($constraint->is_blocker())
{
$output->writeln("\t!!!\t<error>" . $constraint->get_message() . '</error>'); $output->writeln("\t!!!\t<error>" . $constraint->get_message() . '</error>');
$ok = false;
}
else else
{
$output->writeln("\t/!\\\t<comment>" . $constraint->get_message() . '</comment>'); $output->writeln("\t/!\\\t<comment>" . $constraint->get_message() . '</comment>');
}
return; return $ok;
} }
} }

View File

@@ -65,7 +65,7 @@ class module_console_systemMailCheck extends Command
$output->write('Finished !', true); $output->write('Finished !', true);
return; return 0;
} }
protected function manage_group($email, $users, $output, $appbox) protected function manage_group($email, $users, $output, $appbox)

View File

@@ -122,7 +122,7 @@ class module_console_systemTemplateGenerator extends Command
$output->writeln(""); $output->writeln("");
return; return $n_error;
} }
} }

View File

@@ -40,7 +40,7 @@ class module_console_systemUpgrade extends Command
if (!setup::is_installed()) if (!setup::is_installed())
{ {
$output->writeln('This version of Phraseanet requires a config/config.inc'); $output->writeln('This version of Phraseanet requires a config/config.yml, config/connexion.yml, config/service.yml');
$output->writeln('Would you like it to be created based on your settings ?'); $output->writeln('Would you like it to be created based on your settings ?');
$dialog = $this->getHelperSet()->get('dialog'); $dialog = $this->getHelperSet()->get('dialog');

View File

@@ -37,34 +37,43 @@ class module_console_tasklist extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
if(!setup::is_installed()) if (!setup::is_installed())
{ {
throw new RuntimeException('Phraseanet is not set up'); $output->writeln('Phraseanet is not set up');
return 1;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php'; require_once __DIR__ . '/../../../../lib/bootstrap.php';
$appbox = appbox::get_instance(); try
$task_manager = new task_manager($appbox);
$tasks = $task_manager->get_tasks();
if(count($tasks) === 0)
$output->writeln ('No tasks on your install !');
foreach($tasks as $task)
{ {
$this->print_task($task, $output); $appbox = appbox::get_instance();
$task_manager = new task_manager($appbox);
$tasks = $task_manager->get_tasks();
if (count($tasks) === 0)
{
$output->writeln('No tasks on your install !');
}
foreach ($tasks as $task)
{
$this->print_task($task, $output);
}
return 0;
}
catch (\Exception $e)
{
return 1;
} }
return $this;
} }
protected function print_task(task_abstract $task, OutputInterface &$output) protected function print_task(task_abstract $task, OutputInterface &$output)
{ {
$message = $task->get_task_id()."\t".($task->get_status() )."\t".$task->get_title(); $message = $task->get_task_id() . "\t" . ($task->get_status() ) . "\t" . $task->get_title();
$output->writeln($message); $output->writeln($message);
return $this; return $this;
} }
} }

View File

@@ -45,9 +45,10 @@ class module_console_taskrun extends Command
public function execute(InputInterface $input, OutputInterface $output) public function execute(InputInterface $input, OutputInterface $output)
{ {
if(!setup::is_installed()) if (!setup::is_installed())
{ {
throw new RuntimeException('Phraseanet is not set up'); $output->writeln('Phraseanet is not set up');
return 1;
} }
require_once __DIR__ . '/../../../../lib/bootstrap.php'; require_once __DIR__ . '/../../../../lib/bootstrap.php';
@@ -55,20 +56,32 @@ class module_console_taskrun extends Command
$task_id = (int) $input->getArgument('task_id'); $task_id = (int) $input->getArgument('task_id');
if ($task_id <= 0 || strlen($task_id) !== strlen($input->getArgument('task_id'))) if ($task_id <= 0 || strlen($task_id) !== strlen($input->getArgument('task_id')))
throw new \RuntimeException('Argument must be an Id.'); {
$output->writeln('Argument must be an Id.');
return 1;
}
$appbox = appbox::get_instance(); try
$task_manager = new task_manager($appbox); {
$task = $task_manager->get_task($task_id); $appbox = appbox::get_instance();
$task_manager = new task_manager($appbox);
$task = $task_manager->get_task($task_id);
$runner = task_abstract::RUNNER_SCHEDULER; $runner = task_abstract::RUNNER_SCHEDULER;
if ($input->getOption('runner') === task_abstract::RUNNER_MANUAL)
$runner = task_abstract::RUNNER_MANUAL;
$task->run($runner); if ($input->getOption('runner') === task_abstract::RUNNER_MANUAL)
{
return $this; $runner = task_abstract::RUNNER_MANUAL;
}
$task->run($runner);
return 0;
}
catch (\exception $e)
{
return 1;
}
} }
} }