1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Application\Commands; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Common\Reflection\ClassIsTrait; |
22
|
|
|
use Limoncello\Contracts\Commands\CommandInterface; |
23
|
|
|
use Limoncello\Contracts\Commands\CommandStorageInterface; |
24
|
|
|
use function array_key_exists; |
25
|
|
|
use function assert; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @package Limoncello\Application |
29
|
|
|
*/ |
30
|
|
|
class CommandStorage implements CommandStorageInterface |
31
|
|
|
{ |
32
|
|
|
use ClassIsTrait; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string[] |
36
|
|
|
*/ |
37
|
|
|
private $commandClasses = []; |
38
|
1 |
|
|
39
|
|
|
/** |
40
|
1 |
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
1 |
|
public function has(string $class): bool |
43
|
|
|
{ |
44
|
|
|
assert($this->isCommand($class)); |
45
|
|
|
|
46
|
|
|
return array_key_exists($class, $this->commandClasses); |
47
|
|
|
} |
48
|
1 |
|
|
49
|
|
|
/** |
50
|
1 |
|
* @inheritdoc |
51
|
|
|
*/ |
52
|
1 |
|
public function add(string $class): CommandStorageInterface |
53
|
|
|
{ |
54
|
1 |
|
assert($this->isCommand($class)); |
55
|
|
|
|
56
|
|
|
$this->commandClasses[$class] = $class; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
1 |
|
|
61
|
|
|
/** |
62
|
1 |
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
|
|
public function getAll(): array |
65
|
|
|
{ |
66
|
|
|
return $this->commandClasses; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
1 |
|
* @param string $class |
71
|
|
|
* |
72
|
1 |
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
private function isCommand(string $class): bool |
75
|
|
|
{ |
76
|
|
|
return $this->classImplements($class, CommandInterface::class); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|