Skip to content

Commit 7463fed

Browse files
add parent service
1 parent 55ac8dd commit 7463fed

File tree

7 files changed

+75
-10
lines changed

7 files changed

+75
-10
lines changed

DependencyInjection/Compiler/ServiceAliasPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function process(ContainerBuilder $container)
2424

2525
if (true === $this->definitionHasAnnotation($definition, ServiceAlias::class)) {
2626
/** @var ServiceAlias[] $serviceAliases */
27-
$serviceAliases = $this->getDefinitionAnnotation($definition, ServiceAlias::class);
27+
$serviceAliases = $this->getDefinitionAnnotations($definition, ServiceAlias::class);
2828

2929
foreach ($serviceAliases as $alias) {
3030
$container->setAlias($alias->getName(), $serviceId);

DependencyInjection/Compiler/ServiceTagArgumentPass.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function process(ContainerBuilder $container)
2323
}
2424

2525
/** @var ServiceTagArgument[] $arguments */
26-
$arguments = $this->getDefinitionAnnotation($definition, ServiceTagArgument::class);
26+
$arguments = $this->getDefinitionAnnotations($definition, ServiceTagArgument::class);
2727
/** @var ServiceTag[] $serviceTags */
28-
$serviceTags = $this->getDefinitionAnnotation($definition, ServiceTag::class);
28+
$serviceTags = $this->getDefinitionAnnotations($definition, ServiceTag::class);
2929

3030
$attributesByTag = [];
3131

DependencyInjection/Compiler/ServiceTagPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function process(ContainerBuilder $container)
2323
}
2424

2525
/** @var ServiceTag[] $tags */
26-
$tags = $this->getDefinitionAnnotation($definition, ServiceTag::class);
26+
$tags = $this->getDefinitionAnnotations($definition, ServiceTag::class);
2727

2828
foreach ($tags as $tag) {
2929
$definition->addTag($tag->getName(), $tag->getAttributes());

README.MD

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,23 @@ class SomeClass {
241241

242242
}
243243
}
244+
```
245+
246+
#### Example 7: Assign Parent service
247+
248+
If your current service requires a parent service, just annotate your service class with `@ParentService`, the compilerpass
249+
will redefine your service as a ChildDefinition and replace it in the service container
250+
251+
```php
252+
<?php
244253

254+
namespace App;
255+
256+
use SimonMarx\Symfony\Bundles\ServiceAnnotations\Annotation\ParentService;
257+
258+
/**
259+
* @ParentService("App\Service\ParentService")
260+
*/
261+
class ChildService {
262+
}
245263
```

SimaServiceAnnotationsBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use SimonMarx\Symfony\Bundles\ServiceAnnotations\DependencyInjection\Compiler\DependencyInjectionPass;
88
use SimonMarx\Symfony\Bundles\ServiceAnnotations\DependencyInjection\Compiler\NoServicePass;
9+
use SimonMarx\Symfony\Bundles\ServiceAnnotations\DependencyInjection\Compiler\ParentServicePass;
910
use SimonMarx\Symfony\Bundles\ServiceAnnotations\DependencyInjection\Compiler\ServiceAliasPass;
1011
use SimonMarx\Symfony\Bundles\ServiceAnnotations\DependencyInjection\Compiler\ServiceTagArgumentPass;
1112
use SimonMarx\Symfony\Bundles\ServiceAnnotations\DependencyInjection\Compiler\ServiceTagPass;
@@ -23,6 +24,7 @@ public function build(ContainerBuilder $container)
2324
->addCompilerPass(new ServiceAliasPass())
2425
->addCompilerPass(new DependencyInjectionPass())
2526
->addCompilerPass(new ServiceTagArgumentPass())
27+
->addCompilerPass(new ParentServicePass())
2628
->addCompilerPass(new NoServicePass());
2729
}
2830
}

Utils/CompilerPassServiceAnnotationTrait.php

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,37 @@ private function getDefinitionCacheKey(Definition $definition): string
114114
return \md5($definition->getClass());
115115
}
116116

117-
private function getDefinitionAnnotation(Definition $definition, string $annotationClass): array
117+
private function findDefinitionAnnotation(Definition $definition, string $annotationClass): ?object
118118
{
119119
if (false === $this->definitionHasAnnotation($definition, $annotationClass)) {
120-
return [];
120+
return null;
121121
}
122122

123123
$annotations = \array_filter(
124124
$this->getDefinitionAnnotations($definition),
125125
fn($annotation) => $annotation instanceof $annotationClass
126126
);
127127

128-
return \array_values($annotations);
128+
return \array_values($annotations)[0] ?? null;
129129
}
130130

131-
private function getDefinitionAnnotations(Definition $definition): array
131+
private function getDefinitionAnnotations(Definition $definition, ?string $annotationClass = null): array
132132
{
133133
$this->warmupDefinitionAnnotationCache($definition);
134134
$cacheKey = $this->getDefinitionCacheKey($definition);
135135

136-
return $this->annotationCache[$cacheKey] ?? [];
136+
$annotations = $this->annotationCache[$cacheKey] ?? [];
137+
138+
if (null === $annotationClass) {
139+
return $annotations;
140+
}
141+
142+
return \array_values(
143+
\array_filter(
144+
$annotations,
145+
fn($annotation) => $annotation instanceof $annotationClass
146+
)
147+
);
137148
}
138149

139150
private function fullAnnotationScan(Definition $definition): ClassAnnotationScan

Utils/ReflectionCacheTrait.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ trait ReflectionCacheTrait
1212
{
1313
private array $classReflectionCache = [];
1414

15-
private function getClassReflection(string $className): ?ReflectionClass
15+
private function getClassReflection(string $className, bool $recursive = false): ?ReflectionClass
1616
{
1717
$cacheKey = \md5($className);
1818

@@ -24,9 +24,43 @@ private function getClassReflection(string $className): ?ReflectionClass
2424
}
2525
}
2626

27+
if (
28+
true === $recursive
29+
&& false !== $this->classReflectionCache[$cacheKey]->getParentClass()
30+
&& false === \array_key_exists($cacheKey, $this->classReflectionCache)
31+
) {
32+
$this->getClassReflection($this->classReflectionCache[$cacheKey]->getParentClass());
33+
}
34+
2735
return $this->classReflectionCache[$cacheKey] ?? null;
2836
}
2937

38+
/**
39+
* @return ReflectionProperty[]
40+
*/
41+
public function getPropertyReflections(string $className, bool $recursive = false, int $filter = null): array
42+
{
43+
$classReflection = $this->getClassReflection($className, $recursive);
44+
45+
$properties = $classReflection->getProperties();
46+
47+
if (false !== $classReflection->getParentClass() && true === $recursive) {
48+
$parentProperties = $this->getPropertyReflections($classReflection->getParentClass()->getName(), $recursive, $filter);
49+
50+
$filtered = \array_filter($parentProperties, function (ReflectionProperty $parentProperty) use ($properties) {
51+
$childDefinition = \array_filter($properties, function (ReflectionProperty $childProperty) use ($parentProperty) {
52+
return $childProperty->getName() === $parentProperty->getName()
53+
&& $childProperty->getDeclaringClass()->getName() === $parentProperty->getDeclaringClass()->getName();
54+
});
55+
return \count($childDefinition) === 0;
56+
});
57+
58+
$properties = \array_merge($filtered, $properties);
59+
}
60+
61+
return $properties;
62+
}
63+
3064
public function getMethodReflection(string $className, string $methodName): ?ReflectionMethod
3165
{
3266
$rc = $this->getClassReflection($className);

0 commit comments

Comments
 (0)