Skip to content

Commit 73757e0

Browse files
sabbelasichonsimonschaufi
authored andcommitted
[TASK] Use constructor injection for Container
Resolves: #796
1 parent c22b941 commit 73757e0

File tree

17 files changed

+66
-72
lines changed

17 files changed

+66
-72
lines changed

Resources/services.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
->share(false);
7777

7878
$services->set(Factory::class)
79-
->args([service(FilesystemInterface::class), service(LoggerInterface::class)]);
79+
->args([service('service_container'), service(FilesystemInterface::class), service(LoggerInterface::class)]);
8080

8181
$services->alias(FactoryInterface::class, Factory::class);
8282

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"neos/utility-files": "^7.3.10 || ^8.3.9",
1717
"symfony/config": "^5.0 || ^6.0 || ^7.0",
1818
"symfony/console": "^5.0 || ^6.0 || ^7.0",
19-
"symfony/dependency-injection": "^5.0 || ^6.0",
19+
"symfony/dependency-injection": "^5.0 || ^6.0 || ^7.0",
2020
"symfony/finder": "^5.1 || ^6.0 || ^7.0",
2121
"symfony/options-resolver": "^5.0 || ^6.0 || ^7.0",
2222
"symfony/process": "^5.0 || ^6.0 || ^7.0",

