Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Inject Logger for custom task #778

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Resources/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use Psr\Log\LoggerInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use TYPO3\Surf\Cli\Symfony\ConsoleApplication;
Expand Down
5 changes: 5 additions & 0 deletions src/Domain/Model/Deployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use TYPO3\Surf\Exception as SurfException;
use TYPO3\Surf\Integration\LoggerAwareTrait;
use UnexpectedValueException;
use Webmozart\Assert\Assert;

/**
* This is the base object exposed to a deployment configuration script and serves as a configuration builder and
Expand Down Expand Up @@ -458,6 +459,8 @@ public function getTemporaryPath(): string

public function rollback(bool $dryRun = false): void
{
Assert::notNull($this->container);

$this->logger->notice('Rollback deployment ' . $this->name . ' (' . $this->releaseIdentifier . ')');

/** @var RollbackWorkflow $workflow */
Expand Down Expand Up @@ -497,6 +500,8 @@ private function setDeploymentLockIdentifier(?string $deploymentLockIdentifier =

private function createSimpleWorkflow(): SimpleWorkflow
{
Assert::notNull($this->container);

$workflow = $this->container->get(SimpleWorkflow::class);

if (!$workflow instanceof SimpleWorkflow) {
Expand Down
10 changes: 10 additions & 0 deletions src/Domain/Service/TaskFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@

namespace TYPO3\Surf\Domain\Service;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use TYPO3\Surf\Domain\Model\Task;
use UnexpectedValueException;
use Webmozart\Assert\Assert;

/**
* @final
Expand All @@ -30,11 +33,18 @@ public function createTaskInstance(string $taskName): Task

private function createTask(string $taskName): Task
{
Assert::notNull($this->container);

if (! $this->container->has($taskName)) {
$task = new $taskName();
if ($task instanceof ShellCommandServiceAwareInterface) {
$task->setShellCommandService(new ShellCommandService());
}
if ($task instanceof LoggerAwareInterface) {
/** @var LoggerInterface $logger */
$logger = $this->container->get(LoggerInterface::class);
$task->setLogger($logger);
}
} else {
$task = $this->container->get($taskName);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/Domain/Service/CustomTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace TYPO3\Surf\Tests\Unit\Domain\Service;

use Psr\Log\LoggerInterface;
use TYPO3\Surf\Domain\Model\Application;
use TYPO3\Surf\Domain\Model\Deployment;
use TYPO3\Surf\Domain\Model\Node;
Expand All @@ -31,4 +32,9 @@ public function getShell(): ShellCommandService
{
return $this->shell;
}

public function getLogger(): LoggerInterface
{
return $this->logger;
}
}
6 changes: 4 additions & 2 deletions tests/Unit/Domain/Service/TaskFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace TYPO3\Surf\Tests\Unit\Domain\Service;

use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use TYPO3\Surf\Domain\Service\ShellCommandService;
use TYPO3\Surf\Domain\Service\TaskFactory;
use TYPO3\Surf\Task\CreateArchiveTask;
use TYPO3\Surf\Tests\Unit\KernelAwareTrait;
Expand Down Expand Up @@ -49,11 +51,11 @@ public function createTaskInstance(): void
*/
public function createSyntheticServiceIfNotExists(): void
{
/** @var CustomTask $customTask */
$customTask = $this->subject->createTaskInstance(CustomTask::class);

self::assertNotNull($customTask->getShell());
self::assertInstanceOf(CustomTask::class, $customTask);
self::assertInstanceOf(LoggerInterface::class, $customTask->getLogger());
self::assertInstanceOf(ShellCommandService::class, $customTask->getShell());
}

/**
Expand Down