|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Robin Appelman <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OC\Command; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\Command\ICommand; |
|
25
|
|
|
use SuperClosure\Serializer; |
|
26
|
|
|
|
|
27
|
|
|
class CronBus extends AsyncBus { |
|
28
|
|
|
/** |
|
29
|
|
|
* @var \OCP\BackgroundJob\IJobList |
|
30
|
|
|
*/ |
|
31
|
|
|
private $jobList; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param \OCP\BackgroundJob\IJobList $jobList |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($jobList) { |
|
38
|
|
|
$this->jobList = $jobList; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
protected function queueCommand($command) { |
|
42
|
|
|
$this->jobList->add($this->getJobClass($command), $this->serializeCommand($command)); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param \OCP\Command\ICommand | callable $command |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
private function getJobClass($command) { |
|
50
|
|
|
if ($command instanceof \Closure) { |
|
51
|
|
|
return 'OC\Command\ClosureJob'; |
|
52
|
|
|
} else if (is_callable($command)) { |
|
53
|
|
|
return 'OC\Command\CallableJob'; |
|
54
|
|
|
} else if ($command instanceof ICommand) { |
|
55
|
|
|
return 'OC\Command\CommandJob'; |
|
56
|
|
|
} else { |
|
57
|
|
|
throw new \InvalidArgumentException('Invalid command'); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param \OCP\Command\ICommand | callable $command |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
private function serializeCommand($command) { |
|
66
|
|
|
if ($command instanceof \Closure) { |
|
67
|
|
|
$serializer = new Serializer(); |
|
68
|
|
|
return $serializer->serialize($command); |
|
69
|
|
|
} else if (is_callable($command) or $command instanceof ICommand) { |
|
|
|
|
|
|
70
|
|
|
return serialize($command); |
|
71
|
|
|
} else { |
|
72
|
|
|
throw new \InvalidArgumentException('Invalid command'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and&&or||The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&, or||.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
dieintroduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrowat this point:These limitations lead to logical operators rarely being of use in current PHP code.