Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
85.71% |
6 / 7 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| StrategyRegistry | |
85.71% |
6 / 7 |
|
80.00% |
4 / 5 |
7.14 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
3 | |||
| register | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| list | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Registry for action strategies; allows dynamic registration (plugin extension). |
| 5 | * |
| 6 | * This Source Code Form is subject to the terms of the Mozilla Public License, |
| 7 | * v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | * |
| 10 | * @package phpMyFAQ |
| 11 | * @author Thorsten Rinne <thorsten@phpmyfaq.de> |
| 12 | * @copyright 2025 phpMyFAQ Team |
| 13 | * @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 |
| 14 | * @link https://www.phpmyfaq.de |
| 15 | * @since 2025-11-08 |
| 16 | */ |
| 17 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Link\Strategy; |
| 21 | |
| 22 | /** |
| 23 | * Registry für Action-Strategien; erlaubt dynamisches Registrieren (Plugin-Erweiterung). |
| 24 | */ |
| 25 | final class StrategyRegistry |
| 26 | { |
| 27 | /** @var array<string, StrategyInterface> */ |
| 28 | private array $strategies = []; |
| 29 | |
| 30 | /** |
| 31 | * @param array<string, StrategyInterface>|null $initial |
| 32 | */ |
| 33 | public function __construct(?array $initial = null) |
| 34 | { |
| 35 | if ($initial) { |
| 36 | foreach ($initial as $action => $strategy) { |
| 37 | $this->register($action, $strategy); |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | public function register(string $action, StrategyInterface $strategy): void |
| 43 | { |
| 44 | $this->strategies[$action] = $strategy; |
| 45 | } |
| 46 | |
| 47 | public function has(string $action): bool |
| 48 | { |
| 49 | return array_key_exists($action, $this->strategies); |
| 50 | } |
| 51 | |
| 52 | public function get(string $action): ?StrategyInterface |
| 53 | { |
| 54 | return $this->strategies[$action] ?? null; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Liefert alle registrierten Actions. |
| 59 | * @return string[] |
| 60 | */ |
| 61 | public function list(): array |
| 62 | { |
| 63 | return array_keys($this->strategies); |
| 64 | } |
| 65 | } |