Skip to content

Commit 40bfdb3

Browse files
committed
fix(intl): resolve optional event bus safely
1 parent fd2a739 commit 40bfdb3

5 files changed

Lines changed: 43 additions & 2 deletions

File tree

packages/container/src/Container.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public function config(object $config): self;
2727
*/
2828
public function get(string $className, string|UnitEnum|null $tag = null, mixed ...$params): mixed;
2929

30+
/**
31+
* @template TClassName of object
32+
* @param class-string<TClassName> $className
33+
* @return TClassName|null
34+
*/
35+
public function getIfExists(string $className, string|UnitEnum|null $tag = null, mixed ...$params): ?object;
36+
3037
public function has(string $className, string|UnitEnum|null $tag = null): bool;
3138

3239
public function invoke(ClassReflector|MethodReflector|FunctionReflector|callable|string $method, mixed ...$params): mixed;

packages/container/src/GenericContainer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,22 @@ public function get(string $className, string|UnitEnum|null $tag = null, mixed .
216216
return $dependency;
217217
}
218218

219+
/**
220+
* @template TClassName of object
221+
* @param class-string<TClassName> $className
222+
* @return TClassName|null
223+
* @throws CircularDependencyEncountered
224+
* @throws TaggedDependencyCouldNotBeResolved
225+
*/
226+
public function getIfExists(string $className, string|UnitEnum|null $tag = null, mixed ...$params): ?object
227+
{
228+
if (! class_exists($className) && ! interface_exists($className)) {
229+
return null;
230+
}
231+
232+
return $this->get($className, $tag, ...$params);
233+
}
234+
219235
public function invoke(ClassReflector|MethodReflector|FunctionReflector|callable|string $method, mixed ...$params): mixed
220236
{
221237
if ($method instanceof ClassReflector) {

packages/container/tests/ContainerTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,23 @@ public function test_get_with_definition(): void
105105
$this->assertEquals('test', $c->prop);
106106
}
107107

108+
public function test_get_if_exists_returns_null_when_type_does_not_exist(): void
109+
{
110+
$container = new GenericContainer();
111+
112+
$this->assertNull($container->getIfExists('Tempest\\Container\\Tests\\Fixtures\\DoesNotExist'));
113+
}
114+
115+
public function test_get_if_exists_uses_normal_get_flow_when_type_exists(): void
116+
{
117+
$container = new GenericContainer();
118+
119+
$object = $container->getIfExists(ContainerObjectB::class);
120+
121+
$this->assertInstanceOf(ContainerObjectB::class, $object);
122+
$this->assertInstanceOf(ContainerObjectA::class, $object->a);
123+
}
124+
108125
public function test_get_with_initializer(): void
109126
{
110127
$container = new GenericContainer()->setInitializers([

packages/intl/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
},
1515
"suggest": {
1616
"tempest/datetime": "In order to use the `datetime` function",
17-
"tempest/icon": "In order to use the `icon` function"
17+
"tempest/icon": "In order to use the `icon` function",
18+
"tempest/event-bus": "Enables dispatching translation miss and translation failure events."
1819
},
1920
"autoload": {
2021
"files": [

packages/intl/src/TranslatorInitializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public function initialize(Container $container): Translator
1818
config: $container->get(IntlConfig::class),
1919
catalog: $container->get(Catalog::class),
2020
formatter: $container->get(MessageFormatter::class),
21-
eventBus: $container->get(EventBus::class),
21+
eventBus: $container->getIfExists(EventBus::class),
2222
);
2323
}
2424
}

0 commit comments

Comments
 (0)