Skip to content

Commit 79ced2c

Browse files
committed
allow tagged classes in configuration
1 parent 19d2b03 commit 79ced2c

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## Unreleased
88

9+
## [2.0.0]
10+
11+
### Added
12+
- Configurations can now include tags
13+
914
## [1.9.0]
1015

1116
### Added

readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ The notations of config and service definitions is the same as used in Symfony.
146146
// Will inject an instance of the Message class
147147
#[Configure(['$message' => '@App\Domain\Message'])]
148148

149+
// When you want tagged classes
150+
#[Configure(['$messages' => '#messages'])]
151+
149152
// When you have multiple constructor arguments
150153
#[Configure(['$message' => '%app.message%', '$logger' => '@Psr\Log\LoggerInterface'])]
151154
```

src/Attribute/Configure.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ final class Configure implements ConfigureInterface
1515

1616
private array $definitions = [];
1717

18+
private array $tags = [];
19+
1820
public function __construct(array $cables)
1921
{
2022
foreach ($cables as $variable => $value) {
@@ -37,6 +39,11 @@ public function getDefinitions(): array
3739
return $this->definitions;
3840
}
3941

42+
public function getTags(): array
43+
{
44+
return $this->tags;
45+
}
46+
4047
private function parse(string $variable, string $value): void
4148
{
4249
$hasConfig = preg_match("/%(.*)%/", $value, $config);
@@ -53,6 +60,13 @@ private function parse(string $variable, string $value): void
5360
return;
5461
}
5562

63+
$hasTag = preg_match("/#(.*)/", $value, $service);
64+
65+
if ($hasTag === 1) {
66+
$this->tags[$variable] = $service[1];
67+
return;
68+
}
69+
5670
$this->definitions[$variable] = $value;
5771
}
5872
}

src/Attribute/ConfigureInterface.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,9 @@ public function getServices(): array;
2020
* @return array<non-empty-string, scalar>
2121
*/
2222
public function getDefinitions(): array;
23+
24+
/**
25+
* @return array<non-empty-string, scalar>
26+
*/
27+
public function getTags(): array;
2328
}

src/AutowireServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace JeroenG\Autowire;
66

7+
use App\Console\Commands\Bot\SendEndOfWeekUpdate;
8+
use App\Domain\Bot\AlarmBellInterface;
79
use Illuminate\Support\Facades\App;
810
use Illuminate\Support\Facades\Event;
911
use Illuminate\Support\Facades\File;
@@ -131,6 +133,9 @@ private function define(string $implementation, ConfigurationValue $definition):
131133
$give = $this->app->make($definition->give);
132134
$this->app->when($implementation)->needs($definition->need)->give($give);
133135
break;
136+
case ConfigurationType::TAGGED:
137+
$this->app->when($implementation)->needs($definition->need)->giveTagged($definition->give);
138+
break;
134139
case ConfigurationType::UNKNOWN:
135140
default:
136141
$this->app->when($implementation)->needs($definition->need)->give($definition->give);

src/Electrician.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ public function configure(string $implementation): Configuration
7070
$configurations[] = new ConfigurationValue($need, $give, ConfigurationType::SERVICE);
7171
}
7272

73+
foreach ($instance->getTags() as $need => $give) {
74+
$configurations[] = new ConfigurationValue($need, $give, ConfigurationType::TAGGED);
75+
}
76+
7377
foreach ($instance->getDefinitions() as $need => $give) {
7478
$configurations[] = new ConfigurationValue($need, $give, ConfigurationType::UNKNOWN);
7579
}

tests/Support/Attributes/CustomConfigure.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class CustomConfigure implements ConfigureInterface
1616

1717
private array $definitions = [];
1818

19+
private array $tags = [];
20+
1921
public function __construct(array $cables)
2022
{
2123
foreach ($cables as $variable => $value) {
@@ -38,6 +40,11 @@ public function getDefinitions(): array
3840
return $this->definitions;
3941
}
4042

43+
public function getTags(): array
44+
{
45+
return $this->tags;
46+
}
47+
4148
private function parse(string $variable, string $value): void
4249
{
4350
$hasConfig = preg_match("/%(.*)%/", $value, $config);
@@ -54,6 +61,12 @@ private function parse(string $variable, string $value): void
5461
return;
5562
}
5663

64+
$hasTag = preg_match("/#(.*)/", $value, $service);
65+
if ($hasTag === 1) {
66+
$this->tags[$variable] = $service[1];
67+
return;
68+
}
69+
5770
$this->definitions[$variable] = $value;
5871
}
5972
}

0 commit comments

Comments
 (0)