Skip to content

Commit

Permalink
TASK: Inject Logger for custom task
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon committed Oct 10, 2023
1 parent 5832255 commit 3ad5586
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/Domain/Model/Deployment.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,10 @@ public function getTemporaryPath(): string

public function rollback(bool $dryRun = false): void
{
if($this->container === null) {
throw new UnexpectedValueException('Container must not be null');
}

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

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

private function createSimpleWorkflow(): SimpleWorkflow
{
if($this->container === null) {
throw new UnexpectedValueException('Container must not be null');
}

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

if (!$workflow instanceof SimpleWorkflow) {
Expand Down
9 changes: 8 additions & 1 deletion src/Domain/Service/TaskFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

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;
Expand All @@ -30,11 +32,16 @@ public function createTaskInstance(string $taskName): Task

private function createTask(string $taskName): Task
{
if (! $this->container->has($taskName)) {
if ($this->container === null || ! $this->container->has($taskName)) {
$task = new $taskName();
if ($task instanceof ShellCommandServiceAwareInterface) {
$task->setShellCommandService(new ShellCommandService());
}
if($this->container !== null && $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

0 comments on commit 3ad5586

Please sign in to comment.