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

Ensure decorator collections work, update changelog, deprecate cacheD… #392

Merged
merged 1 commit into from
Jun 7, 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
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
4 changes: 4 additions & 0 deletions fixture_src/Fixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ public static function thirdPartyKitchenSink() : ThirdPartyKitchenSinkFixture {
return new ThirdPartyKitchenSinkFixture();
}

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 @@ -226,7 +226,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
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->serviceDefinitions() as $serviceDefinition) {
if ($serviceDefinition->isAbstract()) {
if ($serviceDefinition->isAbstract() || $serviceDefinition->type()->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 @@ -166,10 +166,10 @@ protected function createAnnotatedContainer(ContainerFactoryState $state, Profil

$container->when($service)
->needs($paramIdentifier)
->give(function() use($state, $container, $value) {
->give(function() use($state, $container, $value, $service): mixed {
$values = [];
foreach ($state->containerDefinition->serviceDefinitions() as $serviceDefinition) {
if ($serviceDefinition->isAbstract()) {
if ($serviceDefinition->isAbstract() || $serviceDefinition->type()->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 @@ -46,7 +46,7 @@ public function addMethodInject(string $class, string $method, string $param, mi
if ($value instanceof ServiceCollectorReference) {
$values = [];
foreach ($this->containerDefinition->serviceDefinitions() as $serviceDefinition) {
if ($serviceDefinition->isAbstract()) {
if ($serviceDefinition->isAbstract() || $serviceDefinition->type()->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 @@ -471,4 +471,33 @@ public function testContainerCreationEventsEmitted() : void {

self::assertSame(['BeforeContainerCreation', 'AfterContainerCreation'], $listener->getTriggeredEvents());
}

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
);
}
}
Loading