sebastianfeldmann /
crontab
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * This file is part of SebastianFeldmann\Crontab. |
||
| 5 | * |
||
| 6 | * (c) Sebastian Feldmann <[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 SebastianFeldmann\Crontab; |
||
| 13 | |||
| 14 | use RuntimeException; |
||
| 15 | use SebastianFeldmann\Cli\Command\Runner; |
||
| 16 | use SebastianFeldmann\Crontab\Output\ListFormatter; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Class Operator |
||
| 20 | * |
||
| 21 | * @package SebastianFeldmann\Crontab |
||
| 22 | * @author Sebastian Feldmann <[email protected]> |
||
| 23 | * @link https://github.com/sebastianfeldmann/crontab |
||
| 24 | * @since Class available since Release 0.9.0 |
||
| 25 | */ |
||
| 26 | class Operator |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Path to crontab cli command. |
||
| 30 | * |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $pathToCmd; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Runner to execute cli commands. |
||
| 37 | * |
||
| 38 | * @var \SebastianFeldmann\Cli\Command\Runner |
||
| 39 | */ |
||
| 40 | private $runner; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Crontab command row parser. |
||
| 44 | * |
||
| 45 | * @var \SebastianFeldmann\Crontab\Parser |
||
| 46 | */ |
||
| 47 | private $parser; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Username |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $user; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Operator constructor. |
||
| 58 | * |
||
| 59 | * @param \SebastianFeldmann\Crontab\Parser $parser |
||
| 60 | * @param \SebastianFeldmann\Cli\Command\Runner $runner |
||
| 61 | */ |
||
| 62 | public function __construct(Parser $parser = null, Runner $runner = null) |
||
| 63 | { |
||
| 64 | $this->parser = empty($parser) ? new Parser\Vixie() : $parser; |
||
| 65 | $this->runner = empty($runner) ? new Runner\Simple() : $runner; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Set custom path to 'crontab' command. |
||
| 70 | * |
||
| 71 | * @param string $pathToCmd |
||
| 72 | * @return \SebastianFeldmann\Crontab\Operator |
||
| 73 | */ |
||
| 74 | public function setCommandPath(string $pathToCmd): Operator |
||
| 75 | { |
||
| 76 | $this->pathToCmd = $pathToCmd; |
||
| 77 | return $this; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * User setter. |
||
| 82 | * |
||
| 83 | * @param string $user |
||
| 84 | * @return \SebastianFeldmann\Crontab\Operator |
||
| 85 | */ |
||
| 86 | public function setUser(string $user): Operator |
||
| 87 | { |
||
| 88 | $this->user = $user; |
||
| 89 | return $this; |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Return list of crontab entries. |
||
| 94 | * |
||
| 95 | * @return \SebastianFeldmann\Crontab\Job[] |
||
| 96 | * @throws \RuntimeException |
||
| 97 | */ |
||
| 98 | public function getJobs(): array |
||
| 99 | { |
||
| 100 | $cmd = new Cli\Executable(); |
||
| 101 | $cmdLine = $cmd->forUser($this->user)->createCommandLine(); |
||
| 102 | |||
| 103 | try { |
||
| 104 | $result = $this->runner->run($cmdLine, new ListFormatter($this->parser)); |
||
| 105 | } catch (RuntimeException $e) { |
||
| 106 | if (strpos($e->getMessage(), 'no crontab') !== false) { |
||
| 107 | return []; |
||
| 108 | } |
||
| 109 | throw $e; |
||
| 110 | } |
||
| 111 | |||
| 112 | return $result->getFormattedOutput(); |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Checks if a job is scheduled already. |
||
| 117 | * |
||
| 118 | * @param \SebastianFeldmann\Crontab\Job $job Job to search for. |
||
| 119 | * @param bool $strict If strict is true the schedule has to match as well. |
||
| 120 | * @return bool |
||
| 121 | */ |
||
| 122 | public function isJobScheduled(Job $job, bool $strict = true) |
||
| 123 | { |
||
| 124 | // get current job list |
||
| 125 | foreach ($this->getJobs() as $activeJob) { |
||
| 126 | if ($job->getCommand() == $activeJob->getCommand()) { |
||
| 127 | if (!$strict || $job->getSchedule() == $activeJob->getSchedule()) { |
||
| 128 | return true; |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } |
||
| 132 | return false; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Add a crontab entry to a users crontab. |
||
| 137 | * |
||
| 138 | * @param \SebastianFeldmann\Crontab\Job $entry |
||
| 139 | */ |
||
| 140 | public function scheduleJob(Job $entry) |
||
| 141 | { |
||
| 142 | $exe = new Cli\Executable(); |
||
| 143 | $cmd = $exe->forUser($this->user)->addJob($entry)->createCommandLine(); |
||
| 144 | $this->runner->run($cmd); |
||
| 145 | } |
||
| 146 | } |
||
| 147 |