Skip to content

Commit

Permalink
Merge pull request #17 from 21TORR/port-v2
Browse files Browse the repository at this point in the history
  • Loading branch information
keichinger authored Jun 7, 2024
2 parents 84fd736 + 937ed89 commit 4ed44e5
Show file tree
Hide file tree
Showing 27 changed files with 833 additions and 179 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
2.0.0
=====

* (bc) **This bundle now requires that all your messages extend from the `Task` base class.**
* (bc) Change signature of `TaskManager::enqueue()` to only accept tasks.
* (bc) Remove `RegisterTasksEvent::registerTask()` and replace it with `RegisterTasksEvent::register(Task $task)`.
* (feature) Add `TaskDirector` and `RunDirector` to better integrate and log your task runs.
* (feature) Add native `Task` event that encapsulates commonly used logic.


1.3.1
=====

Expand Down
6 changes: 6 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
1.x to 2.0
==========

* All your messages need to extend `Task` now.
* `TaskManager::enqueue()` now only accepts `Task`s.
* `RegisterTasksEvent::registerTask()` was removed, you should use `RegisterTasksEvent::register(Task $task)`.
31 changes: 17 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,25 @@
],
"homepage": "https://github.com/21TORR/TaskManagerBundle",
"require": {
"php": ">= 8.1",
"php": ">= 8.3",
"21torr/bundle-helpers": "^2.1",
"21torr/cli": "^1.0",
"symfony/config": "^6.4 || ^7.0",
"symfony/console": "^6.4 || ^7.0",
"symfony/dependency-injection": "^6.4 || ^7.0",
"doctrine/orm": "^2.19",
"symfony/clock": "^7.0",
"symfony/config": "^7.0",
"symfony/console": "^7.0",
"symfony/dependency-injection": "^7.0",
"symfony/event-dispatcher-contracts": "^3.4",
"symfony/http-kernel": "^6.4 || ^7.0",
"symfony/messenger": "^6.4 || ^7.0",
"symfony/string": "^6.4 || ^7.0"
"symfony/http-kernel": "^7.0",
"symfony/messenger": "^7.0",
"symfony/string": "^7.0",
"symfony/uid": "^7.0"
},
"require-dev": {
"21torr/janus": "^1.2",
"21torr/janus": "^1.3.3",
"bamarni/composer-bin-plugin": "^1.8",
"roave/security-advisories": "dev-latest",
"symfony/phpunit-bridge": "^6.4 || ^7.0"
"symfony/phpunit-bridge": "^7.0"
},
"autoload": {
"psr-4": {
Expand All @@ -52,16 +55,16 @@
},
"scripts": {
"fix-lint": [
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi",
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi"
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --ansi",
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi"
],
"lint": [
"@composer bin c-norm normalize \"$(pwd)/composer.json\" --indent-style tab --indent-size 1 --dry-run --ansi",
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer fix --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --dry-run --no-interaction --ansi"
"vendor-bin/cs-fixer/vendor/bin/php-cs-fixer check --diff --config vendor-bin/cs-fixer/vendor/21torr/php-cs-fixer/.php-cs-fixer.dist.php --no-interaction --ansi"
],
"test": [
"vendor/bin/simple-phpunit",
"vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi"
"vendor-bin/phpstan/vendor/bin/phpstan analyze -c phpstan.neon . --ansi -v"
]
}
}
}
8 changes: 7 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
includes:
- vendor/21torr/janus/phpstan/lib.neon
- vendor/21torr/janus/phpstan/lib.neon

# If you use simple-phpunit, you need to uncomment the following line.
# Always make sure to first run simple-phpunit and then PHPStan.
# parameters:
# bootstrapFiles:
# - vendor/bin/.phpunit/phpunit/vendor/autoload.php
11 changes: 5 additions & 6 deletions src/Command/ListTasksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ protected function execute (InputInterface $input, OutputInterface $output) : in
foreach ($tasks as $task)
{
$row = [];
$metaData = $task->getMetaData();

if ($first)
{
Expand All @@ -67,25 +68,23 @@ protected function execute (InputInterface $input, OutputInterface $output) : in
$first = false;
}

$row[] = \sprintf(
$row[] = sprintf(
"<fg=yellow>%s</>",
$task->key,
$metaData->getKey(),
);
$row[] = $task->label;
$row[] = \get_class($task->task);
$row[] = $metaData->label;
$row[] = $task::class;
$rows[] = $row;
}
}


$io->table(
headers: [
"Group",
"Key",
"Name",
"Task Class",
],
// @phpstan-ignore-next-line This is fine and the type was fixed in newer versions of the CLI bundle
rows: $rows,
);

Expand Down
29 changes: 16 additions & 13 deletions src/Command/QueueTasksCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Torr\TaskManager\Exception\Registry\UnknownTaskKeyException;
use Torr\TaskManager\Manager\TaskManager;
use Torr\TaskManager\Receiver\ReceiverHelper;
use Torr\TaskManager\Registry\Data\Task;
use Torr\TaskManager\Registry\TaskRegistry;
use Torr\TaskManager\Task\Task;

#[AsCommand("task-manager:queue")]
final class QueueTasksCommand extends Command
Expand Down Expand Up @@ -58,26 +58,27 @@ protected function execute (InputInterface $input, OutputInterface $output) : in
catch (UnknownTaskKeyException $exception)
{
$io->error($exception->getMessage());

return self::FAILURE;
}


