Skip to content

Commit

Permalink
Implement BootstrappingConfigurationProvider (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
cspray authored Jun 5, 2024
1 parent 531f273 commit 7fd9a27
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 17 deletions.
6 changes: 4 additions & 2 deletions docs/how-to/02-bootstrap-your-container.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,14 @@ Perhaps you didn't name your configuration file the default, it is recommended y
namespace Acme\Demo;

use Cspray\AnnotatedContainer\Bootstrap\Bootstrap;
use Cspray\AnnotatedContainer\Event\Emitter;use Cspray\AnnotatedContainer\Profiles;
use Cspray\AnnotatedContainer\Bootstrap\XmlBootstrappingConfigurationProvider;use Cspray\AnnotatedContainer\Event\Emitter;use Cspray\AnnotatedContainer\Profiles;

$container = Bootstrap::fromMinimalSetup(new Emitter)
->bootstrapContainer(configurationFile: 'my-container.xml');
->bootstrapContainer(bootstrappingConfigurationProvider: new XmlBootstrappingConfigurationProvider('my-container.xml'));
```

If you require a configuration that is not the default XML files, you can implement your own `Cspray\AnnotatedContainer\Bootstrap\BootstrappingConfigurationProvider` instead.

#### Constructing DefinitionProvider

There might be dependencies you need to determine what third-party services should be included in your `DefinitionProvider` implementations. If so, there's a `Cspray\AnnotatedContainer\Bootstrap\DefinitionProviderFactory` interface that you can implement and then pass that instance to the `Bootstrap::minimalSetup()` method.
Expand Down
18 changes: 6 additions & 12 deletions src/Bootstrap/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,17 @@ public static function fromCompleteSetup(

public function bootstrapContainer(
Profiles $profiles = null,
string $configurationFile = 'annotated-container.xml'
BootstrappingConfigurationProvider $bootstrappingConfigurationProvider = new XmlBootstrappingConfigurationProvider()
) : AnnotatedContainer {
$profiles ??= Profiles::defaultOnly();

$this->stopwatch->start();

$configuration = $this->bootstrappingConfiguration($configurationFile);
$configuration = $bootstrappingConfigurationProvider->bootstrappingConfiguration(
$this->directoryResolver,
$this->parameterStoreFactory,
$this->definitionProviderFactory
);
$analysisOptions = $this->analysisOptions($configuration);

$this->emitter->emitBeforeBootstrap($configuration);
Expand Down Expand Up @@ -132,16 +136,6 @@ public function bootstrapContainer(
return $container;
}

private function bootstrappingConfiguration(string $configurationFile) : BootstrappingConfiguration {
$configFile = $this->directoryResolver->configurationPath($configurationFile);
return new XmlBootstrappingConfiguration(
$configFile,
parameterStoreFactory: $this->parameterStoreFactory,
definitionProviderFactory: $this->definitionProviderFactory
);
}


private function analysisOptions(BootstrappingConfiguration $configuration) : ContainerDefinitionAnalysisOptions {
$scanPaths = [];
foreach ($configuration->scanDirectories() as $scanDirectory) {
Expand Down
12 changes: 12 additions & 0 deletions src/Bootstrap/BootstrappingConfigurationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainer\Bootstrap;

interface BootstrappingConfigurationProvider {

public function bootstrappingConfiguration(
BootstrappingDirectoryResolver $directoryResolver,
ParameterStoreFactory $parameterStoreFactory,
DefinitionProviderFactory $definitionProviderFactory,
) : BootstrappingConfiguration;
}
24 changes: 24 additions & 0 deletions src/Bootstrap/XmlBootstrappingConfigurationProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainer\Bootstrap;

final class XmlBootstrappingConfigurationProvider implements BootstrappingConfigurationProvider {

public function __construct(
private readonly string $fileName = 'annotated-container.xml'
) {
}

public function bootstrappingConfiguration(
BootstrappingDirectoryResolver $directoryResolver,
ParameterStoreFactory $parameterStoreFactory,
DefinitionProviderFactory $definitionProviderFactory,
) : BootstrappingConfiguration {
$configFile = $directoryResolver->configurationPath($this->fileName);
return new XmlBootstrappingConfiguration(
$configFile,
$parameterStoreFactory,
$definitionProviderFactory
);
}
}
5 changes: 3 additions & 2 deletions src/Cli/Command/ValidateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Cspray\AnnotatedContainer\Bootstrap\DefaultDefinitionProviderFactory;
use Cspray\AnnotatedContainer\Bootstrap\DefaultParameterStoreFactory;
use Cspray\AnnotatedContainer\Bootstrap\PostAnalysisObserver;
use Cspray\AnnotatedContainer\Bootstrap\XmlBootstrappingConfigurationProvider;
use Cspray\AnnotatedContainer\Cli\Command;
use Cspray\AnnotatedContainer\Cli\Exception\ConfigurationNotFound;
use Cspray\AnnotatedContainer\Cli\Input;
Expand Down Expand Up @@ -156,8 +157,8 @@ public function handle(Input $input, TerminalOutput $output) : int {
$profiles = Profiles::fromList($inputProfiles);

$bootstrap->bootstrapContainer(
profiles: $profiles,
configurationFile: $configOption
$profiles,
new XmlBootstrappingConfigurationProvider($configOption)
);
assert($containerDefinition instanceof ContainerDefinition);

Expand Down
5 changes: 4 additions & 1 deletion test/Unit/BootstrapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Cspray\AnnotatedContainer\Bootstrap\ServiceFromServiceDefinition;
use Cspray\AnnotatedContainer\Bootstrap\ServiceGatherer;
use Cspray\AnnotatedContainer\Bootstrap\ServiceWiringListener;
use Cspray\AnnotatedContainer\Bootstrap\XmlBootstrappingConfigurationProvider;
use Cspray\AnnotatedContainer\ContainerFactory\AurynContainerFactory;
use Cspray\AnnotatedContainer\ContainerFactory\ContainerFactory;
use Cspray\AnnotatedContainer\Definition\ContainerDefinition;
Expand Down Expand Up @@ -247,7 +248,9 @@ public function testBootstrapSingleConcreteServiceUsesCustomFileName() : void {
new DefaultDefinitionProviderFactory(),
new Stopwatch()
);
$container = $bootstrap->bootstrapContainer(configurationFile: 'my-container.xml.dist');
$container = $bootstrap->bootstrapContainer(
bootstrappingConfigurationProvider: new XmlBootstrappingConfigurationProvider('my-container.xml.dist')
);

$service = $container->get(Fixtures::singleConcreteService()->fooImplementation()->getName());

Expand Down

0 comments on commit 7fd9a27

Please sign in to comment.