Skip to content

Commit

Permalink
Ensure decorator collections work, update changelog, deprecate cacheD…
Browse files Browse the repository at this point in the history
…ir (#391)

* Ensure decorator collections work, update changelog, deprecate cacheDir

* Fix missing closure return type
  • Loading branch information
cspray committed Jun 7, 2024
1 parent 0768643 commit 1a5fba2
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 16 deletions.
46 changes: 46 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,52 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v2.4.0](https://github.com/cspray/annotated-container/tree/v2.4.0) - 2024-06-08

### Added

- Added the ability to inject a collection of services as an array or a custom collection by passing an implementation of `Cspray\AnnotatedContainer\ContainerFactory\ListOf` to an `#[Inject]` attribute.
- Added `Cspray\AnnotatedContainer\ContainerFactory\ListOfAsArray` implementation of to allow implementing a collection of services as an array out-of-the-box

### Deprecated

Several portions of the library will now trigger deprecation notices. These notices are to indicate you're using a feature that will be removed in 3.0. Each message will explain what feature is being used and what it is being replaced with. Some deprecations can be replaced now while others do not have suitable replacements until 3.0 is launched.

#### Observer Removal Deprecations

In v3 the bootstrapping Observer system is being replaced with a more complete Event system that encompasses the entire Annotated Container lifecycle. The deprecations in this section are related to the removal of this system. There is no suitable replacement until v3 hits for these deprecations. However, transitioning to the new Event system mostly involves implementing a new interface and adjusting a method signature. Your implementations should not require adjustments.

- Providing a `Cspray\AnnotatedContainer\Bootstrap\ObserverFactory` during your bootstrapping process will trigger a deprecation.
- Calling `Cspray\AnnotatedContainer\Bootstrap::addObserver` during your bootstrapping process will trigger a deprecation.
- Extending and using the `Cspray\AnnotatedContainer\Bootstrap\ServiceWiringObserver` will trigger a deprecation.
- Providing any observers in your `annotated-container.xml` file will trigger a deprecation.

#### Transition `ActiveProfiles` to `Profiles`

In v3 the `Cspray\AnnotatedContainer\Profiles` module has been removed and drastically simplified. In v2.x this module was overly complicated and not designed properly for usability in mind. This led to funky code dealing with these complications and a subpar user experience. The entire module was replaced by a single value object and a set of static constructors. There is no suitable replacement until v3 hits for these deprecations.

- Calling `Cspray\AnnotatedContainer\Profiles\ActiveProfiles::getProfiles` or `ActiveProfiles::isActive` will trigger a deprecation.
- Using the `Cspray\AnnotatedContainer\Profiles\ActiveProfilesBuilder` will trigger a deprecation.
- Calling `Cspray\AnnotatedContainer\Profiles\CsvActiveProfilesParser` will trigger a deprecation.

#### Removing Logging

In v3 Logging has been moved to a separate library that has to be explicitly opted in via the new Event system. Making this a separate, explicit opt-in that you must configure greatly simplifies the bootstrapping process and reduces the amount of code in Annotated Container.

- Defining a `<logging></logging>` configuration in `annoatated-container.xml` will trigger a deprecation.

#### Removing Configuration Attribute

The #[Configuration] Attribute has long been deprecated. It was not well-thought-out and the sole reason the ability to inject into properties existed. It has long been recommended to move to using the #[Service] Attribute directly or implementing your own custom attribute.

- Defining an instance of `Cspray\AnnotatedContainer\Attribute\ConfigurationAttribute` will trigger a deprecation.

#### Removing `cacheDir` configuration

In v3 caching functionality is drastically improved and much more control is provided over how your ContainerDefinition is cached. This new caching system must be setup as part of your bootstrapping and can't be configured.

- Defining a `<cacheDir></cacheDir>` configuration in `annotated-container.xml` will trigger a deprecation.

## [v2.3.0](https://github.com/cspray/annotated-container/tree/v2.3.0) - 2024-05-22

### Changed
Expand Down
25 changes: 14 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions fixture_src/Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,8 @@ public static function injectServiceDomainCollection() : InjectServiceDomainColl
return new InjectServiceDomainCollectionFixture();
}

public static function injectServiceCollectionDecorator() : InjectServiceCollectionDecoratorFixture {
return new InjectServiceCollectionDecoratorFixture();
}

}
10 changes: 10 additions & 0 deletions fixture_src/InjectServiceCollectionDecorator/BarImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;

use Cspray\AnnotatedContainer\Attribute\Service;

#[Service]
class BarImplementation implements FooInterface {

}
10 changes: 10 additions & 0 deletions fixture_src/InjectServiceCollectionDecorator/BazImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;

use Cspray\AnnotatedContainer\Attribute\Service;

