Skip to content

Commit 7577566

Browse files
authored
Feature/listener configuration (#408)
* Initial skeleton for supporting configured listeners * Organize bootstrap code into better sub-namespaces * Add Listeners to config, fix various psalm issues * Add phploc to tools * Add support for listener option
1 parent fb49a61 commit 7577566

File tree

67 files changed

+604
-230
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+604
-230
lines changed

Justfile

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ _default:
44
just --list --unsorted
55

66
# Install all dependencies necesesary to run Annotated Container tools
7-
install: _install_labrador_cs _install_phpunit _install_psalm _install_ac
7+
install: _install_labrador_cs _install_phpunit _install_psalm _install_phploc _install_ac
88

99
_install_ac:
1010
composer install
@@ -18,6 +18,9 @@ _install_phpunit:
1818
_install_psalm:
1919
cd tools/psalm && composer install
2020

21+
_install_phploc:
22+
cd tools/phploc && composer install
23+
2124
# Run unit tests
2225
test *FLAGS:
2326
@XDEBUG_MODE=coverage ./tools/phpunit/vendor/bin/phpunit {{FLAGS}}
@@ -47,6 +50,9 @@ code-lint:
4750
code-lint-fix:
4851
@./tools/labrador-cs/vendor/bin/phpcbf -p --standard=./tools/labrador-cs/vendor/cspray/labrador-coding-standard/ruleset.xml --exclude=Generic.Files.LineLength src test
4952

53+
loc:
54+
@./tools/phploc/vendor/bin/phploc src
55+
5056
# Run all CI checks. ALL checks will run, regardless of failures
5157
ci-check:
5258
-@just test

annotated-container.xsd

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

3333
<xs:element name="definitionProviders" type="definitionProvidersType" minOccurs="0" maxOccurs="1" />
3434
<xs:element name="parameterStores" type="parameterStoresType" minOccurs="0" maxOccurs="1" />
35+
<xs:element name="listeners" type="listenersType" minOccurs="0" maxOccurs="1" />
3536
</xs:all>
3637
<xs:attribute name="version" type="xs:string" use="required" />
3738
</xs:complexType>
@@ -86,5 +87,10 @@
8687
</xs:sequence>
8788
</xs:complexType>
8889

90+
<xs:complexType name="listenersType">
91+
<xs:sequence minOccurs="0" maxOccurs="unbounded">
92+
<xs:element name="listener" type="xs:token" />
93+
</xs:sequence>
94+
</xs:complexType>
8995

9096
</xs:schema>

docs/how-to/02-bootstrap-your-container.md

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ By default, boostrapping expects all the path fragments in your configuration to
224224

225225
namespace Acme\Demo;
226226

227-
use Cspray\AnnotatedContainer\Bootstrap\Bootstrap;use Cspray\AnnotatedContainer\Bootstrap\DefaultDefinitionProviderFactory;use Cspray\AnnotatedContainer\Bootstrap\DefaultParameterStoreFactory;use Cspray\AnnotatedContainer\ContainerFactory\PhpDiContainerFactory;use Cspray\AnnotatedContainer\Event\Emitter;
227+
use Cspray\AnnotatedContainer\Bootstrap\Bootstrap;use Cspray\AnnotatedContainer\Bootstrap\Configuration\DefaultDefinitionProviderFactory;use Cspray\AnnotatedContainer\Bootstrap\Configuration\DefaultParameterStoreFactory;use Cspray\AnnotatedContainer\ContainerFactory\PhpDiContainerFactory;use Cspray\AnnotatedContainer\Event\Emitter;
228228

229229
// This method is an implementation provided by the reader
230230
$directoryResolver = MyDirectoryResolver::create();
@@ -252,12 +252,7 @@ Setting up caching is something that you must explicitly opt into during your bo
252252

253253
namespace Acme\Demo;
254254

255-
use Cspray\AnnotatedContainer\Bootstrap\Bootstrap;
256-
use Cspray\AnnotatedContainer\Bootstrap\CacheAwareBootstrappingConfigurationProvider;
257-
use Cspray\AnnotatedContainer\Bootstrap\DefaultDefinitionProviderFactory;
258-
use Cspray\AnnotatedContainer\Bootstrap\DefaultParameterStoreFactory;
259-
use Cspray\AnnotatedContainer\Bootstrap\XmlBootstrappingConfigurationProvider;use Cspray\AnnotatedContainer\ContainerFactory\PhpDiContainerFactory;
260-
use Cspray\AnnotatedContainer\Definition\Cache\FileBackedContainerDefinitionCache;use Cspray\AnnotatedContainer\Definition\Serializer\XmlContainerDefinitionSerializer;use Cspray\AnnotatedContainer\Event\Emitter;
255+
use Cspray\AnnotatedContainer\Bootstrap\Bootstrap;use Cspray\AnnotatedContainer\Bootstrap\CacheAwareBootstrappingConfigurationProvider;use Cspray\AnnotatedContainer\Bootstrap\XmlBootstrappingConfigurationProvider;use Cspray\AnnotatedContainer\Definition\Cache\FileBackedContainerDefinitionCache;use Cspray\AnnotatedContainer\Definition\Serializer\XmlContainerDefinitionSerializer;use Cspray\AnnotatedContainer\Event\Emitter;
261256

262257
$container = Bootstrap::from(new Emitter())
263258
->bootstrapContainer(

known-issues.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
3-
<file src="src/Bootstrap/ServiceWiringListener.php">
3+
<file src="src/Bootstrap/Configuration/ContainerDefinitionAnalysisOptionsFromBootstrappingConfiguration.php">
4+
<ArgumentTypeCoercion>
5+
<code><![CDATA[$scanPaths]]></code>
6+
</ArgumentTypeCoercion>
7+
</file>
8+
<file src="src/Bootstrap/Listener/ServiceWiringListener.php">
49
<MixedArgument>
510
<code><![CDATA[$service]]></code>
611
</MixedArgument>
@@ -66,7 +71,7 @@
6671
[$serviceDelegateDefinition->classMethod()->class()->name(), $serviceDelegateDefinition->classMethod()->methodName()],
6772
$this->parametersForServiceDelegateToArray($injector, $state, $serviceDelegateDefinition),
6873
)]]></code>
69-
<code><![CDATA[$value]]></code>
74+
<code><![CDATA[$this->injector->make($id)]]></code>
7075
</MixedReturnStatement>
7176
</file>
7277
<file src="src/ContainerFactory/ContainerFactoryOptionsBuilder.php">
@@ -234,11 +239,8 @@
234239
</InvalidArgument>
235240
</file>
236241
<file src="src/StaticAnalysis/ContainerDefinitionAnalysisOptionsBuilder.php">
237-
<LessSpecificReturnStatement>
242+
<InvalidArgument>
238243
<code><![CDATA[$this->directories]]></code>
239-
</LessSpecificReturnStatement>
240-
<MoreSpecificReturnType>
241-
<code><![CDATA[array]]></code>
242-
</MoreSpecificReturnType>
244+
</InvalidArgument>
243245
</file>
244246
</files>

