Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove observers, add events to container factory #357

Merged
merged 2 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions annotated-container.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<xs:element name="cacheDir" type="xs:token" minOccurs="0" maxOccurs="1"/>
<xs:element name="definitionProviders" type="definitionProvidersType" minOccurs="0" maxOccurs="1" />
<xs:element name="parameterStores" type="parameterStoresType" minOccurs="0" maxOccurs="1" />
<xs:element name="observers" type="observersType" minOccurs="0" maxOccurs="1" />
</xs:all>
</xs:complexType>

Expand Down Expand Up @@ -75,12 +74,6 @@
</xs:sequence>
</xs:complexType>

<xs:complexType name="observersType">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="observer" type="xs:token" />
</xs:sequence>
</xs:complexType>

<xs:complexType name="definitionProvidersType">
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element name="definitionProvider" type="xs:token" />
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
},
"files": [
"fixture_src/VendorScanningInitializers/vendor/cspray/package/other_src/DependencyDefinitionProvider.php",
"fixture_src/VendorScanningInitializers/vendor/cspray/package/src/DependencyObserver.php",
"fixture_src/VendorScanningInitializers/vendor/cspray/package/src/FirstInitializer.php",
"fixture_src/VendorScanningInitializers/vendor/cspray/package/src/SecondInitializer.php",
"fixture_src/VendorScanningInitializers/vendor/cspray/package/src/SomeService.php",
Expand Down

This file was deleted.

62 changes: 7 additions & 55 deletions known-issues.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.30.0@d0bc6e25d89f649e4f36a534f330f8bb4643dd69">
<file src="src/Bootstrap/Bootstrap.php">
<DeprecatedClass occurrences="5">
<code>?ObserverFactory</code>
<code>ObserverFactory</code>
<code>PreAnalysisObserver|PostAnalysisObserver|ContainerCreatedObserver|ContainerAnalyticsObserver $observer</code>
<code>array</code>
<code>private array $observers = [];</code>
</DeprecatedClass>
<DeprecatedMethod occurrences="1">
<code>getObservers</code>
</DeprecatedMethod>
</file>
<file src="src/Bootstrap/BootstrappingConfiguration.php">
<DeprecatedClass occurrences="1">
<code>list&lt;PreAnalysisObserver|PostAnalysisObserver|ContainerCreatedObserver|ContainerAnalyticsObserver&gt;</code>
</DeprecatedClass>
</file>
<file src="src/Bootstrap/ComposerJsonScanningThirdPartyInitializerProvider.php">
<MixedArrayAccess occurrences="1">
<code>$composerData['extra']</code>
Expand All @@ -35,34 +18,13 @@
<code>isDot</code>
</UndefinedMethod>
</file>
<file src="src/Bootstrap/ObserverFactory.php">
<DeprecatedClass occurrences="1">
<code>PreAnalysisObserver|PostAnalysisObserver|ContainerCreatedObserver</code>
</DeprecatedClass>
</file>
<file src="src/Bootstrap/ServiceWiringObserver.php">
<DeprecatedInterface occurrences="1">
<code>ServiceWiringObserver</code>
</DeprecatedInterface>
<LessSpecificReturnStatement occurrences="1">
<code>$services</code>
</LessSpecificReturnStatement>
<MoreSpecificReturnType occurrences="1">
<code>array</code>
</MoreSpecificReturnType>
</file>
<file src="src/Bootstrap/XmlBootstrappingConfiguration.php">
<DeprecatedClass occurrences="3">
<code>array</code>
<code>list&lt;PreAnalysisObserver|PostAnalysisObserver|ContainerCreatedObserver|ContainerAnalyticsObserver&gt;</code>
<code>private</code>
</DeprecatedClass>
<InvalidStringClass occurrences="1">
<code>new $observerClass()</code>
</InvalidStringClass>
<PropertyTypeCoercion occurrences="1">
<code>$observers</code>
</PropertyTypeCoercion>
<file src="src/Bootstrap/ServiceWiringListener.php">
<MixedArgument occurrences="1">
<code>$service</code>
</MixedArgument>
<MixedAssignment occurrences="1">
<code>$service</code>
</MixedAssignment>
</file>
<file src="src/Cli/Command/BuildCommand.php">
<MixedArrayAccess occurrences="1">
Expand All @@ -85,9 +47,6 @@
</MixedAssignment>
</file>
<file src="src/Cli/Command/InitCommand.php">
<DeprecatedMethod occurrences="1">
<code>getObserverClasses</code>
</DeprecatedMethod>
<MixedArgument occurrences="3">
<code>$composer</code>
<code>$configName</code>
Expand All @@ -109,13 +68,6 @@
</MixedAssignment>
</file>
<file src="src/Cli/Command/ValidateCommand.php">
<DeprecatedClass occurrences="2">
<code>PostAnalysisObserver</code>
<code>PostAnalysisObserver</code>
</DeprecatedClass>
<DeprecatedInterface occurrences="1">
<code>class($closure) implements PostAnalysisObserver {</code>
</DeprecatedInterface>
<PossiblyInvalidArgument occurrences="3">
<code>$configOption</code>
<code>$configOption</code>
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</exclude>
<report>
<text outputFile="php://stdout" showOnlySummary="true" />
<!-- <html outputDirectory="build/code-coverage/html" /> -->
<html outputDirectory="build/code-coverage/html" />
</report>
</coverage>
</phpunit>
88 changes: 6 additions & 82 deletions src/Bootstrap/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
use Cspray\AnnotatedContainer\AnnotatedContainer;
use Cspray\AnnotatedContainer\Definition\ContainerDefinition;
use Cspray\AnnotatedContainer\Event\BootstrapEmitter;
use Cspray\AnnotatedContainer\Event\ContainerFactoryEmitter;
use Cspray\AnnotatedContainer\Event\Emitter;
use Cspray\AnnotatedContainer\Profiles;
use Cspray\AnnotatedContainer\StaticAnalysis\AnnotatedTargetContainerDefinitionAnalyzer;
use Cspray\AnnotatedContainer\StaticAnalysis\CacheAwareContainerDefinitionAnalyzer;
Expand Down Expand Up @@ -35,31 +37,22 @@ final class Bootstrap {

private readonly ?DefinitionProviderFactory $definitionProviderFactory;

private readonly ?ObserverFactory $observerFactory;

private readonly ?ContainerFactory $containerFactory;
private readonly ContainerFactory $containerFactory;

private readonly Stopwatch $stopwatch;

/**
* @var list<PreAnalysisObserver|PostAnalysisObserver|ContainerCreatedObserver|ContainerAnalyticsObserver>
*/
private array $observers = [];

public function __construct(
ContainerFactory $containerFactory,
BootstrapEmitter $emitter = null,
BootstrappingDirectoryResolver $directoryResolver = null,
ParameterStoreFactory $parameterStoreFactory = null,
DefinitionProviderFactory $definitionProviderFactory = null,
ObserverFactory $observerFactory = null,
Stopwatch $stopwatch = null,
ContainerFactory $containerFactory = null
) {
$this->emitter = $emitter;
$this->directoryResolver = $directoryResolver ?? $this->defaultDirectoryResolver();
$this->parameterStoreFactory = $parameterStoreFactory ?? new DefaultParameterStoreFactory();
$this->definitionProviderFactory = $definitionProviderFactory;
$this->observerFactory = $observerFactory;
$this->stopwatch = $stopwatch ?? new Stopwatch();
$this->containerFactory = $containerFactory;
}
Expand All @@ -73,10 +66,6 @@ private function defaultDirectoryResolver() : BootstrappingDirectoryResolver {
return new RootDirectoryBootstrappingDirectoryResolver($rootDir);
}

public function addObserver(PreAnalysisObserver|PostAnalysisObserver|ContainerCreatedObserver|ContainerAnalyticsObserver $observer) : void {
$this->observers[] = $observer;
}

public function bootstrapContainer(
Profiles $profiles,
string $configurationFile = 'annotated-container.xml'
Expand All @@ -89,16 +78,9 @@ public function bootstrapContainer(

$this->emitter?->emitBeforeBootstrap($configuration);

foreach ($configuration->getObservers() as $observer) {
$this->addObserver($observer);
}

$this->notifyPreAnalysis($profiles);

$analysisPrepped = $this->stopwatch->mark();

$containerDefinition = $this->runStaticAnalysis($configuration, $analysisOptions);
$this->notifyPostAnalysis($profiles, $containerDefinition);

$analysisCompleted = $this->stopwatch->mark();

Expand All @@ -108,11 +90,8 @@ public function bootstrapContainer(
$containerDefinition,
);

$this->notifyContainerCreated($profiles, $containerDefinition, $container);

$metrics = $this->stopwatch->stop();
$analytics = $this->createAnalytics($metrics, $analysisPrepped, $analysisCompleted);
$this->notifyAnalytics($analytics);

$this->emitter?->emitAfterBootstrap(
$configuration,
Expand All @@ -129,7 +108,6 @@ private function bootstrappingConfiguration(string $configurationFile) : Bootstr
return new XmlBootstrappingConfiguration(
$configFile,
parameterStoreFactory: $this->parameterStoreFactory,
observerFactory: $this->observerFactory,
definitionProviderFactory: $this->definitionProviderFactory
);
}
Expand All @@ -149,14 +127,6 @@ private function analysisOptions(BootstrappingConfiguration $configuration) : Co
return $analysisOptions->build();
}

private function notifyPreAnalysis(Profiles $activeProfiles) : void {
foreach ($this->observers as $observer) {
if ($observer instanceof PreAnalysisObserver) {
$observer->notifyPreAnalysis($activeProfiles);
}
}
}

private function runStaticAnalysis(
BootstrappingConfiguration $configuration,
ContainerDefinitionAnalysisOptions $analysisOptions
Expand All @@ -181,56 +151,18 @@ private function containerDefinitionAnalyzer(?string $cacheDir) : ContainerDefin
return $compiler;
}

private function notifyPostAnalysis(Profiles $activeProfiles, ContainerDefinition $containerDefinition) : void {
foreach ($this->observers as $observer) {
if ($observer instanceof PostAnalysisObserver) {
$observer->notifyPostAnalysis($activeProfiles, $containerDefinition);
}
}
}

private function createContainer(
BootstrappingConfiguration $configuration,
Profiles $activeProfiles,
ContainerDefinition $containerDefinition,
) : AnnotatedContainer {
$containerFactory = $this->containerFactory();

foreach ($configuration->getParameterStores() as $parameterStore) {
$containerFactory->addParameterStore($parameterStore);
$this->containerFactory->addParameterStore($parameterStore);
}

$factoryOptions = ContainerFactoryOptionsBuilder::forProfiles($activeProfiles);

return $containerFactory->createContainer($containerDefinition, $factoryOptions->build());
}

private function containerFactory() : ContainerFactory {
if ($this->containerFactory !== null) {
return $this->containerFactory;
}

if (class_exists(Injector::class)) {
return new AurynContainerFactory();
}

if (class_exists(Container::class)) {
return new PhpDiContainerFactory();
}

throw BackingContainerNotFound::fromMissingImplementation();
}

private function notifyContainerCreated(
Profiles $activeProfiles,
ContainerDefinition $containerDefinition,
AnnotatedContainer $container
) : void {
foreach ($this->observers as $observer) {
if ($observer instanceof ContainerCreatedObserver) {
$observer->notifyContainerCreated($activeProfiles, $containerDefinition, $container);
}
}
return $this->containerFactory->createContainer($containerDefinition, $factoryOptions->build());
}

private function createAnalytics(
Expand All @@ -246,12 +178,4 @@ private function createAnalytics(
);
}

private function notifyAnalytics(ContainerAnalytics $analytics) : void {
foreach ($this->observers as $observer) {
if ($observer instanceof ContainerAnalyticsObserver) {
$observer->notifyAnalytics($analytics);
}
}
}

}
6 changes: 0 additions & 6 deletions src/Bootstrap/BootstrappingConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,4 @@ public function getContainerDefinitionProvider() : ?DefinitionProvider;
*/
public function getParameterStores() : array;

/**
* @return list<PreAnalysisObserver|PostAnalysisObserver|ContainerCreatedObserver|ContainerAnalyticsObserver>
* @deprecated
*/
public function getObservers() : array;

}
12 changes: 0 additions & 12 deletions src/Bootstrap/ContainerAnalyticsObserver.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Bootstrap/ContainerCreatedObserver.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/Bootstrap/ObserverFactory.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Bootstrap/PostAnalysisObserver.php

This file was deleted.

Loading
Loading