src/Cli/Symfony/ConsoleKernel.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
use Symfony\Component\Config\FileLocator;
1818
use Symfony\Component\Config\Loader\LoaderInterface;
1919
use Symfony\Component\DependencyInjection\Container;
20-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
2120
use Symfony\Component\DependencyInjection\ContainerBuilder;
22-
use Symfony\Component\DependencyInjection\ContainerInterface;
2321
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
2422
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
2523
use Symfony\Component\DependencyInjection\Reference;
@@ -47,12 +45,6 @@ private function registerContainerConfiguration(LoaderInterface $loader): void
4745
private function build(ContainerBuilder $container): void
4846
{
4947
$container->addCompilerPass(new CommandsToApplicationCompilerPass());
50-
$container->registerForAutoconfiguration(
51-
ContainerAwareInterface::class
52-
)->addMethodCall(
53-
'setContainer',
54-
[new Reference(ContainerInterface::class)]
55-
);
5648
$container->registerForAutoconfiguration(
5749
ShellCommandServiceAwareInterface::class
5850
)->addMethodCall(

src/Domain/Model/Deployment.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,21 @@
1212
namespace TYPO3\Surf\Domain\Model;
1313

1414
use Neos\Utility\Files;
15+
use Psr\Container\ContainerInterface;
1516
use Psr\Log\LoggerAwareInterface;
1617
use Psr\Log\LoggerInterface;
17-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
18-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1918
use TYPO3\Surf\Domain\Enum\DeploymentStatus;
2019
use TYPO3\Surf\Exception as SurfException;
2120
use TYPO3\Surf\Integration\LoggerAwareTrait;
2221
use UnexpectedValueException;
23-
use Webmozart\Assert\Assert;
2422

2523
/**
2624
* This is the base object exposed to a deployment configuration script and serves as a configuration builder and
2725
* model for an actual deployment.
2826
*/
29-
class Deployment implements LoggerAwareInterface, ContainerAwareInterface
27+
class Deployment implements LoggerAwareInterface
3028
{
3129
use LoggerAwareTrait;
32-
use ContainerAwareTrait;
3330

3431
/**
3532
* The name of this deployment
@@ -97,9 +94,11 @@ class Deployment implements LoggerAwareInterface, ContainerAwareInterface
9794
private bool $forceRun = false;
9895

9996
private string $deploymentLockIdentifier;
97+
private ContainerInterface $container;
10098

101-
public function __construct(string $name, string $deploymentLockIdentifier = null)
99+
public function __construct(ContainerInterface $container, string $name, string $deploymentLockIdentifier = null)
102100
{
101+
$this->container = $container;
103102
$this->name = $name;
104103
$this->status = DeploymentStatus::UNKNOWN();
105104
$this->releaseIdentifier = date('YmdHis');
@@ -459,8 +458,6 @@ public function getTemporaryPath(): string
459458

460459
public function rollback(bool $dryRun = false): void
461460
{
462-
Assert::notNull($this->container);
463-
464461
$this->logger->notice('Rollback deployment ' . $this->name . ' (' . $this->releaseIdentifier . ')');
465462

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

501498
private function createSimpleWorkflow(): SimpleWorkflow
502499
{
503-
Assert::notNull($this->container);
504-
505500
$workflow = $this->container->get(SimpleWorkflow::class);
506501

507502
if (!$workflow instanceof SimpleWorkflow) {

src/Domain/Model/FailedDeployment.php

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

1212
namespace TYPO3\Surf\Domain\Model;
1313

14+
use Psr\Container\ContainerInterface;
1415
use TYPO3\Surf\Domain\Enum\DeploymentStatus;
1516

1617
/**
@@ -20,9 +21,9 @@
2021
*/
2122
class FailedDeployment extends Deployment
2223
{
23-
public function __construct(string $name)
24+
public function __construct(ContainerInterface $container, string $name)
2425
{
25-
parent::__construct($name);
26+
parent::__construct($container, $name);
2627
$this->releaseIdentifier = null;
2728
}
2829

src/Domain/Service/TaskFactory.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,24 @@
1111

1212
namespace TYPO3\Surf\Domain\Service;
1313

14+
use Psr\Container\ContainerInterface;
1415
use Psr\Log\LoggerAwareInterface;
1516
use Psr\Log\LoggerInterface;
16-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
17-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
1817
use TYPO3\Surf\Domain\Model\Task;
1918
use UnexpectedValueException;
20-
use Webmozart\Assert\Assert;
2119

2220
/**
2321
* @final
2422
*/
25-
class TaskFactory implements ContainerAwareInterface
23+
class TaskFactory
2624
{
27-
use ContainerAwareTrait;
25+
26+
private ContainerInterface $container;
27+
28+
public function __construct(ContainerInterface $container)
29+
{
30+
$this->container = $container;
31+
}
2832

2933
public function createTaskInstance(string $taskName): Task
3034
{
@@ -33,8 +37,6 @@ public function createTaskInstance(string $taskName): Task
3337

3438
private function createTask(string $taskName): Task
3539
{
36-
Assert::notNull($this->container);
37-
3840
if (! $this->container->has($taskName)) {
3941
$task = new $taskName();
4042
if ($task instanceof ShellCommandServiceAwareInterface) {

src/Integration/Factory.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,28 @@
1414
use Monolog\Handler\StreamHandler;
1515
use Monolog\Logger;
1616
use Neos\Utility\Files;
17+
use Psr\Container\ContainerInterface;
18+
use Psr\Log\LoggerInterface;
1719
use RuntimeException;
1820
use Symfony\Component\Console\Output\OutputInterface;
19-
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
20-
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
2121
use TYPO3\Surf\Domain\Filesystem\FilesystemInterface;
2222
use TYPO3\Surf\Domain\Model\Deployment;
2323
use TYPO3\Surf\Domain\Model\FailedDeployment;
2424
use TYPO3\Surf\Exception\InvalidConfigurationException;
2525

26-
class Factory implements FactoryInterface, ContainerAwareInterface
26+
class Factory implements FactoryInterface
2727
{
28-
use ContainerAwareTrait;
29-
3028
protected OutputInterface $output;
3129

32-
protected Logger $logger;
30+
protected LoggerInterface $logger;
3331

3432
protected FilesystemInterface $filesystem;
3533

36-
public function __construct(FilesystemInterface $filesystem, Logger $logger)
34+
protected ContainerInterface $container;
35+
36+
public function __construct(ContainerInterface $container, FilesystemInterface $filesystem, LoggerInterface $logger)
3737
{
38+
$this->container = $container;
3839
$this->filesystem = $filesystem;
3940
$this->logger = $logger;
4041
}
@@ -45,7 +46,9 @@ public function getDeployment(string $deploymentName, string $configurationPath
4546

4647
if (! $simulateDeployment) {
4748
$logFilePath = Files::concatenatePaths([$this->getWorkspacesBasePath($configurationPath), 'logs', $deployment->getName() . '.log']);
48-
$this->logger->pushHandler(new StreamHandler($logFilePath));
49+
if ($this->logger instanceof Logger) {
50+
$this->logger->pushHandler(new StreamHandler($logFilePath));
51+
}
4952
}
5053

5154
$deployment->setForceRun($forceDeployment);
@@ -145,15 +148,14 @@ protected function createDeployment(string $deploymentName, string $path = null)
145148

146149
if (! $this->filesystem->fileExists($deploymentPathAndFilename)) {
147150
$this->logger->error(sprintf("The deployment file %s does not exist.\n", $deploymentPathAndFilename));
148-
$deployment = new FailedDeployment($deploymentName);
151+
$deployment = new FailedDeployment($this->container, $deploymentName);
149152
$deployment->setLogger($this->logger);
150153

151154
return $deployment;
152155
}
153156

154-
$deployment = new Deployment($deploymentName);
157+
$deployment = new Deployment($this->container, $deploymentName);
155158
$deployment->setLogger($this->logger);
156-
$deployment->setContainer($this->container);
157159
$deployment->setDeploymentBasePath($deploymentConfigurationPath);
158160
$deployment->setWorkspacesBasePath($workspacesBasePath);
159161

tests/Unit/Command/DescribeCommandTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ class DescribeCommandTest extends TestCase
4242

4343
protected function setUp(): void
4444
{
45-
$this->deployment = new Deployment('TestDeployment');
46-
$this->deployment->setContainer(static::getKernel()->getContainer());
45+
$this->deployment = new Deployment(static::getKernel()->getContainer(), 'TestDeployment');
4746

4847
$this->node = new Node('TestNode');
4948
$this->node->setHostname('hostname');

tests/Unit/Domain/Model/DeploymentTest.php

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ protected function tearDown(): void
4242
*/
4343
public function initializeUsesSimpleWorkflowAsDefault(): void
4444
{
45-
$deployment = new Deployment('Test deployment');
46-
$deployment->setContainer(static::getKernel()->getContainer());
45+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Test deployment');
4746
$deployment->initialize();
4847

4948
self::assertInstanceOf(SimpleWorkflow::class, $deployment->getWorkflow());
@@ -54,8 +53,7 @@ public function initializeUsesSimpleWorkflowAsDefault(): void
5453
*/
5554
public function getNodesReturnsNodesFromApplicationsAsSet(): void
5655
{
57-
$deployment = new Deployment('Test deployment');
58-
$deployment->setContainer(static::getKernel()->getContainer());
56+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Test deployment');
5957
$application1 = new Application('Test application 1');
6058
$application2 = new Application('Test application 2');
6159

@@ -81,8 +79,7 @@ public function getNodesReturnsNodesFromApplicationsAsSet(): void
8179
*/
8280
public function constructorCreatesReleaseIdentifier(): void
8381
{
84-
$deployment = new Deployment('Test deployment');
85-
$deployment->setContainer(static::getKernel()->getContainer());
82+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Test deployment');
8683

8784
$releaseIdentifier = $deployment->getReleaseIdentifier();
8885

@@ -98,7 +95,7 @@ public function initializeIsAllowedOnlyOnce(): void
9895

9996
$workflow = new SimpleWorkflow($this->prophesize(TaskManager::class)->reveal());
10097

101-
$deployment = new Deployment('Test deployment');
98+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Test deployment');
10299
$deployment->setWorkflow($workflow);
103100
$deployment->initialize();
104101

@@ -113,8 +110,7 @@ public function initializeIsAllowedOnlyOnce(): void
113110
*/
114111
public function deploymentHasDefaultLockIdentifierIfNoIdentifierIsGiven($deploymentLockIdentifier): void
115112
{
116-
$deployment = new Deployment('Some name', $deploymentLockIdentifier);
117-
$deployment->setContainer(static::getKernel()->getContainer());
113+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Some name', $deploymentLockIdentifier);
118114

119115
self::assertSame($deployment->getReleaseIdentifier(), $deployment->getDeploymentLockIdentifier());
120116
}
@@ -125,7 +121,7 @@ public function deploymentHasDefaultLockIdentifierIfNoIdentifierIsGiven($deploym
125121
public function deploymentHasDefinedLockIdentifier(): void
126122
{
127123
$deploymentLockIdentifier = 'Deployment lock identifier';
128-
$deployment = new Deployment('Some name', $deploymentLockIdentifier);
124+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Some name', $deploymentLockIdentifier);
129125

130126
self::assertSame($deploymentLockIdentifier, $deployment->getDeploymentLockIdentifier());
131127
}
@@ -138,7 +134,7 @@ public function deploymentHasLockIdentifierDefinedByEnvironmentVariable(): void
138134
$deploymentLockIdentifier = 'Deployment lock identifier';
139135
putenv(sprintf('SURF_DEPLOYMENT_LOCK_IDENTIFIER=%s', $deploymentLockIdentifier));
140136

141-
$deployment = new Deployment('Some name');
137+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Some name');
142138

143139
self::assertSame($deploymentLockIdentifier, $deployment->getDeploymentLockIdentifier());
144140
}
@@ -148,7 +144,7 @@ public function deploymentHasLockIdentifierDefinedByEnvironmentVariable(): void
148144
*/
149145
public function deploymentContainsRelativeProjectRootPathForApplicationReleasePath(): void
150146
{
151-
$deployment = new Deployment('Some name');
147+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Some name');
152148

153149
$node = new Node('Node');
154150
$node->setDeploymentPath('/deployment/path');
@@ -166,7 +162,7 @@ public function deploymentContainsRelativeProjectRootPathForApplicationReleasePa
166162
*/
167163
public function deploymentContainsChangedRelativeProjectRootPathForApplicationReleasePath(): void
168164
{
169-
$deployment = new Deployment('Some name');
165+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Some name');
170166
$deployment->setRelativeProjectRootPath('htdocs');
171167

172168
$node = new Node('Node');

tests/Unit/Domain/Model/RollbackWorkflowTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@
2121
use TYPO3\Surf\Domain\Service\TaskManager;
2222
use TYPO3\Surf\Exception as SurfException;
2323
use TYPO3\Surf\Task\Generic\RollbackTask;
24+
use TYPO3\Surf\Tests\Unit\KernelAwareTrait;
2425

2526
class RollbackWorkflowTest extends TestCase
2627
{
28+
use KernelAwareTrait;
29+
2730
/**
2831
* @test
2932
*/
@@ -367,7 +370,7 @@ public function tasksAreExecutedInTheRightOrder(): void
367370
*/
368371
protected function buildDeployment(array &$executedTasks = []): Deployment
369372
{
370-
$deployment = new Deployment('Test rollback deployment');
373+
$deployment = new Deployment(static::getKernel()->getContainer(), 'Test rollback deployment');
371374
$mockLogger = $this->createMock(LoggerInterface::class);
372375
// Enable log to console to debug tests
373376
// $mockLogger->expects(self::any())->method('log')->will($this->returnCallback(function($message) {

0 commit comments

Comments
 (0)