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