1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the PierstovalCharacterManagerBundle package. |
7
|
|
|
* |
8
|
|
|
* (c) Alexandre Rock Ancelet <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Pierstoval\Bundle\CharacterManagerBundle\Registry; |
15
|
|
|
|
16
|
|
|
use Pierstoval\Bundle\CharacterManagerBundle\Action\StepActionInterface; |
17
|
|
|
|
18
|
|
|
class ActionsRegistry implements ActionsRegistryInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var StepActionInterface[][] |
22
|
|
|
*/ |
23
|
|
|
private $actions = []; |
24
|
|
|
|
25
|
|
|
public function addStepAction(string $manager, string $stepName, $action): void |
26
|
|
|
{ |
27
|
|
|
$this->actions[$manager][$stepName] = $action; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function getAction(string $stepName, string $manager = null): StepActionInterface |
31
|
|
|
{ |
32
|
|
|
if (!$this->actions) { |
|
|
|
|
33
|
|
|
throw new \RuntimeException('No actions in the registry.'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (null === $manager) { |
37
|
|
|
$manager = \array_keys($this->actions)[0]; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if (!isset($this->actions[$manager])) { |
41
|
|
|
throw new \InvalidArgumentException(\sprintf( |
42
|
|
|
'Manager "%s" does not exist.', |
43
|
|
|
$manager |
44
|
|
|
)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!isset($this->actions[$manager][$stepName])) { |
48
|
|
|
throw new \InvalidArgumentException(\sprintf( |
49
|
|
|
'Step "%s" not found in manager "%s".', |
50
|
|
|
$stepName, |
51
|
|
|
$manager |
52
|
|
|
)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$action = $this->actions[$manager][$stepName]; |
56
|
|
|
|
57
|
|
|
if ($action instanceof \Closure) { |
58
|
|
|
// Lazy loading |
59
|
|
|
$action = $this->actions[$manager][$stepName](); |
60
|
|
View Code Duplication |
if (!$action instanceof StepActionInterface) { |
61
|
|
|
throw new \RuntimeException(\sprintf( |
62
|
|
|
"Lazy-loaded action \"%s\" for character manager \"%s\" must be resolved to an instance of \"%s\".\n\"%s\" given.", |
63
|
|
|
$stepName, |
64
|
|
|
$manager, |
65
|
|
|
StepActionInterface::class, |
66
|
|
|
\is_object($action) ? \get_class($action) : \gettype($action) |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
$this->actions[$manager][$stepName] = $action; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $action; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.