Skip to content

Commit 12bad70

Browse files
committed
AutowiredService - support for "factory"
1 parent 94e4b6e commit 12bad70

File tree

9 files changed

+24
-25
lines changed

9 files changed

+24
-25
lines changed

conf/config.neon

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,6 @@ services:
244244
-
245245
class: PHPStan\Rules\Properties\UninitializedPropertyRule
246246

247-
-
248-
class: PHPStan\Php\PhpVersion
249-
factory: @PHPStan\Php\PhpVersionFactory::create
250-
251-
-
252-
class: PHPStan\Php\PhpVersionFactory
253-
factory: @PHPStan\Php\PhpVersionFactoryFactory::create
254-
255-
-
256-
class: PHPStan\Analyser\ConstantResolver
257-
factory: @PHPStan\Analyser\ConstantResolverFactory::create()
258-
259247
-
260248
implement: PHPStan\Analyser\ResultCache\ResultCacheManagerFactory
261249
arguments:
@@ -273,10 +261,6 @@ services:
273261
parametersNotInvalidatingCache: %parametersNotInvalidatingCache%
274262
skipResultCacheIfOlderThanDays: %resultCacheSkipIfOlderThanDays%
275263

276-
-
277-
class: PHPStan\Collectors\Registry
278-
factory: @PHPStan\Collectors\RegistryFactory::create
279-
280264
-
281265
implement: PHPStan\File\FileExcluderRawFactory
282266

@@ -296,14 +280,6 @@ services:
296280
arguments:
297281
parser: @defaultAnalysisParser
298282

299-
-
300-
class: PHPStan\Reflection\SignatureMap\SignatureMapProvider
301-
factory: @PHPStan\Reflection\SignatureMap\SignatureMapProviderFactory::create()
302-
303-
typeSpecifier:
304-
class: PHPStan\Analyser\TypeSpecifier
305-
factory: @typeSpecifierFactory::create
306-
307283
simpleRelativePathHelper:
308284
class: PHPStan\File\RelativePathHelper
309285
factory: PHPStan\File\SimpleRelativePathHelper

src/Analyser/ConstantResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Analyser;
44

55
use PhpParser\Node\Name;
6+
use PHPStan\DependencyInjection\AutowiredService;
67
use PHPStan\Php\ComposerPhpVersionFactory;
78
use PHPStan\Php\PhpVersion;
89
use PHPStan\Reflection\NamespaceAnswerer;
@@ -31,6 +32,7 @@
3132
use const NAN;
3233
use const PHP_INT_SIZE;
3334

35+
#[AutowiredService(factory: '@PHPStan\Analyser\ConstantResolverFactory::create')]
3436
final class ConstantResolver
3537
{
3638

src/Analyser/TypeSpecifier.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpParser\Node\Expr\StaticCall;
2020
use PhpParser\Node\Expr\StaticPropertyFetch;
2121
use PhpParser\Node\Name;
22+
use PHPStan\DependencyInjection\AutowiredService;
2223
use PHPStan\Node\Expr\AlwaysRememberedExpr;
2324
use PHPStan\Node\IssetExpr;
2425
use PHPStan\Node\Printer\ExprPrinter;
@@ -86,6 +87,7 @@
8687
use function substr;
8788
use const COUNT_NORMAL;
8889

90+
#[AutowiredService(name: 'typeSpecifier', factory: '@typeSpecifierFactory::create')]
8991
final class TypeSpecifier
9092
{
9193

src/Collectors/Registry.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace PHPStan\Collectors;
44

55
use PhpParser\Node;
6+
use PHPStan\DependencyInjection\AutowiredService;
67
use function class_implements;
78
use function class_parents;
89

10+
#[AutowiredService(factory: '@PHPStan\Collectors\RegistryFactory::create')]
911
final class Registry
1012
{
1113

src/DependencyInjection/AutowiredAttributeServicesExtension.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nette\DI\CompilerExtension;
66
use Nette\DI\Definitions\Reference;
77
use Nette\DI\Definitions\ServiceDefinition;
8+
use Nette\DI\Definitions\Statement;
89
use Nette\DI\Helpers;
910
use Nette\Schema\Expect;
1011
use Nette\Schema\Schema;
@@ -15,6 +16,7 @@
1516
use PHPStan\Rules\LazyRegistry;
1617
use ReflectionClass;
1718
use stdClass;
19+
use function explode;
1820
use function strtolower;
1921
use function substr;
2022

@@ -43,6 +45,11 @@ public function loadConfiguration(): void
4345
->setType($class->name)
4446
->setAutowired($attribute->as);
4547

48+
if ($attribute->factory !== null) {
49+
[$ref, $method] = explode('::', $attribute->factory);
50+
$definition->setFactory(new Statement([new Reference(substr($ref, 1)), $method]));
51+
}
52+
4653
$this->processParameters($class->name, $definition, $autowiredParameters);
4754

4855
foreach (ValidateServiceTagsExtension::INTERFACE_TAG_MAPPING as $interface => $tag) {

src/DependencyInjection/AutowiredService.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ final class AutowiredService
1919
/**
2020
* @param true|list<class-string>|class-string $as
2121
*/
22-
public function __construct(public ?string $name = null, public bool|array|string $as = true)
22+
public function __construct(
23+
public ?string $name = null,
24+
public ?string $factory = null,
25+
public bool|array|string $as = true,
26+
)
2327
{
2428
}
2529

src/Php/PhpVersion.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace PHPStan\Php;
44

5+
use PHPStan\DependencyInjection\AutowiredService;
56
use function floor;
67

78
/**
89
* @api
910
*/
11+
#[AutowiredService(factory: '@PHPStan\Php\PhpVersionFactory::create')]
1012
final class PhpVersion
1113
{
1214

src/Php/PhpVersionFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace PHPStan\Php;
44

5+
use PHPStan\DependencyInjection\AutowiredService;
56
use function explode;
67
use function max;
78
use function min;
89
use const PHP_VERSION_ID;
910

11+
#[AutowiredService(factory: '@PHPStan\Php\PhpVersionFactoryFactory::create')]
1012
final class PhpVersionFactory
1113
{
1214

src/Reflection/SignatureMap/SignatureMapProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
namespace PHPStan\Reflection\SignatureMap;
44

55
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionMethod;
6+
use PHPStan\DependencyInjection\AutowiredService;
67
use PHPStan\Type\Type;
78
use ReflectionFunctionAbstract;
89

10+
#[AutowiredService(factory: '@PHPStan\Reflection\SignatureMap\SignatureMapProviderFactory::create')]
911
interface SignatureMapProvider
1012
{
1113

0 commit comments

Comments
 (0)