Skip to content

Commit 5a90197

Browse files
authored
DI compiler extension replaced by DynamicThrowTypeServiceFactory (#80)
1 parent 7608f8f commit 5a90197

File tree

3 files changed

+50
-48
lines changed

3 files changed

+50
-48
lines changed

extension.neon

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@ parameters:
88
methodThrowTypeDeclarations: []
99
functionThrowTypeDeclarations: []
1010

11-
extensions:
12-
exceptionRules: Pepakriz\PHPStanExceptionRules\DI\ExceptionRulesExtension
13-
1411
services:
12+
-
13+
class: Pepakriz\PHPStanExceptionRules\DynamicThrowTypeServiceFactory
14+
15+
-
16+
class: Pepakriz\PHPStanExceptionRules\DynamicThrowTypeService
17+
factory: @Pepakriz\PHPStanExceptionRules\DynamicThrowTypeServiceFactory::create
18+
1519
-
1620
class: Pepakriz\PHPStanExceptionRules\ThrowsAnnotationReader
1721
-

src/DI/ExceptionRulesExtension.php

Lines changed: 0 additions & 45 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Pepakriz\PHPStanExceptionRules;
4+
5+
use Nette\DI\Container;
6+
use function array_keys;
7+
use function array_map;
8+
9+
class DynamicThrowTypeServiceFactory
10+
{
11+
12+
private const TAG_DYNAMIC_METHOD_THROW_TYPE = 'exceptionRules.dynamicMethodThrowTypeExtension';
13+
private const TAG_DYNAMIC_STATIC_METHOD_THROW_TYPE = 'exceptionRules.dynamicStaticMethodThrowTypeExtension';
14+
private const TAG_DYNAMIC_CONSTRUCTOR_THROW_TYPE = 'exceptionRules.dynamicConstructorThrowTypeExtension';
15+
private const TAG_DYNAMIC_FUNCTION_THROW_TYPE = 'exceptionRules.dynamicFunctionThrowTypeExtension';
16+
17+
/**
18+
* @var Container
19+
*/
20+
private $container;
21+
22+
public function __construct(Container $container)
23+
{
24+
$this->container = $container;
25+
}
26+
27+
public function create(): DynamicThrowTypeService
28+
{
29+
$tagToService = function (array $tags) {
30+
return array_map(function (string $serviceName) {
31+
return $this->container->getService($serviceName);
32+
}, array_keys($tags));
33+
};
34+
35+
return new DynamicThrowTypeService(
36+
$tagToService($this->container->findByTag(self::TAG_DYNAMIC_METHOD_THROW_TYPE)),
37+
$tagToService($this->container->findByTag(self::TAG_DYNAMIC_STATIC_METHOD_THROW_TYPE)),
38+
$tagToService($this->container->findByTag(self::TAG_DYNAMIC_CONSTRUCTOR_THROW_TYPE)),
39+
$tagToService($this->container->findByTag(self::TAG_DYNAMIC_FUNCTION_THROW_TYPE))
40+
);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)