Skip to content

Commit 8f61f32

Browse files
committed
Attribute #[AutowiredService] for slimmer config.neon
1 parent 72c2a8d commit 8f61f32

File tree

8 files changed

+108
-5
lines changed

8 files changed

+108
-5
lines changed

bin/phpstan

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use Symfony\Component\Console\Helper\ProgressBar;
1919
$analysisStartTime = microtime(true);
2020

2121
$devOrPharLoader = require_once __DIR__ . '/../vendor/autoload.php';
22+
require_once __DIR__ . '/../vendor/attributes.php';
2223
require_once __DIR__ . '/../preload.php';
2324
$composer = ComposerHelper::getComposerConfig(getcwd());
2425

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"nette/schema": "^1.2.2",
2424
"nette/utils": "^3.2.5",
2525
"nikic/php-parser": "^5.4.0",
26+
"olvlvl/composer-attribute-collector": "^2.0",
2627
"ondram/ci-detector": "^3.4.0",
2728
"ondrejmirtes/better-reflection": "6.57.0.0",
2829
"phpstan/php-8-stubs": "0.4.12",
@@ -72,10 +73,17 @@
7273
"platform-check": false,
7374
"sort-packages": true,
7475
"allow-plugins": {
75-
"cweagans/composer-patches": true
76+
"cweagans/composer-patches": true,
77+
"olvlvl/composer-attribute-collector": true,
78+
"vaimo/composer-patches": true
7679
}
7780
},
7881
"extra": {
82+
"composer-attribute-collector": {
83+
"include": [
84+
"src"
85+
]
86+
},
7987
"composer-exit-on-patch-failure": true,
8088
"patches": {
8189
"composer/ca-bundle": [

composer.lock

Lines changed: 60 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

conf/config.neon

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ parameters:
210210
extensions:
211211
rules: PHPStan\DependencyInjection\RulesExtension
212212
expandRelativePaths: PHPStan\DependencyInjection\ExpandRelativePathExtension
213+
autowiredAttributeServices: PHPStan\DependencyInjection\AutowiredAttributeServicesExtension
213214
conditionalTags: PHPStan\DependencyInjection\ConditionalTagsExtension
214215
parametersSchema: PHPStan\DependencyInjection\ParametersSchemaExtension
215216
validateIgnoredErrors: PHPStan\DependencyInjection\ValidateIgnoredErrorsExtension
@@ -575,9 +576,6 @@ services:
575576
editorUrl: %editorUrl%
576577
usedLevel: %usedLevel%
577578

578-
-
579-
class: PHPStan\Dependency\DependencyResolver
580-
581579
-
582580
class: PHPStan\Dependency\ExportedNodeFetcher
583581
arguments:

src/Dependency/DependencyResolver.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Analyser\Scope;
1212
use PHPStan\Broker\ClassNotFoundException;
1313
use PHPStan\Broker\FunctionNotFoundException;
14+
use PHPStan\DependencyInjection\AutowiredService;
1415
use PHPStan\File\FileHelper;
1516
use PHPStan\Node\ClassPropertyNode;
1617
use PHPStan\Node\InClassMethodNode;
@@ -28,6 +29,7 @@
2829
use function array_merge;
2930
use function count;
3031

32+
#[AutowiredService]
3133
final class DependencyResolver
3234
{
3335

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use Nette\DI\CompilerExtension;
6+
use olvlvl\ComposerAttributeCollector\Attributes;
7+
8+
final class AutowiredAttributeServicesExtension extends CompilerExtension
9+
{
10+
11+
public function loadConfiguration(): void
12+
{
13+
$autowiredServiceClasses = Attributes::findTargetClasses(AutowiredService::class);
14+
$builder = $this->getContainerBuilder();
15+
16+
foreach ($autowiredServiceClasses as $class) {
17+
$builder->addDefinition(null)
18+
->setType($class->name)
19+
->setAutowired();
20+
}
21+
}
22+
23+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\DependencyInjection;
4+
5+
use Attribute;
6+
7+
#[Attribute(flags: Attribute::TARGET_CLASS)]
8+
final class AutowiredService
9+
{
10+
11+
}

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
error_reporting(E_ALL);
66

77
require_once __DIR__ . '/../vendor/autoload.php';
8+
require_once __DIR__ . '/../vendor/attributes.php';
89
require_once __DIR__ . '/PHPStan/Rules/AlwaysFailRule.php';
910
require_once __DIR__ . '/PHPStan/Rules/DummyRule.php';
1011
require_once __DIR__ . '/phpstan-bootstrap.php';

0 commit comments

Comments
 (0)