Skip to content

Commit 1987488

Browse files
authored
Add service definition to ServiceWiringObserver (#257)
1 parent 45524c6 commit 1987488

File tree

5 files changed

+51
-6
lines changed

5 files changed

+51
-6
lines changed

known-issues.xml

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<files psalm-version="4.27.0@faf106e717c37b8c81721845dba9de3d8deed8ff">
3+
<file src="src/Bootstrap/ServiceWiringObserver.php">
4+
<LessSpecificReturnStatement occurrences="1">
5+
<code>$services</code>
6+
</LessSpecificReturnStatement>
7+
<MoreSpecificReturnType occurrences="1">
8+
<code>array</code>
9+
</MoreSpecificReturnType>
10+
</file>
311
<file src="src/Cli/Command/BuildCommand.php">
412
<MixedArrayAccess occurrences="1">
513
<code>$composer['extra']['annotatedContainer']['configFile']</code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Cspray\AnnotatedContainer\Bootstrap;
4+
5+
use Cspray\AnnotatedContainer\Definition\ServiceDefinition;
6+
7+
/**
8+
* @template Service
9+
*/
10+
interface ServiceFromServiceDefinition {
11+
12+
/**
13+
* @return Service
14+
*/
15+
public function getService() : object;
16+
17+
public function getDefinition() : ServiceDefinition;
18+
19+
}

src/Bootstrap/ServiceGatherer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace Cspray\AnnotatedContainer\Bootstrap;
44

55
/**
6-
* @template Service
6+
* @template T
77
*/
88
interface ServiceGatherer {
99

1010
/**
11-
* @param class-string<Service> $type
12-
* @return Service[]
11+
* @param class-string<T> $type
12+
* @return ServiceFromServiceDefinition<T>[]
1313
*/
1414
public function getServicesForType(string $type) : array;
1515

src/Bootstrap/ServiceWiringObserver.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Cspray\AnnotatedContainer\AnnotatedContainer;
66
use Cspray\AnnotatedContainer\Definition\ContainerDefinition;
7+
use Cspray\AnnotatedContainer\Definition\ServiceDefinition;
78

89
abstract class ServiceWiringObserver implements Observer {
910

@@ -40,7 +41,21 @@ public function getServicesForType(string $type) : array {
4041
if (is_a($serviceType, $type, true)) {
4142
$service = $this->container->get($serviceType);
4243
assert($service instanceof $type);
43-
$services[] = $service;
44+
$services[] = new class($service, $serviceDefinition) implements ServiceFromServiceDefinition {
45+
46+
public function __construct(
47+
private readonly object $service,
48+
private readonly ServiceDefinition $definition
49+
) {}
50+
51+
public function getService() : object {
52+
return $this->service;
53+
}
54+
55+
public function getDefinition() : ServiceDefinition {
56+
return $this->definition;
57+
}
58+
};
4459
}
4560
}
4661

test/BootstrapTest.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Cspray\AnnotatedContainer\Bootstrap\Bootstrap;
66
use Cspray\AnnotatedContainer\Bootstrap\ContainerDefinitionBuilderContextConsumerFactory;
77
use Cspray\AnnotatedContainer\Bootstrap\ParameterStoreFactory;
8+
use Cspray\AnnotatedContainer\Bootstrap\ServiceFromServiceDefinition;
89
use Cspray\AnnotatedContainer\Bootstrap\ServiceGatherer;
910
use Cspray\AnnotatedContainer\Bootstrap\ServiceWiringObserver;
1011
use Cspray\AnnotatedContainer\Helper\FixtureBootstrappingDirectoryResolver;
@@ -506,14 +507,16 @@ protected function wireServices(AnnotatedContainer $container, ServiceGatherer $
506507

507508
$actual = $observer->getServices();
508509

509-
usort($actual, fn($a, $b) => $a::class <=> $b::class);
510+
$actualServices = array_map(fn(ServiceFromServiceDefinition $fromServiceDefinition) => $fromServiceDefinition->getService(), $actual);
511+
512+
usort($actualServices, fn($a, $b) => $a::class <=> $b::class);
510513

511514
self::assertSame($container, $observer->getAnnotatedContainer());
512515
self::assertSame([
513516
$container->get(Fixtures::ambiguousAliasedServices()->barImplementation()->getName()),
514517
$container->get(Fixtures::ambiguousAliasedServices()->bazImplementation()->getName()),
515518
$container->get(Fixtures::ambiguousAliasedServices()->quxImplementation()->getName()),
516-
], $actual);
519+
], $actualServices);
517520
}
518521

519522
}

0 commit comments

Comments
 (0)