fractalzombie /
frzb-dependency-injection
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /** |
||
| 6 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
| 7 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 8 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
||
| 9 | * |
||
| 10 | * Copyright (c) 2024 Mykhailo Shtanko [email protected] |
||
| 11 | * |
||
| 12 | * For the full copyright and license information, please view the LICENSE.MD |
||
| 13 | * file that was distributed with this source code. |
||
| 14 | */ |
||
| 15 | |||
| 16 | namespace FRZB\Component\DependencyInjection\Attribute; |
||
| 17 | |||
| 18 | use FRZB\Component\DependencyInjection\Enum\AliasType; |
||
| 19 | use JetBrains\PhpStorm\Immutable; |
||
| 20 | |||
| 21 | #[Immutable] |
||
| 22 | #[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)] |
||
| 23 | final class AsAlias |
||
| 24 | { |
||
| 25 | public readonly AliasType $aliasType; |
||
| 26 | |||
| 27 | public function __construct( |
||
| 28 | public readonly string $service, |
||
| 29 | public readonly bool $isPublic = true, |
||
| 30 | public readonly ?string $aliasForArgument = null, |
||
| 31 | ) { |
||
| 32 | $this->aliasType = match (true) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 33 | !empty($this->aliasForArgument) => AliasType::WithArgumentName, |
||
| 34 | empty($this->aliasForArgument) => AliasType::WithoutArgumentName, |
||
| 35 | default => AliasType::LogicException, |
||
| 36 | }; |
||
| 37 | } |
||
| 38 | |||
| 39 | public function getServiceAlias(): string |
||
| 40 | { |
||
| 41 | return match ($this->aliasType) { |
||
| 42 | AliasType::WithArgumentName => sprintf('%s %s', $this->service, $this->aliasForArgument), |
||
| 43 | AliasType::WithoutArgumentName, AliasType::LogicException => $this->service, |
||
| 44 | }; |
||
| 45 | } |
||
| 46 | } |
||
| 47 |