$io->comment(\sprintf(
$io->comment(sprintf(
"Queuing <fg=magenta>%d</> task%s",
\count($tasksToQueue),
1 !== \count($tasksToQueue) ? "s" : "",
));

foreach ($tasksToQueue as $task)
{
$io->writeln(\sprintf(
$io->writeln(sprintf(
"• Queuing task %s",
$this->formatTaskLabel($task),
));
$this->taskManager->enqueue($task->task);
$this->taskManager->enqueue($task);
}

$io->success("All done.");

return self::SUCCESS;
}

Expand Down Expand Up @@ -119,7 +120,7 @@ private function getTasksToQueue (InputInterface $input, TorrStyle $io) : array

foreach ($selectedOptions as $option)
{
$index = \array_search($option, $choices, true);
$index = array_search($option, $choices, true);
\assert(\is_int($index));

$result[] = $flatTasks[$index];
Expand All @@ -140,7 +141,7 @@ private function fetchTasksByKey (array $keys) : array
foreach ($keys as $taskKey)
{
$result[] = $this->taskRegistry->getTaskByKey($taskKey)
?? throw new UnknownTaskKeyException(\sprintf(
?? throw new UnknownTaskKeyException(sprintf(
"Unknown task key '%s'",
$taskKey,
));
Expand All @@ -154,16 +155,18 @@ private function fetchTasksByKey (array $keys) : array
*/
private function formatTaskLabel (Task $task) : string
{
if (null !== $task->group)
$metaData = $task->getMetaData();

if (null !== $metaData->group)
{
return \sprintf(
return sprintf(
"<fg=blue>%s</>: %s (<fg=yellow>%s</>)",
$task->group,
$task->label,
$task->key,
$metaData->group,
$metaData->label,
$metaData->getKey(),
);
}

return $task->label;
return $metaData->label;
}
}
6 changes: 3 additions & 3 deletions src/Config/BundleConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace Torr\TaskManager\Config;

final class BundleConfig
final readonly class BundleConfig
{
/**
*/
public function __construct (
/** @var string[] */
public readonly array $sortedQueues,
public array $sortedQueues,
/** @var string[] */
public readonly array $failureTransports = [],
public array $failureTransports = [],
) {}
}
132 changes: 132 additions & 0 deletions src/Console/ChainOutput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Console;

use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

final readonly class ChainOutput implements OutputInterface
{
private ConsoleOutput $consoleOutput;
private BufferedOutput $bufferedOutput;

/**
*/
public function __construct (
int $verbosity = self::VERBOSITY_NORMAL,
bool $decorated = true,
?OutputFormatterInterface $formatter = null,
)
{
$this->bufferedOutput = new BufferedOutput($verbosity, $decorated, $formatter);
$this->consoleOutput = new ConsoleOutput($verbosity, $decorated, $formatter);
}

/**
* @inheritDoc
*/
public function write (iterable|string $messages, bool $newline = false, int $options = 0) : void
{
$this->bufferedOutput->write($messages, $newline, $options);
$this->consoleOutput->write($messages, $newline, $options);
}

/**
* @inheritDoc
*/
public function writeln (iterable|string $messages, int $options = 0) : void
{
$this->bufferedOutput->writeln($messages, $options);
$this->consoleOutput->writeln($messages, $options);
}

/**
* @inheritDoc
*/
public function setVerbosity (int $level) : void
{
$this->bufferedOutput->setVerbosity($level);
$this->consoleOutput->setVerbosity($level);
}

/**
* @inheritDoc
*/
public function getVerbosity () : int
{
return $this->bufferedOutput->getVerbosity();
}

/**
* @inheritDoc
*/
public function isQuiet () : bool
{
return $this->bufferedOutput->isQuiet();
}

/**
* @inheritDoc
*/
public function isVerbose () : bool
{
return $this->bufferedOutput->isVerbose();
}

/**
* @inheritDoc
*/
public function isVeryVerbose () : bool
{
return $this->bufferedOutput->isVeryVerbose();
}

/**
* @inheritDoc
*/
public function isDebug () : bool
{
return $this->bufferedOutput->isDebug();
}

/**
* @inheritDoc
*/
public function setDecorated (bool $decorated) : void
{
$this->bufferedOutput->setDecorated(true);
$this->consoleOutput->setDecorated(true);
}

/**
* @inheritDoc
*/
public function isDecorated () : bool
{
return $this->bufferedOutput->isDecorated();
}

public function setFormatter (OutputFormatterInterface $formatter) : void
{
$this->bufferedOutput->setFormatter($formatter);
$this->consoleOutput->setFormatter($formatter);
}

/**
* @inheritDoc
*/
public function getFormatter () : OutputFormatterInterface
{
return $this->bufferedOutput->getFormatter();
}

/**
*
*/
public function getBufferedOutput () : string
{
return $this->bufferedOutput->fetch();
}
}
31 changes: 31 additions & 0 deletions src/Console/MessageHandlerIo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace Torr\TaskManager\Console;

use Symfony\Component\Console\Input\ArrayInput;
use Torr\Cli\Console\Style\TorrStyle;

final class MessageHandlerIo extends TorrStyle
{
private readonly ChainOutput $output;

/**
*/
public function __construct ()
{
$this->output = new ChainOutput();

parent::__construct(
new ArrayInput([]),
$this->output,
);
}

/**
*
*/
public function getBufferedOutput () : string
{
return $this->output->getBufferedOutput();
}
}
Loading

0 comments on commit 4ed44e5

Please sign in to comment.