Source of file Job.php
Size: 5,351 Bytes - Last Modified: 2021-12-23T10:11:42+00:00
/var/www/docs.ssmods.com/process/src/code/cronkeep/src/application/models/Crontab/Job.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 | <?php /** * Copyright 2014 Bogdan Ghervan * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ namespace models\Crontab; /** * Job model. * * @author Bogdan Ghervan <bogdan.ghervan@gmail.com> * @copyright 2014 Bogdan Ghervan * @link http://github.com/cronkeep/cronkeep * @license http://opensource.org/licenses/Apache-2.0 Apache License 2.0 */ class Job { /** * @var string */ protected $_originalRaw; /** * @var string */ protected $_raw; /** * @var string|Expression */ protected $_expression; /** * @var string */ protected $_command; /** * @var boolean */ protected $_isPaused; /** * @var string */ protected $_comment; /** * @var string */ protected $_hash; /** * Default line separator. * * @var string */ protected $_lineSeparator = PHP_EOL; /** * Sets raw job representation. * @todo Find another way to keep original data and changed data * * @param string $raw * @return Job */ public function setRaw($raw) { if (is_null($this->_originalRaw)) { $this->_originalRaw = $raw; } $this->_raw = $raw; $this->_generateHash(); return $this; } /** * Sets expression part. * * @param string|Expression $expression * @return Job */ public function setExpression($expression) { $this->_expression = $expression; $this->_updateRaw(); return $this; } /** * Sets command part. * * @param string $command * @return Job */ public function setCommand($command) { $this->_command = $command; $this->_updateRaw(); return $this; } /** * Sets comment. * * @param string $comment * @return Job */ public function setComment($comment) { $this->_comment = $comment; $this->_updateRaw(); return $this; } /** * Sets job schedule status (paused or ready to run). * * @param boolean $isPaused * @return Job */ public function setIsPaused($isPaused) { $this->_isPaused = (bool) $isPaused; $this->_updateRaw(); return $this; } /** * Retrieves initial raw job representation. * * @return string */ public function getOriginalRaw() { return $this->_originalRaw; } /** * Retrieves raw job representation. * * @return string * @throws \BadMethodCallException */ public function getRaw() { if (empty($this->_raw)) { throw new \BadMethodCallException('Raw job definition could not be generated for ' . 'incomplete object (either expression or command fields are missing)'); } return $this->_raw; } /** * Retrieves expression part. * * @return string */ public function getExpression() { return $this->_expression; } /** * Retrieves command part. * * @return string */ public function getCommand() { return $this->_command; } /** * Retrieves job schedule status (paused or ready to run). * * @return string */ public function getIsPaused() { return $this->_isPaused; } /** * Retrieves comment. * * @return string */ public function getComment() { return $this->_comment; } /** * Retrieves crc32 generated hash unique to this job. * * @return string */ public function getHash() { return $this->_hash; } /** * Rebuilds internal raw job representation from parts and returns it. * * @return string */ protected function _updateRaw() { if ($this->_command && $this->_expression) { $newRaw = ($this->_comment ? '# ' . ltrim($this->_comment, '# ') . $this->_lineSeparator : '') . ($this->_isPaused ? '# ' : '') . $this->_expression . ' ' . $this->_command . $this->_lineSeparator; // Update raw job representation (this in turn refreshes the hash) $this->setRaw($newRaw); } return $this; } /** * Generates unique hash for this job using crc32. * * @return Job */ protected function _generateHash() { $this->_hash = hash('crc32', $this->_raw); return $this; } } |