Skip to content

Commit

Permalink
Throw "Crunz\Task\WrongTaskInstanceException" when task is not "Sched…
Browse files Browse the repository at this point in the history
…ule" instance (#221)
  • Loading branch information
PabloKowalczyk authored Apr 19, 2019
1 parent cb4a6a0 commit 9c9cf9d
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 124 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
],
"crunz:link-changelog": "@php vendor/bin/changelog-linker dump-merges --dry-run --in-categories",
"crunz:link-changelog:since": "@php vendor/bin/changelog-linker dump-merges --dry-run --in-categories --since-id",
"phpstan:check": "@php vendor/bin/phpstan analyse -c phpstan.neon -l 7 src tests crunz config bootstrap.php .php_cs.dist"
"phpstan:check": "@php vendor/bin/phpstan analyse -c phpstan.neon -l 7 src tests crunz config bootstrap.php"
},
"extra": {
"branch-alias": {
Expand Down
5 changes: 3 additions & 2 deletions src/Console/Command/ClosureRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ protected function configure(): void
}

/** {@inheritdoc} */
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$args = [];
$this->arguments = $input->getArguments();

\parse_str($this->arguments['closure'], $args);
$serializer = new Serializer();
\call_user_func_array($serializer->unserialize($args[0]), []);

return (int) !((bool) \call_user_func_array($serializer->unserialize($args[0]), []));
}
}
13 changes: 6 additions & 7 deletions src/Console/Command/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Crunz\Configuration\Configuration;
use Crunz\Schedule;
use Crunz\Task\Collection;
use Crunz\Task\WrongTaskInstanceException;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -48,11 +49,14 @@ protected function configure(): void
->setHelp('This command displays the scheduled tasks in a tabular format.');
}

/** {@inheritdoc} */
/** {@inheritdoc}
* @throws WrongTaskInstanceException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->options = $input->getOptions();
$this->arguments = $input->getArguments();
/** @var \SplFileInfo[] $tasks */
$tasks = $this->taskCollection
->all($this->arguments['source']);

Expand All @@ -76,12 +80,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($tasks as $taskFile) {
$schedule = require $taskFile->getRealPath();
if (!$schedule instanceof Schedule) {
// @TODO throw exception in v2
@\trigger_error(
"File '{$taskFile->getRealPath()}' didn't return '\Crunz\Schedule' instance, this behavior is deprecated since v1.12 and will result in exception in v2.0+",
E_USER_DEPRECATED
);

throw WrongTaskInstanceException::fromFilePath($taskFile, $schedule);
continue;
}

Expand Down
13 changes: 6 additions & 7 deletions src/Console/Command/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Crunz\Task\Collection;
use Crunz\Task\TaskNumber;
use Crunz\Task\Timezone;
use Crunz\Task\WrongTaskInstanceException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
Expand Down Expand Up @@ -86,12 +87,15 @@ protected function configure(): void

/**
* {@inheritdoc}
*
* @throws WrongTaskInstanceException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->arguments = $input->getArguments();
$this->options = $input->getOptions();
$task = $this->options['task'];
/** @var \SplFileInfo[] $files */
$files = $this->taskCollection
->all($this->arguments['source']);

Expand All @@ -110,12 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($files as $file) {
$schedule = require $file->getRealPath();
if (!$schedule instanceof Schedule) {
// @TODO throw exception in v2
@\trigger_error(
"File '{$file->getRealPath()}' didn't return '\Crunz\Schedule' instance, this behavior is deprecated since v1.12 and will result in exception in v2.0+",
E_USER_DEPRECATED
);

throw WrongTaskInstanceException::fromFilePath($file, $schedule);
continue;
}

Expand Down Expand Up @@ -147,7 +146,7 @@ function (Schedule $schedule) use ($tasksTimezone) {
);
$schedules = \array_filter(
$schedules,
function (Schedule $schedule) {
static function (Schedule $schedule) {
return \count($schedule->events());
}
);
Expand Down
Loading

0 comments on commit 9c9cf9d

Please sign in to comment.