|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This software package is licensed under AGPL or Commercial license. |
|
5
|
|
|
* |
|
6
|
|
|
* @package maslosoft/mangan |
|
7
|
|
|
* @licence AGPL or Commercial |
|
8
|
|
|
* @copyright Copyright (c) Piotr Masełkowski <[email protected]> |
|
9
|
|
|
* @copyright Copyright (c) Maslosoft |
|
10
|
|
|
* @copyright Copyright (c) Others as mentioned in code |
|
11
|
|
|
* @link https://maslosoft.com/mangan/ |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Maslosoft\Mangan\Helpers; |
|
15
|
|
|
|
|
16
|
|
|
use Maslosoft\Addendum\Interfaces\AnnotatedInterface; |
|
17
|
|
|
use Maslosoft\Mangan\Command; |
|
18
|
|
|
use Maslosoft\Mangan\Exceptions\CommandNotFoundException; |
|
19
|
|
|
use Maslosoft\Mangan\Mangan; |
|
20
|
|
|
use Maslosoft\Mangan\Storage\CommandProxyStorage; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* CommandProxy |
|
24
|
|
|
* This evaluates commands only if available |
|
25
|
|
|
* |
|
26
|
|
|
* @author Piotr Maselkowski <pmaselkowski at gmail.com> |
|
27
|
|
|
*/ |
|
28
|
|
|
class CommandProxy extends Command |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Static store of available commands |
|
33
|
|
|
* @var CommandProxyStorage |
|
34
|
|
|
*/ |
|
35
|
|
|
private $available; |
|
36
|
|
|
|
|
37
|
|
|
public function __construct(AnnotatedInterface $model = null) |
|
38
|
|
|
{ |
|
39
|
|
|
parent::__construct($model); |
|
40
|
|
|
if ($model === null) |
|
41
|
|
|
{ |
|
42
|
|
|
$mangan = Mangan::fly(); |
|
43
|
|
|
} |
|
44
|
|
|
else |
|
45
|
|
|
{ |
|
46
|
|
|
$mangan = Mangan::fromModel($model); |
|
47
|
|
|
} |
|
48
|
|
|
$this->available = new CommandProxyStorage($this, $mangan->connectionId); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function isAvailable($command) |
|
52
|
|
|
{ |
|
53
|
|
|
if (!isset($this->available->$command)) |
|
54
|
|
|
{ |
|
55
|
|
|
return true; |
|
56
|
|
|
} |
|
57
|
|
|
return $this->available->$command; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function call($command, $arguments = []): array |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->isAvailable($command)) |
|
63
|
|
|
{ |
|
64
|
|
|
try |
|
65
|
|
|
{ |
|
66
|
|
|
return parent::call($command, $arguments); |
|
67
|
|
|
} |
|
68
|
|
|
catch (CommandNotFoundException $e) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->available->$command = false; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
return []; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|