psalm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<MissingTemplateParam>
3737
<errorLevel type="suppress">
3838
<!-- This anonymous class is 100% documented with the appropriate template param but it is not getting picked up properly -->
39-
<file name="src/Bootstrap/ServiceWiringListener.php" />
39+
<file name="src/Bootstrap/Listener/ServiceWiringListener.php" />
4040
</errorLevel>
4141
</MissingTemplateParam>
4242
</issueHandlers>

src/Bootstrap/Bootstrap.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
namespace Cspray\AnnotatedContainer\Bootstrap;
44

55
use Cspray\AnnotatedContainer\AnnotatedContainer;
6+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\BootstrappingConfiguration;
7+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\ContainerDefinitionAnalysisOptionsFromBootstrappingConfiguration;
8+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\DefaultDefinitionProviderFactory;
9+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\DefaultListenerFactory;
10+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\DefaultParameterStoreFactory;
11+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\DefinitionProviderFactory;
12+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\ListenerFactory;
13+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\ParameterStoreFactory;
14+
use Cspray\AnnotatedContainer\Bootstrap\Configuration\XmlBootstrappingConfiguration;
15+
use Cspray\AnnotatedContainer\Bootstrap\DirectoryResolver\BootstrappingDirectoryResolver;
16+
use Cspray\AnnotatedContainer\Bootstrap\DirectoryResolver\VendorPresenceBasedBootstrappingDirectoryResolver;
617
use Cspray\AnnotatedContainer\ContainerFactory\ContainerFactory;
718
use Cspray\AnnotatedContainer\ContainerFactory\ContainerFactoryOptionsBuilder;
819
use Cspray\AnnotatedContainer\Definition\ContainerDefinition;
@@ -33,6 +44,7 @@ public static function fromAnnotatedContainerConventions(
3344
ContainerFactory $containerFactory,
3445
Emitter $emitter,
3546
ParameterStoreFactory $parameterStoreFactory = new DefaultParameterStoreFactory(),
47+
ListenerFactory $listenerFactory = new DefaultListenerFactory(),
3648
DefinitionProviderFactory $definitionProviderFactory = new DefaultDefinitionProviderFactory(),
3749
BootstrappingDirectoryResolver $directoryResolver = new VendorPresenceBasedBootstrappingDirectoryResolver(),
3850
Filesystem $filesystem = new PhpFunctionsFilesystem()
@@ -41,7 +53,8 @@ public static function fromAnnotatedContainerConventions(
4153
$filesystem,
4254
$directoryResolver->configurationPath('annotated-container.xml'),
4355
$parameterStoreFactory,
44-
$definitionProviderFactory
56+
$definitionProviderFactory,
57+
$listenerFactory
4558
);
4659

4760
return self::fromCompleteSetup(
@@ -75,6 +88,10 @@ public function bootstrapContainer(
7588

7689
$this->stopwatch->start();
7790

91+
foreach ($this->bootstrappingConfiguration->listeners() as $listener) {
92+
$this->emitter->addListener($listener);
93+
}
94+
7895
$analysisOptions = $this->analysisOptions($this->bootstrappingConfiguration);
7996

8097
$this->emitter->emitBeforeBootstrap($this->bootstrappingConfiguration);

src/Bootstrap/BootstrappingConfiguration.php renamed to src/Bootstrap/Configuration/BootstrappingConfiguration.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
<?php declare(strict_types=1);
22

3-
namespace Cspray\AnnotatedContainer\Bootstrap;
3+
namespace Cspray\AnnotatedContainer\Bootstrap\Configuration;
44

55
use Cspray\AnnotatedContainer\ArchitecturalDecisionRecords\SingleEntrypointDefinitionProvider;
6+
use Cspray\AnnotatedContainer\ContainerFactory\ParameterStore;
67
use Cspray\AnnotatedContainer\Definition\Cache\ContainerDefinitionCache;
8+
use Cspray\AnnotatedContainer\Event\Listener;
79
use Cspray\AnnotatedContainer\StaticAnalysis\DefinitionProvider;
8-
use Cspray\AnnotatedContainer\ContainerFactory\ParameterStore;
910

1011
interface BootstrappingConfiguration {
1112

@@ -23,4 +24,9 @@ public function containerDefinitionProvider() : ?DefinitionProvider;
2324
* @return list<ParameterStore>
2425
*/
2526
public function parameterStores() : array;
27+
28+
/**
29+
* @return list<Listener>
30+
*/
31+
public function listeners() : array;
2632
}

src/Bootstrap/CacheAwareBootstrappingConfiguration.php renamed to src/Bootstrap/Configuration/CacheAwareBootstrappingConfiguration.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace Cspray\AnnotatedContainer\Bootstrap;
3+
namespace Cspray\AnnotatedContainer\Bootstrap\Configuration;
44

55
use Cspray\AnnotatedContainer\ArchitecturalDecisionRecords\SingleEntrypointDefinitionProvider;
66
use Cspray\AnnotatedContainer\Definition\Cache\ContainerDefinitionCache;
@@ -30,4 +30,8 @@ public function containerDefinitionProvider() : ?DefinitionProvider {
3030
public function parameterStores() : array {
3131
return $this->configuration->parameterStores();
3232
}
33+
34+
public function listeners() : array {
35+
return $this->configuration->listeners();
36+
}
3337
}

src/Bootstrap/ContainerDefinitionAnalysisOptionsFromBootstrappingConfiguration.php renamed to src/Bootstrap/Configuration/ContainerDefinitionAnalysisOptionsFromBootstrappingConfiguration.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php declare(strict_types=1);
22

3-
namespace Cspray\AnnotatedContainer\Bootstrap;
3+
namespace Cspray\AnnotatedContainer\Bootstrap\Configuration;
44

5+
use Cspray\AnnotatedContainer\Bootstrap\DirectoryResolver\BootstrappingDirectoryResolver;
56
use Cspray\AnnotatedContainer\StaticAnalysis\ContainerDefinitionAnalysisOptions;
67
use Cspray\AnnotatedContainer\StaticAnalysis\ContainerDefinitionAnalysisOptionsBuilder;
78

src/Bootstrap/DefaultDefinitionProviderFactory.php renamed to src/Bootstrap/Configuration/DefaultDefinitionProviderFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php declare(strict_types=1);
22

3-
namespace Cspray\AnnotatedContainer\Bootstrap;
3+
namespace Cspray\AnnotatedContainer\Bootstrap\Configuration;
44

55
use Cspray\AnnotatedContainer\Exception\InvalidDefinitionProvider;
66
use Cspray\AnnotatedContainer\StaticAnalysis\DefinitionProvider;

0 commit comments

Comments
 (0)