Skip to content

Commit e7e4968

Browse files
committed
Alias refactoring
1 parent 79e2286 commit e7e4968

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/Container/ConfigProvider.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
namespace CoiSA\ErrorHandler\Container;
1515

16-
use CoiSA\ErrorHandler\Container\Factory\AliasFactory;
1716
use CoiSA\ErrorHandler\ErrorHandler;
1817
use CoiSA\ErrorHandler\Handler\DispatchErrorEventThrowableHandler;
1918
use CoiSA\ErrorHandler\Handler\DispatchThrowableHandler;
@@ -48,10 +47,23 @@ public function __invoke(): array
4847
public function getDependencies(): array
4948
{
5049
return [
50+
'aliases' => $this->getAliases(),
5151
'factories' => $this->getFactories(),
5252
];
5353
}
5454

55+
/**
56+
* @return array
57+
*/
58+
public function getAliases(): array
59+
{
60+
return [
61+
ThrowableHandlerInterface::class => ThrowableHandlerAggregate::class,
62+
ThrowableResponseFactoryInterface::class => ThrowableResponseFactory::class,
63+
ThrowableStreamFactoryInterface::class => ThrowableStreamFactory::class,
64+
];
65+
}
66+
5567
/**
5668
* @return array
5769
*/
@@ -63,11 +75,8 @@ public function getFactories(): array
6375
DispatchErrorEventThrowableHandler::class => Factory\DispatchErrorEventThrowableHandlerFactory::class,
6476
DispatchThrowableHandler::class => Factory\DispatchThrowableHandlerFactory::class,
6577
ThrowableHandlerAggregate::class => Factory\ThrowableHandlerAggregateFactory::class,
66-
ThrowableHandlerInterface::class => new AliasFactory(ThrowableHandlerAggregate::class),
6778
ThrowableResponseFactory::class => Factory\ThrowableResponseFactoryFactory::class,
68-
ThrowableResponseFactoryInterface::class => new AliasFactory(ThrowableResponseFactory::class),
6979
ThrowableStreamFactory::class => Factory\ThrowableStreamFactoryFactory::class,
70-
ThrowableStreamFactoryInterface::class => new AliasFactory(ThrowableStreamFactory::class),
7180
];
7281
}
7382
}

src/Container/ErrorHandlerContainer.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ final class ErrorHandlerContainer implements ContainerInterface
3232
*/
3333
private $factories;
3434

35+
/**
36+
* @var string[]
37+
*/
38+
private $aliases;
39+
3540
/**
3641
* @var object[]
3742
*/
@@ -44,7 +49,11 @@ final class ErrorHandlerContainer implements ContainerInterface
4449
*/
4550
public function __construct(ContainerInterface $container = null)
4651
{
47-
$this->factories = (new ConfigProvider())->getFactories();
52+
$configProvider = new ConfigProvider();
53+
54+
$this->factories = $configProvider->getFactories();
55+
$this->aliases = $configProvider->getAliases();
56+
4857
$this->container = $container;
4958
}
5059

@@ -56,6 +65,7 @@ public function __construct(ContainerInterface $container = null)
5665
public function has($id)
5766
{
5867
return ($this->container && $this->container->has($id))
68+
|| \array_key_exists($id, $this->aliases)
5969
|| \array_key_exists($id, $this->factories);
6070
}
6171

@@ -99,8 +109,10 @@ private function getFactory(string $id): callable
99109
throw new Exception\NotFoundException(\sprintf('Factory for class %s was not found', $id));
100110
}
101111

102-
$factory = $this->factories[$id];
112+
if (isset($this->aliases[$id])) {
113+
return new Factory\AliasFactory($this->aliases[$id]);
114+
}
103115

104-
return \is_callable($factory) ? $factory : new $factory();
116+
return new $this->factories[$id]();
105117
}
106118
}

0 commit comments

Comments
 (0)