Skip to content

Commit

Permalink
TASK: Inject Logger for custom task (#778)
Browse files Browse the repository at this point in the history
* TASK: Inject Logger for custom task
* TASK: Throw an exception if container is null
* Update src/Domain/Service/TaskFactory.php
  • Loading branch information
sabbelasichon authored Oct 10, 2023
1 parent 5832255 commit 7b8e224
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
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

0 comments on commit 7b8e224

Please sign in to comment.