Skip to content

Commit

Permalink
Merge pull request #358 from lcobucci/reorganise-named-constructors
Browse files Browse the repository at this point in the history
Deprecate the `default` named constructor
  • Loading branch information
lcobucci authored Oct 11, 2021
2 parents 3fe88f5 + 9dae039 commit cf9a0bd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 15 deletions.
10 changes: 5 additions & 5 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
Setting up the container can be a complicated task.
In order to make the it easier, we provide a builder.

The builder is initialised via a named constructor using XML as the default format.
The builder is initialised via a named constructors, which represent the format of files to be parsed (php, xml, yaml).
You may use `ContainerBuilder::delegating()` to allow all formats to be used.

Once everything is configured, the builder gives you a fully functional container:

```php
Expand All @@ -16,7 +18,7 @@ use Lcobucci\DependencyInjection\ContainerBuilder;

// The path to the current file is passed so we can track changes
// to it and refresh the cache (for development mode)
$builder = ContainerBuilder::default(__FILE__, __NAMESPACE__);
$builder = ContainerBuilder::xml(__FILE__, __NAMESPACE__);

$container = $builder->getContainer();

Expand All @@ -27,8 +29,6 @@ $testContainer = $builder->getTestContainer();

## Available methods

* `ContainerBuilder#setGenerator()`: Modifies the generator to be used.
We support the `XML`, `Yaml`, `PHP`, and `Delegating` generators (the latter allows to use all formats together)
* `ContainerBuilder#addPath()`: Add a base path to find files
* `ContainerBuilder#addFile()`: Adds a container source file
* `ContainerBuilder#addPass()`: Adds an instance of a [Compiler Pass](compiler-passes.md) to be processed
Expand Down Expand Up @@ -56,7 +56,7 @@ use function getenv;

require __DIR__ . '/../vendor/autoload.php';

$builder = ContainerBuilder::default(__FILE__, __NAMESPACE__);
$builder = ContainerBuilder::xml(__FILE__, __NAMESPACE__);
$projectRoot = dirname(__DIR__);

if (getenv('APPLICATION_MODE', true) === 'development') {
Expand Down
2 changes: 2 additions & 0 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ interface Builder

/**
* Changes the generator to handle the files
*
* @deprecated This is deprecated in favour of using the correct naming constructor.
*/
public function setGenerator(Generator $generator): Builder;

Expand Down
49 changes: 43 additions & 6 deletions src/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use Lcobucci\DependencyInjection\Compiler\ParameterBag;
use Lcobucci\DependencyInjection\Config\ContainerConfiguration;
use Lcobucci\DependencyInjection\Generators\Xml as XmlGenerator;
use Lcobucci\DependencyInjection\Testing\MakeServicesPublic;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
Expand All @@ -25,13 +24,51 @@ public function __construct(
$this->setDefaultConfiguration();
}

public static function default(
string $configurationFile,
string $namespace,
): self {
/**
* @deprecated Use the named constructor according to the generator
*
* @see ContainerBuilder::xml()
* @see ContainerBuilder::yaml()
* @see ContainerBuilder::php()
* @see ContainerBuilder::delegating()
*/
public static function default(string $configurationFile, string $namespace): self
{
return self::xml($configurationFile, $namespace);
}

public static function xml(string $configurationFile, string $namespace): self
{
return new self(
new ContainerConfiguration($namespace),
new Generators\Xml($configurationFile),
new ParameterBag(),
);
}

public static function php(string $configurationFile, string $namespace): self
{
return new self(
new ContainerConfiguration($namespace),
new Generators\Php($configurationFile),
new ParameterBag(),
);
}

public static function yaml(string $configurationFile, string $namespace): self
{
return new self(
new ContainerConfiguration($namespace),
new Generators\Yaml($configurationFile),
new ParameterBag(),
);
}

public static function delegating(string $configurationFile, string $namespace): self
{
return new self(
new ContainerConfiguration($namespace),
new XmlGenerator($configurationFile),
new Generators\Delegating($configurationFile),
new ParameterBag(),
);
}
Expand Down
23 changes: 19 additions & 4 deletions test/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Lcobucci\DependencyInjection\Compiler\ParameterBag;
use Lcobucci\DependencyInjection\Config\ContainerConfiguration;
use Lcobucci\DependencyInjection\Config\Package;
use Lcobucci\DependencyInjection\Generators\Xml as XmlGenerator;
use Lcobucci\DependencyInjection\Testing\MakeServicesPublic;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -43,20 +42,36 @@ public function configureDependencies(): void

/**
* @test
* @dataProvider supportedFormats
*
* @covers ::default
* @covers ::xml
* @covers ::delegating
* @covers ::php
* @covers ::yaml
* @covers ::__construct
* @covers ::setDefaultConfiguration
*/
public function defaultShouldSimplifyTheObjectCreation(): void
public function namedConstructorsShouldSimplifyTheObjectCreation(string $method, Generator $generator): void
{
$expected = new ContainerBuilder(
new ContainerConfiguration('Lcobucci\\DependencyInjection'),
new XmlGenerator(__FILE__),
$generator,
new ParameterBag(),
);

self::assertEquals($expected, ContainerBuilder::default(__FILE__, __NAMESPACE__));
// @phpstan-ignore-next-line
self::assertEquals($expected, ContainerBuilder::$method(__FILE__, __NAMESPACE__));
}

/** @return iterable<string, array{string, Generator}> */
public function supportedFormats(): iterable
{
yield 'default' => ['default', new Generators\Xml(__FILE__)];
yield 'xml' => ['xml', new Generators\Xml(__FILE__)];
yield 'yaml' => ['yaml', new Generators\Yaml(__FILE__)];
yield 'php' => ['php', new Generators\Php(__FILE__)];
yield 'delegating' => ['delegating', new Generators\Delegating(__FILE__)];
}

/**
Expand Down

0 comments on commit cf9a0bd

Please sign in to comment.