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 evalueates 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 = null; |
36
|
|
|
|
37
|
1 |
|
public function __construct(AnnotatedInterface $model = null) |
38
|
|
|
{ |
39
|
1 |
|
parent::__construct($model); |
40
|
1 |
|
if (empty($model)) |
41
|
|
|
{ |
42
|
|
|
$mangan = Mangan::fly(); |
43
|
|
|
} |
44
|
|
|
else |
45
|
|
|
{ |
46
|
1 |
|
$mangan = Mangan::fromModel($model); |
47
|
|
|
} |
48
|
1 |
|
$this->available = new CommandProxyStorage($this, $mangan->connectionId); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
1 |
|
public function isAvailable($command) |
52
|
|
|
{ |
53
|
1 |
|
if (!isset($this->available->$command)) |
54
|
|
|
{ |
55
|
1 |
|
return true; |
56
|
|
|
} |
57
|
1 |
|
return $this->available->$command; |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function call($command, $arguments = []) |
61
|
|
|
{ |
62
|
1 |
|
if ($this->isAvailable($command)) |
63
|
|
|
{ |
64
|
|
|
try |
65
|
|
|
{ |
66
|
1 |
|
return parent::call($command, $arguments); |
67
|
|
|
} |
68
|
1 |
|
catch (CommandNotFoundException $e) |
69
|
|
|
{ |
70
|
1 |
|
$this->available->$command = false; |
71
|
|
|
} |
72
|
|
|
} |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
} |
76
|
|
|
|