1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Paraunit\Configuration; |
6
|
|
|
|
7
|
|
|
use Paraunit\Configuration\DependencyInjection\ParallelContainerDefinition; |
8
|
|
|
use Paraunit\Printer\DebugPrinter; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
11
|
|
|
use Symfony\Component\DependencyInjection\Alias; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
13
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
14
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Paraunit |
19
|
|
|
* @package Paraunit\Configuration |
20
|
|
|
*/ |
21
|
|
|
class ParallelConfiguration |
22
|
|
|
{ |
23
|
|
|
const TAG_EVENT_SUBSCRIBER = 'paraunit.event_subscriber'; |
24
|
|
|
const PUBLIC_ALIAS_FORMAT = '%s_public_alias'; |
25
|
|
|
|
26
|
|
|
/** @var ParallelContainerDefinition */ |
27
|
|
|
protected $containerDefinition; |
28
|
|
|
|
29
|
|
|
/** @var bool */ |
30
|
|
|
private $createPublicServiceAliases; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* ParallelConfiguration constructor. |
34
|
|
|
* @param bool $createPublicServiceAliases |
35
|
|
|
*/ |
36
|
25 |
|
public function __construct(bool $createPublicServiceAliases = false) |
37
|
|
|
{ |
38
|
25 |
|
$this->containerDefinition = new ParallelContainerDefinition(); |
39
|
25 |
|
$this->createPublicServiceAliases = $createPublicServiceAliases; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param InputInterface $input |
44
|
|
|
* @param OutputInterface $output |
45
|
|
|
* @return ContainerBuilder |
46
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\BadMethodCallException |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
70 |
|
public function buildContainer(InputInterface $input, OutputInterface $output): ContainerBuilder |
50
|
|
|
{ |
51
|
70 |
|
$containerBuilder = new ContainerBuilder(); |
52
|
|
|
|
53
|
70 |
|
$this->containerDefinition->configure($containerBuilder); |
54
|
70 |
|
$this->loadCommandLineOptions($containerBuilder, $input); |
55
|
70 |
|
$this->tagEventSubscribers($containerBuilder); |
56
|
|
|
|
57
|
70 |
|
$this->createPublicAliases($containerBuilder); |
58
|
70 |
|
$containerBuilder->compile(); |
59
|
70 |
|
$containerBuilder->set(OutputInterface::class, $output); |
60
|
|
|
|
61
|
70 |
|
return $containerBuilder; |
62
|
|
|
} |
63
|
|
|
|
64
|
70 |
|
protected function tagEventSubscribers(ContainerBuilder $container) |
65
|
|
|
{ |
66
|
70 |
|
foreach ($container->getDefinitions() as $definition) { |
67
|
70 |
|
if ($definition->isSynthetic() || $definition->isAbstract()) { |
68
|
70 |
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
70 |
|
if (array_key_exists(EventSubscriberInterface::class, class_implements($definition->getClass()))) { |
72
|
70 |
|
$definition->addTag(self::TAG_EVENT_SUBSCRIBER); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
70 |
|
protected function loadCommandLineOptions(ContainerBuilder $containerBuilder, InputInterface $input): void |
78
|
|
|
{ |
79
|
70 |
|
$containerBuilder->setParameter('paraunit.max_process_count', $input->getOption('parallel')); |
80
|
70 |
|
$containerBuilder->setParameter('paraunit.phpunit_config_filename', $input->getOption('configuration') ?? '.'); |
81
|
70 |
|
$containerBuilder->setParameter('paraunit.testsuite', $input->getOption('testsuite')); |
82
|
70 |
|
$containerBuilder->setParameter('paraunit.string_filter', $input->getArgument('stringFilter')); |
83
|
70 |
|
$containerBuilder->setParameter('paraunit.show_logo', $input->getOption('logo')); |
84
|
|
|
|
85
|
70 |
|
if ($input->getOption('debug')) { |
86
|
3 |
|
$this->enableDebugMode($containerBuilder); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
private function enableDebugMode(ContainerBuilder $containerBuilder) |
91
|
|
|
{ |
92
|
3 |
|
$definition = new Definition(DebugPrinter::class, [new Reference(OutputInterface::class)]); |
93
|
3 |
|
$definition->addTag(self::TAG_EVENT_SUBSCRIBER); |
94
|
|
|
|
95
|
3 |
|
$containerBuilder->setDefinition(DebugPrinter::class, $definition); |
96
|
|
|
} |
97
|
|
|
|
98
|
70 |
|
private function createPublicAliases(ContainerBuilder $containerBuilder) |
99
|
|
|
{ |
100
|
70 |
|
if (! $this->createPublicServiceAliases) { |
101
|
12 |
|
return; |
102
|
|
|
} |
103
|
|
|
|
104
|
58 |
|
$services = $containerBuilder->getServiceIds(); |
105
|
|
|
// the synthetic service isn't listed |
106
|
58 |
|
$services[] = OutputInterface::class; |
107
|
58 |
|
foreach ($services as $serviceName) { |
108
|
58 |
|
if ($serviceName === 'service_container') { |
109
|
|
|
// needed with SF 3.x |
110
|
58 |
|
continue; |
111
|
|
|
} |
112
|
|
|
|
113
|
58 |
|
$containerBuilder->setAlias( |
114
|
58 |
|
sprintf(self::PUBLIC_ALIAS_FORMAT, $serviceName), |
115
|
58 |
|
new Alias($serviceName, true) |
116
|
|
|
); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|