#[Service]
class BazImplementation implements FooInterface {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;

use Cspray\AnnotatedContainer\Attribute\Inject;
use Cspray\AnnotatedContainer\Attribute\Service;
use Cspray\AnnotatedContainer\ContainerFactory\ListOfAsArray;

#[Service(primary: true)]
class CompositeFooImplementation implements FooInterface {

public function __construct(
#[Inject(new ListOfAsArray(FooInterface::class))]
public readonly array $foos
) {}

}
10 changes: 10 additions & 0 deletions fixture_src/InjectServiceCollectionDecorator/FooImplementation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;

use Cspray\AnnotatedContainer\Attribute\Service;

#[Service]
class FooImplementation implements FooInterface {

}
10 changes: 10 additions & 0 deletions fixture_src/InjectServiceCollectionDecorator/FooInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;

use Cspray\AnnotatedContainer\Attribute\Service;

#[Service]
interface FooInterface {

}
14 changes: 14 additions & 0 deletions fixture_src/InjectServiceCollectionDecorator/FooService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;

use Cspray\AnnotatedContainer\Attribute\Service;

#[Service]
class FooService {

public function __construct(
public readonly FooInterface $foo
) {}

}
43 changes: 43 additions & 0 deletions fixture_src/InjectServiceCollectionDecoratorFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php declare(strict_types=1);

namespace Cspray\AnnotatedContainerFixture;

use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\BarImplementation;
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\BazImplementation;
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\CompositeFooImplementation;
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\FooImplementation;
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\FooInterface;
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\FooService;
use Cspray\Typiphy\ObjectType;
use function Cspray\Typiphy\objectType;

class InjectServiceCollectionDecoratorFixture implements Fixture {

public function getPath() : string {
return __DIR__ . '/InjectServiceCollectionDecorator';
}

public function fooService() : ObjectType {
return objectType(FooService::class);
}

public function compositeFoo() : ObjectType {
return objectType(CompositeFooImplementation::class);
}

public function fooInterface() : ObjectType {
return objectType(FooInterface::class);
}

public function fooImplementation() : ObjectType {
return objectType(FooImplementation::class);
}

public function barImplementation() : ObjectType {
return objectType(BarImplementation::class);
}

public function bazImplementation() : ObjectType {
return objectType(BazImplementation::class);
}
}
1 change: 0 additions & 1 deletion known-issues.xml
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@
<code><![CDATA[$created]]></code>
</MissingClosureParamType>
<MissingClosureReturnType>
<code><![CDATA[function() use($state, $container, $value) {]]></code>
<code><![CDATA[static fn(Container $container) => $container->call([$target, $delegateInfo['delegateMethod']])]]></code>
</MissingClosureReturnType>
<MixedArgument>
Expand Down
7 changes: 7 additions & 0 deletions src/Bootstrap/XmlBootstrappingConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ public function __construct(
$cache = $cacheDirNodes[0]->textContent;
}

if ($cache !== null) {
trigger_error(
'A cache directory is defined in your configuration. The ability to define a cache through configuration is removed in v3 and must setup as part of your bootstrapping code.',
E_USER_DEPRECATED
);
}

$loggingFileNodes = $xpath->query('/ac:annotatedContainer/ac:logging/ac:file/text()');
$loggingStdoutNodes = $xpath->query('/ac:annotatedContainer/ac:logging/ac:stdout');
$logger = null;
Expand Down
2 changes: 1 addition & 1 deletion src/ContainerFactory/AurynContainerFactoryState.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function addMethodInject(string $class, string $method, string $param, mi
$key = '+' . $param;
$values = [];
foreach ($this->containerDefinition->getServiceDefinitions() as $serviceDefinition) {
if ($serviceDefinition->isAbstract()) {
if ($serviceDefinition->isAbstract() || $serviceDefinition->getType()->getName() === $class) {
continue;
}

Expand Down
4 changes: 2 additions & 2 deletions src/ContainerFactory/IlluminateContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,10 @@ protected function createAnnotatedContainer(ContainerFactoryState $state, Active

$container->when($service)
->needs($paramIdentifier)
->give(function() use($state, $container, $value) {
->give(function() use($state, $container, $value, $service): mixed {
$values = [];
foreach ($state->containerDefinition->getServiceDefinitions() as $serviceDefinition) {
if ($serviceDefinition->isAbstract()) {
if ($serviceDefinition->isAbstract() || $serviceDefinition->getType()->getName() === $service) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/ContainerFactory/PhpDiContainerFactoryState.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function addMethodInject(string $class, string $method, string $param, mi
if ($value instanceof ServiceCollectorReference) {
$values = [];
foreach ($this->containerDefinition->getServiceDefinitions() as $serviceDefinition) {
if ($serviceDefinition->isAbstract()) {
if ($serviceDefinition->isAbstract() || $serviceDefinition->getType()->getName() === $class) {
continue;
}

Expand Down
29 changes: 29 additions & 0 deletions test/Unit/ContainerFactoryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1000,4 +1000,33 @@ public function testCreatingServiceWithInjectServiceDomainCollection() : void {
$collectionInjector->collection->services
);
}

public function testCreatingServiceWithInjectServiceCollectionDecorator() : void {
$container = $this->getContainer(Fixtures::injectServiceCollectionDecorator()->getPath());

$fooService = $container->get(Fixtures::injectServiceCollectionDecorator()->fooService()->getName());

self::assertInstanceOf(
Fixtures::injectServiceCollectionDecorator()->fooService()->getName(),
$fooService
);
self::assertInstanceOf(
Fixtures::injectServiceCollectionDecorator()->compositeFoo()->getName(),
$fooService->foo
);
self::assertCount(3, $fooService->foo->foos);
$fooClasses = array_map(static fn(object $foo) => $foo::class, $fooService->foo->foos);
self::assertContains(
Fixtures::injectServiceCollectionDecorator()->fooImplementation()->getName(),
$fooClasses
);
self::assertContains(
Fixtures::injectServiceCollectionDecorator()->barImplementation()->getName(),
$fooClasses
);
self::assertContains(
Fixtures::injectServiceCollectionDecorator()->bazImplementation()->getName(),
$fooClasses
);
}
}

0 comments on commit 1a5fba2

Please sign in to comment.