Skip to content

Commit 79487a3

Browse files
authored
Merge pull request #18 from Jeroen-G/tagged
allow tagged classes in configuration
2 parents 19d2b03 + 424ed4d commit 79487a3

File tree

12 files changed

+107
-6
lines changed

12 files changed

+107
-6
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/ConfigurationType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ final class ConfigurationType
1010

1111
public const SERVICE = 'service';
1212

13+
public const TAGGED = 'tagged';
14+
1315
public const UNKNOWN = 'unknown';
1416
}

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: 14 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,13 @@ private function parse(string $variable, string $value): void
5461
return;
5562
}
5663

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

tests/Support/Subject/Contracts/GoodeveningInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use JeroenG\Autowire\Attribute\Tag;
88

9-
#[Tag('evening')]
9+
#[Tag('#evening')]
1010
interface GoodeveningInterface
1111
{
1212
public function goodevening(): string;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace JeroenG\Autowire\Tests\Support\Subject\Domain\Greeting;
6+
7+
use JeroenG\Autowire\Attribute\Configure;
8+
9+
#[Configure(['$greeting' => '#evening'])]
10+
class TagGreeting
11+
{
12+
private $greeting;
13+
14+
public function __construct($greeting)
15+
{
16+
$this->greeting = $greeting;
17+
}
18+
19+
public function getGreeting()
20+
{
21+
return $this->greeting;
22+
}
23+
}

0 commit comments

Comments
 (0)