Skip to content

Commit 7b8e224

Browse files
TASK: Inject Logger for custom task (#778)
* TASK: Inject Logger for custom task * TASK: Throw an exception if container is null * Update src/Domain/Service/TaskFactory.php
1 parent 5832255 commit 7b8e224

File tree

5 files changed

+25
-4
lines changed

5 files changed

+25
-4
lines changed

Resources/services.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Psr\Log\LoggerInterface;
1616
use Symfony\Component\Console\Application;
1717
use Symfony\Component\Console\Output\OutputInterface;
18-
use Symfony\Component\DependencyInjection\Alias;
19-
use Symfony\Component\DependencyInjection\Container;
2018
use Symfony\Component\DependencyInjection\ContainerInterface;
2119
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2220
use TYPO3\Surf\Cli\Symfony\ConsoleApplication;

src/Domain/Model/Deployment.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use TYPO3\Surf\Exception as SurfException;
2121
use TYPO3\Surf\Integration\LoggerAwareTrait;
2222
use UnexpectedValueException;
23+
use Webmozart\Assert\Assert;
2324

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

459460
public function rollback(bool $dryRun = false): void
460461
{
462+
Assert::notNull($this->container);
463+
461464
$this->logger->notice('Rollback deployment ' . $this->name . ' (' . $this->releaseIdentifier . ')');
462465

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

498501
private function createSimpleWorkflow(): SimpleWorkflow
499502
{
503+
Assert::notNull($this->container);
504+
500505
$workflow = $this->container->get(SimpleWorkflow::class);
501506

502507
if (!$workflow instanceof SimpleWorkflow) {

src/Domain/Service/TaskFactory.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@
1111

1212
namespace TYPO3\Surf\Domain\Service;
1313

14+
use Psr\Log\LoggerAwareInterface;
15+
use Psr\Log\LoggerInterface;
1416
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
1517
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1618
use TYPO3\Surf\Domain\Model\Task;
1719
use UnexpectedValueException;
20+
use Webmozart\Assert\Assert;
1821

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

3134
private function createTask(string $taskName): Task
3235
{
36+
Assert::notNull($this->container);
37+
3338
if (! $this->container->has($taskName)) {
3439
$task = new $taskName();
3540
if ($task instanceof ShellCommandServiceAwareInterface) {
3641
$task->setShellCommandService(new ShellCommandService());
3742
}
43+
if ($task instanceof LoggerAwareInterface) {
44+
/** @var LoggerInterface $logger */
45+
$logger = $this->container->get(LoggerInterface::class);
46+
$task->setLogger($logger);
47+
}
3848
} else {
3949
$task = $this->container->get($taskName);
4050
}

tests/Unit/Domain/Service/CustomTask.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

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

14+
use Psr\Log\LoggerInterface;
1415
use TYPO3\Surf\Domain\Model\Application;
1516
use TYPO3\Surf\Domain\Model\Deployment;
1617
use TYPO3\Surf\Domain\Model\Node;
@@ -31,4 +32,9 @@ public function getShell(): ShellCommandService
3132
{
3233
return $this->shell;
3334
}
35+
36+
public function getLogger(): LoggerInterface
37+
{
38+
return $this->logger;
39+
}
3440
}

tests/Unit/Domain/Service/TaskFactoryTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace TYPO3\Surf\Tests\Unit\Domain\Service;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Psr\Log\LoggerInterface;
16+
use TYPO3\Surf\Domain\Service\ShellCommandService;
1517
use TYPO3\Surf\Domain\Service\TaskFactory;
1618
use TYPO3\Surf\Task\CreateArchiveTask;
1719
use TYPO3\Surf\Tests\Unit\KernelAwareTrait;
@@ -49,11 +51,11 @@ public function createTaskInstance(): void
4951
*/
5052
public function createSyntheticServiceIfNotExists(): void
5153
{
52-
/** @var CustomTask $customTask */
5354
$customTask = $this->subject->createTaskInstance(CustomTask::class);
5455

55-
self::assertNotNull($customTask->getShell());
5656
self::assertInstanceOf(CustomTask::class, $customTask);
57+
self::assertInstanceOf(LoggerInterface::class, $customTask->getLogger());
58+
self::assertInstanceOf(ShellCommandService::class, $customTask->getShell());
5759
}
5860

5961
/**

0 commit comments

Comments
 (0)