Skip to content

Commit 1a5fba2

Browse files
authored
Ensure decorator collections work, update changelog, deprecate cacheDir (#391)
* Ensure decorator collections work, update changelog, deprecate cacheDir * Fix missing closure return type
1 parent 0768643 commit 1a5fba2

16 files changed

+218
-16
lines changed

CHANGELOG.md

+46
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,52 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [v2.4.0](https://github.com/cspray/annotated-container/tree/v2.4.0) - 2024-06-08
9+
10+
### Added
11+
12+
- 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.
13+
- Added `Cspray\AnnotatedContainer\ContainerFactory\ListOfAsArray` implementation of to allow implementing a collection of services as an array out-of-the-box
14+
15+
### Deprecated
16+
17+
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.
18+
19+
#### Observer Removal Deprecations
20+
21+
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.
22+
23+
- Providing a `Cspray\AnnotatedContainer\Bootstrap\ObserverFactory` during your bootstrapping process will trigger a deprecation.
24+
- Calling `Cspray\AnnotatedContainer\Bootstrap::addObserver` during your bootstrapping process will trigger a deprecation.
25+
- Extending and using the `Cspray\AnnotatedContainer\Bootstrap\ServiceWiringObserver` will trigger a deprecation.
26+
- Providing any observers in your `annotated-container.xml` file will trigger a deprecation.
27+
28+
#### Transition `ActiveProfiles` to `Profiles`
29+
30+
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.
31+
32+
- Calling `Cspray\AnnotatedContainer\Profiles\ActiveProfiles::getProfiles` or `ActiveProfiles::isActive` will trigger a deprecation.
33+
- Using the `Cspray\AnnotatedContainer\Profiles\ActiveProfilesBuilder` will trigger a deprecation.
34+
- Calling `Cspray\AnnotatedContainer\Profiles\CsvActiveProfilesParser` will trigger a deprecation.
35+
36+
#### Removing Logging
37+
38+
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.
39+
40+
- Defining a `<logging></logging>` configuration in `annoatated-container.xml` will trigger a deprecation.
41+
42+
#### Removing Configuration Attribute
43+
44+
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.
45+
46+
- Defining an instance of `Cspray\AnnotatedContainer\Attribute\ConfigurationAttribute` will trigger a deprecation.
47+
48+
#### Removing `cacheDir` configuration
49+
50+
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.
51+
52+
- Defining a `<cacheDir></cacheDir>` configuration in `annotated-container.xml` will trigger a deprecation.
53+
854
## [v2.3.0](https://github.com/cspray/annotated-container/tree/v2.3.0) - 2024-05-22
955

1056
### Changed

composer.lock

+14-11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fixture_src/Fixtures.php

+4
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,8 @@ public static function injectServiceDomainCollection() : InjectServiceDomainColl
202202
return new InjectServiceDomainCollectionFixture();
203203
}
204204

205+
public static function injectServiceCollectionDecorator() : InjectServiceCollectionDecoratorFixture {
206+
return new InjectServiceCollectionDecoratorFixture();
207+
}
208+
205209
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;
4+
5+
use Cspray\AnnotatedContainer\Attribute\Service;
6+
7+
#[Service]
8+
class BarImplementation implements FooInterface {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;
4+
5+
use Cspray\AnnotatedContainer\Attribute\Service;
6+
7+
#[Service]
8+
class BazImplementation implements FooInterface {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;
4+
5+
use Cspray\AnnotatedContainer\Attribute\Inject;
6+
use Cspray\AnnotatedContainer\Attribute\Service;
7+
use Cspray\AnnotatedContainer\ContainerFactory\ListOfAsArray;
8+
9+
#[Service(primary: true)]
10+
class CompositeFooImplementation implements FooInterface {
11+
12+
public function __construct(
13+
#[Inject(new ListOfAsArray(FooInterface::class))]
14+
public readonly array $foos
15+
) {}
16+
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;
4+
5+
use Cspray\AnnotatedContainer\Attribute\Service;
6+
7+
#[Service]
8+
class FooImplementation implements FooInterface {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;
4+
5+
use Cspray\AnnotatedContainer\Attribute\Service;
6+
7+
#[Service]
8+
interface FooInterface {
9+
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator;
4+
5+
use Cspray\AnnotatedContainer\Attribute\Service;
6+
7+
#[Service]
8+
class FooService {
9+
10+
public function __construct(
11+
public readonly FooInterface $foo
12+
) {}
13+
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Cspray\AnnotatedContainerFixture;
4+
5+
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\BarImplementation;
6+
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\BazImplementation;
7+
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\CompositeFooImplementation;
8+
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\FooImplementation;
9+
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\FooInterface;
10+
use Cspray\AnnotatedContainerFixture\InjectServiceCollectionDecorator\FooService;
11+
use Cspray\Typiphy\ObjectType;
12+
use function Cspray\Typiphy\objectType;
13+
14+
class InjectServiceCollectionDecoratorFixture implements Fixture {
15+
16+
public function getPath() : string {
17+
return __DIR__ . '/InjectServiceCollectionDecorator';
18+
}
19+
20+
public function fooService() : ObjectType {
21+
return objectType(FooService::class);
22+
}
23+
24+
public function compositeFoo() : ObjectType {
25+
return objectType(CompositeFooImplementation::class);
26+
}
27+
28+
public function fooInterface() : ObjectType {
29+
return objectType(FooInterface::class);
30+
}
31+
32+
public function fooImplementation() : ObjectType {
33+
return objectType(FooImplementation::class);
34+
}
35+
36+
public function barImplementation() : ObjectType {
37+
return objectType(BarImplementation::class);
38+
}
39+
40+
public function bazImplementation() : ObjectType {
41+
return objectType(BazImplementation::class);
42+
}
43+
}

known-issues.xml

-1
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,6 @@
476476
<code><![CDATA[$created]]></code>
477477
</MissingClosureParamType>
478478
<MissingClosureReturnType>
479-
<code><![CDATA[function() use($state, $container, $value) {]]></code>
480479
<code><![CDATA[static fn(Container $container) => $container->call([$target, $delegateInfo['delegateMethod']])]]></code>
481480
</MissingClosureReturnType>
482481
<MixedArgument>

src/Bootstrap/XmlBootstrappingConfiguration.php

+7
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,13 @@ public function __construct(
173173
$cache = $cacheDirNodes[0]->textContent;
174174
}
175175

176+
if ($cache !== null) {
177+
trigger_error(
178+
'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.',
179+
E_USER_DEPRECATED
180+
);
181+
}
182+
176183
$loggingFileNodes = $xpath->query('/ac:annotatedContainer/ac:logging/ac:file/text()');
177184
$loggingStdoutNodes = $xpath->query('/ac:annotatedContainer/ac:logging/ac:stdout');
178185
$logger = null;

src/ContainerFactory/AurynContainerFactoryState.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function addMethodInject(string $class, string $method, string $param, mi
4747
$key = '+' . $param;
4848
$values = [];
4949
foreach ($this->containerDefinition->getServiceDefinitions() as $serviceDefinition) {
50-
if ($serviceDefinition->isAbstract()) {
50+
if ($serviceDefinition->isAbstract() || $serviceDefinition->getType()->getName() === $class) {
5151
continue;
5252
}
5353

src/ContainerFactory/IlluminateContainerFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,10 +178,10 @@ protected function createAnnotatedContainer(ContainerFactoryState $state, Active
178178

179179
$container->when($service)
180180
->needs($paramIdentifier)
181-
->give(function() use($state, $container, $value) {
181+
->give(function() use($state, $container, $value, $service): mixed {
182182
$values = [];
183183
foreach ($state->containerDefinition->getServiceDefinitions() as $serviceDefinition) {
184-
if ($serviceDefinition->isAbstract()) {
184+
if ($serviceDefinition->isAbstract() || $serviceDefinition->getType()->getName() === $service) {
185185
continue;
186186
}
187187

src/ContainerFactory/PhpDiContainerFactoryState.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public function addMethodInject(string $class, string $method, string $param, mi
4848
if ($value instanceof ServiceCollectorReference) {
4949
$values = [];
5050
foreach ($this->containerDefinition->getServiceDefinitions() as $serviceDefinition) {
51-
if ($serviceDefinition->isAbstract()) {
51+
if ($serviceDefinition->isAbstract() || $serviceDefinition->getType()->getName() === $class) {
5252
continue;
5353
}
5454

test/Unit/ContainerFactoryTestCase.php

+29
Original file line numberDiff line numberDiff line change
@@ -1000,4 +1000,33 @@ public function testCreatingServiceWithInjectServiceDomainCollection() : void {
10001000
$collectionInjector->collection->services
10011001
);
10021002
}
1003+
1004+
public function testCreatingServiceWithInjectServiceCollectionDecorator() : void {
1005+
$container = $this->getContainer(Fixtures::injectServiceCollectionDecorator()->getPath());
1006+
1007+
$fooService = $container->get(Fixtures::injectServiceCollectionDecorator()->fooService()->getName());
1008+
1009+
self::assertInstanceOf(
1010+
Fixtures::injectServiceCollectionDecorator()->fooService()->getName(),
1011+
$fooService
1012+
);
1013+
self::assertInstanceOf(
1014+
Fixtures::injectServiceCollectionDecorator()->compositeFoo()->getName(),
1015+
$fooService->foo
1016+
);
1017+
self::assertCount(3, $fooService->foo->foos);
1018+
$fooClasses = array_map(static fn(object $foo) => $foo::class, $fooService->foo->foos);
1019+
self::assertContains(
1020+
Fixtures::injectServiceCollectionDecorator()->fooImplementation()->getName(),
1021+
$fooClasses
1022+
);
1023+
self::assertContains(
1024+
Fixtures::injectServiceCollectionDecorator()->barImplementation()->getName(),
1025+
$fooClasses
1026+
);
1027+
self::assertContains(
1028+
Fixtures::injectServiceCollectionDecorator()->bazImplementation()->getName(),
1029+
$fooClasses
1030+
);
1031+
}
10031032
}

0 commit comments

Comments
 (0)