new pisilinux web sites
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Structure;
|
||||
|
||||
class ControllerCommand
|
||||
{
|
||||
/**
|
||||
* Magic Constructor
|
||||
*
|
||||
* @param string $path
|
||||
* @param string & $command
|
||||
*/
|
||||
public function __construct(string $path, & $command)
|
||||
{
|
||||
$datas = Structure::data($path);
|
||||
$controller = $datas['page'];
|
||||
$function = $datas['function'];
|
||||
$namespace = $datas['namespace'];
|
||||
$parameters = $datas['parameters'];
|
||||
$class = $namespace . $controller;
|
||||
$file = str_replace('\\', '\\\\', $datas['file']);
|
||||
$command = '(new \\'.$class.')->' . $function . '(' . Parameters::convert((array) $parameters) . ')';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
interface CrontabIntervalInterface
|
||||
{
|
||||
/**
|
||||
* Hourly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function hourly() : Job;
|
||||
|
||||
/**
|
||||
* Daily
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function daily() : Job;
|
||||
|
||||
/**
|
||||
* Midnight
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function midnight() : Job;
|
||||
|
||||
/**
|
||||
* Monthly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function monthly() : Job;
|
||||
|
||||
/**
|
||||
* Weekly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function weekly() : Job;
|
||||
|
||||
/**
|
||||
* Yearly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function yearly() : Job;
|
||||
|
||||
/**
|
||||
* Annualy
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function annualy() : Job;
|
||||
|
||||
/**
|
||||
* Clock
|
||||
*
|
||||
* @param string $clock = '23:59'
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function clock(string $clock) : Job;
|
||||
|
||||
/**
|
||||
* Minute
|
||||
*
|
||||
* @param string $minute
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function minute(string $minute) : Job;
|
||||
|
||||
/**
|
||||
* Perminute
|
||||
*
|
||||
* @param string $minute
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perMinute(string $minute) : Job;
|
||||
|
||||
/**
|
||||
* Hour
|
||||
*
|
||||
* @param string $hour
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function hour(string $hour) : Job;
|
||||
|
||||
/**
|
||||
* Perhour
|
||||
*
|
||||
* @param string $hour
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perHour(string $hour) : Job;
|
||||
|
||||
/**
|
||||
* Day Number
|
||||
*
|
||||
* @param string $dayNumber
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function dayNumber(string $dayNumber) : Job;
|
||||
|
||||
/**
|
||||
* Month Number
|
||||
*
|
||||
* @param string $monthNumber
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function month(string $monthNumber) : Job;
|
||||
|
||||
/**
|
||||
* Permonth
|
||||
*
|
||||
* @param string $month
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perMonth(string $month) : Job;
|
||||
|
||||
/**
|
||||
* Day
|
||||
*
|
||||
* @param string $day= '*'
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function day(string $day) : Job;
|
||||
|
||||
/**
|
||||
* Perday
|
||||
*
|
||||
* @param string $day
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perDay(string $day) : Job;
|
||||
|
||||
/**
|
||||
* Interval
|
||||
*
|
||||
* @param string $interval = '* * * * *'
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function interval(string $interval) : Job;
|
||||
}
|
||||
@@ -0,0 +1,375 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Base;
|
||||
use ZN\Crontab\Exception\InvalidTimeFormatException;
|
||||
|
||||
trait CrontabIntervalTrait
|
||||
{
|
||||
/**
|
||||
* Interval
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $interval = '* * * * *';
|
||||
|
||||
/**
|
||||
* Minute
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $minute = '*';
|
||||
|
||||
/**
|
||||
* Hour
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $hour = '*';
|
||||
|
||||
/**
|
||||
* Day Number
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $dayNumber = '*';
|
||||
|
||||
/**
|
||||
* Month
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $month = '*';
|
||||
|
||||
/**
|
||||
* Day
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $day = '*';
|
||||
|
||||
/**
|
||||
* Month Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $monthFormat =
|
||||
[
|
||||
'january' => 1,
|
||||
'february' => 2,
|
||||
'march' => 3,
|
||||
'april' => 4,
|
||||
'may' => 5,
|
||||
'june' => 6,
|
||||
'july' => 7,
|
||||
'august' => 8,
|
||||
'september' => 9,
|
||||
'october' => 10,
|
||||
'november' => 11,
|
||||
'december' => 12
|
||||
];
|
||||
|
||||
/**
|
||||
* Day Format
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dayFormat =
|
||||
[
|
||||
'sunday' => 0,
|
||||
'monday' => 1,
|
||||
'tuesday' => 2,
|
||||
'wednesday' => 3,
|
||||
'thursday' => 4,
|
||||
'friday' => 5,
|
||||
'saturday' => 6
|
||||
];
|
||||
|
||||
/**
|
||||
* Hourly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function hourly() : Job
|
||||
{
|
||||
$this->interval = '0 * * * *';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Daily
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function daily() : Job
|
||||
{
|
||||
$this->interval = '0 0 * * *';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Midnight
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function midnight() : Job
|
||||
{
|
||||
$this->daily();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Monthly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function monthly() : Job
|
||||
{
|
||||
$this->interval = '0 0 1 * *';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Weekly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function weekly() : Job
|
||||
{
|
||||
$this->interval = '0 0 * * 0';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Yearly
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function yearly() : Job
|
||||
{
|
||||
$this->interval = '0 0 1 1 *';
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Annualy
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function annualy() : Job
|
||||
{
|
||||
$this->yearly();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clock
|
||||
*
|
||||
* @param string $clock = '23:59'
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function clock(string $clock = '23:59') : Job
|
||||
{
|
||||
if( ! preg_match('/^(?:2[0-3]|[01][0-9]):[0-5][0-9]$/', $clock) )
|
||||
{
|
||||
throw new InvalidTimeFormatException(NULL, $clock);
|
||||
}
|
||||
else
|
||||
{
|
||||
$clockEx = explode(':', $clock);
|
||||
|
||||
$this->minute($clockEx[1]);
|
||||
$this->hour($clockEx[0]);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Minute
|
||||
*
|
||||
* @param string $minute
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function minute(string $minute) : Job
|
||||
{
|
||||
$this->minute = $this->_slashes($minute);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perminute
|
||||
*
|
||||
* @param string $minute
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perMinute(string $minute) : Job
|
||||
{
|
||||
$this->_per($minute, 'minute');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hour
|
||||
*
|
||||
* @param string $hour
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function hour(string $hour) : Job
|
||||
{
|
||||
$this->hour = $this->_slashes($hour);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perhour
|
||||
*
|
||||
* @param string $hour
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perHour(string $hour) : Job
|
||||
{
|
||||
$this->_per($hour, 'hour');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Day Number
|
||||
*
|
||||
* @param string $dayNumber
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function dayNumber(string $dayNumber) : Job
|
||||
{
|
||||
$this->dayNumber = $this->_slashes($dayNumber);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Month Number
|
||||
*
|
||||
* @param string $monthNumber
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function month(string $monthNumber) : Job
|
||||
{
|
||||
$this->_format('monthFormat', __FUNCTION__, $monthNumber );
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Permonth
|
||||
*
|
||||
* @param string $month
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perMonth(string $month) : Job
|
||||
{
|
||||
$this->_per($month, 'month');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Day
|
||||
*
|
||||
* @param string $day= '*'
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function day(string $day) : Job
|
||||
{
|
||||
$this->_format('dayFormat', __FUNCTION__, $day);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Perday
|
||||
*
|
||||
* @param string $day
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function perDay(string $day) : Job
|
||||
{
|
||||
$this->_per($day, 'day');
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Interval
|
||||
*
|
||||
* @param string $interval = '* * * * *'
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function interval(string $interval = '* * * * *') : Job
|
||||
{
|
||||
$this->interval = $interval;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected Time Format
|
||||
*/
|
||||
protected function _format($varname, $objectname, $data)
|
||||
{
|
||||
$format = $this->$varname;
|
||||
$replaceData = str_ireplace(array_keys($format), array_values($format), $data ?? '');
|
||||
|
||||
$this->$objectname = $this->_slashes($replaceData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected Per
|
||||
*/
|
||||
protected function _per($time, $function)
|
||||
{
|
||||
$this->$function(Base::prefix($time));
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected Slashes
|
||||
*/
|
||||
protected function _slashes($data)
|
||||
{
|
||||
if( $data[0] === '/' )
|
||||
{
|
||||
return Base::prefix($data, '*');
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php namespace ZN\Crontab\Exception;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Exception;
|
||||
|
||||
class InvalidTimeFormatException extends Exception
|
||||
{
|
||||
const lang =
|
||||
[
|
||||
'en' => '[%] Invalid time format!',
|
||||
'tr' => '[%] Geçersiz zaman formatı!'
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,621 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
use ZN\Base;
|
||||
use ZN\Config;
|
||||
use ZN\Singleton;
|
||||
use ZN\Filesystem;
|
||||
use ZN\DataTypes\Arrays;
|
||||
use ZN\Crontab\Exception\InvalidTimeFormatException;
|
||||
|
||||
class Job implements JobInterface, CrontabIntervalInterface
|
||||
{
|
||||
use CrontabIntervalTrait;
|
||||
|
||||
/**
|
||||
* Command Type
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $type;
|
||||
|
||||
/**
|
||||
* Is Debug
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $debug = false;
|
||||
|
||||
/**
|
||||
* Crontab Directory
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $crontabDir = '';
|
||||
|
||||
/**
|
||||
* Jobs
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $jobs = [];
|
||||
|
||||
/**
|
||||
* Basic Structure
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $directoryIndexCommand;
|
||||
|
||||
/**
|
||||
* Define
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $projectCommand = NULL;
|
||||
|
||||
/**
|
||||
* Crontab Commands
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $crontabCommands;
|
||||
|
||||
/**
|
||||
* Crontab File Name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $fileName = 'Crontab' . DS . 'Jobs';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $user = NULL;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $command;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $parameters = [];
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $limitFile = 'Limit.json';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $stringQuery;
|
||||
|
||||
/**
|
||||
* @var processor class
|
||||
*/
|
||||
protected $processor;
|
||||
|
||||
/**
|
||||
* Magic Constructor
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$getConfig = Config::default('ZN\Prompt\PromptDefaultConfiguration')
|
||||
::get('Services', 'processor');
|
||||
|
||||
$this->directoryIndexCommand = $this->getDirectoryIndexCommand();
|
||||
$this->processor = Singleton::class('ZN\Prompt\Processor');
|
||||
|
||||
if( PROJECT_TYPE === 'EIP' )
|
||||
{
|
||||
$this->crontabCommands = EXTERNAL_DIR . $this->fileName;
|
||||
$this->user = DEFINED_CURRENT_PROJECT;
|
||||
|
||||
$this->getProjectCommand($this->projectCommand = $this->directoryIndexCommand);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->crontabCommands = FILES_DIR . $this->fileName; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
$this->createCrontabDirectoryIfNotExists();
|
||||
|
||||
$this->path = $getConfig['path'];
|
||||
$this->crontabDir = Filesystem\Info::originpath(STORAGE_DIR.'Crontab'.DS);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Send parameters
|
||||
*
|
||||
* @param mixed ...$arguments
|
||||
*/
|
||||
public function parameters(...$parameters)
|
||||
{
|
||||
$this->parameters = $parameters;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get crontab commands.
|
||||
*/
|
||||
public function getCrontabCommands()
|
||||
{
|
||||
return $this->crontabCommands;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crontab Queue
|
||||
*
|
||||
* @param callable $callable
|
||||
* @param int $decrement = 1
|
||||
*/
|
||||
public function queue(callable $callable, int $decrement = 1)
|
||||
{
|
||||
new Queue($callable, $decrement, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crontab limit
|
||||
*
|
||||
* @param int $getLimit = 1
|
||||
*/
|
||||
public function limit(int $getLimit = 1)
|
||||
{
|
||||
new Limit($getLimit, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects project
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function project(string $name)
|
||||
{
|
||||
$this->user = $name;
|
||||
|
||||
$this->crontabDir = str_replace(REQUESTED_CURRENT_PROJECT, $this->user, $this->crontabDir);
|
||||
|
||||
$this->getProjectCommand($this->projectCommand);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select Processor Driver
|
||||
*
|
||||
* @param string $driver
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function driver(string $driver) : Job
|
||||
{
|
||||
$this->processor->driver($driver);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets crontab list array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listArray() : array
|
||||
{
|
||||
if( ! is_file($this->crontabCommands) )
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
return Arrays\RemoveElement::element(explode(EOL, file_get_contents($this->crontabCommands)), '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets crontab list
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function list() : string
|
||||
{
|
||||
$list = '';
|
||||
|
||||
if( is_file($this->crontabCommands) )
|
||||
{
|
||||
$jobs = $this->listArray();
|
||||
$list = '<pre>';
|
||||
$list .= '[ID] CRON JOB<br><br>';
|
||||
|
||||
foreach( $jobs as $key => $job )
|
||||
{
|
||||
$list .= '[' . $key . ']: '. $job . '<br>';
|
||||
}
|
||||
|
||||
$list .= '</pre>';
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Last job
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function lastJob()
|
||||
{
|
||||
return $this->processor->exec('crontab -l');
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove cron job
|
||||
*
|
||||
* @param string $key = NULL
|
||||
*/
|
||||
public function remove($key = NULL)
|
||||
{
|
||||
$this->executeRemoveCommand();
|
||||
|
||||
if( $key === NULL )
|
||||
{
|
||||
unlink($this->crontabCommands);
|
||||
}
|
||||
else
|
||||
{
|
||||
if( is_numeric($key) )
|
||||
{
|
||||
$this->removeJobFromExecFile($key);
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->removeJobFromExecFileWithTerm($key);
|
||||
}
|
||||
|
||||
$this->executeCommand();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Debug status
|
||||
*
|
||||
* @param bool $status = true
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function debug(bool $status = true) : Job
|
||||
{
|
||||
$this->debug = $status;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cron Controller
|
||||
*
|
||||
* @param string $file
|
||||
*/
|
||||
public function controller(string $file)
|
||||
{
|
||||
new ControllerCommand($file, $command);
|
||||
|
||||
$code = Base::prefix(Base::suffix($command, ';\''), ' -r \'' . $this->directoryIndexCommand);
|
||||
|
||||
return $this->run($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cron wget
|
||||
*
|
||||
* @param string $url
|
||||
*/
|
||||
public function wget(string $url)
|
||||
{
|
||||
$this->path('wget');
|
||||
|
||||
return $this->run($url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Cron Command
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $type = 'Project' - options[Project|External]
|
||||
*/
|
||||
public function command(string $file, $type = 'Project')
|
||||
{
|
||||
$path = $this->convertFileName($file);
|
||||
$pathEx = explode('-', $path);
|
||||
$command = $pathEx[0];
|
||||
$method = $pathEx[1] ?? Config::get('Routing', 'openFunction') ?: 'main';
|
||||
|
||||
$code = ' -r \'' . $this->directoryIndexCommand . '(new \\'.$type.'\Commands\\'.$command.')->'.$method.'(' . Parameters::convert($this->parameters) . ');\'';
|
||||
|
||||
$this->parameters = [];
|
||||
|
||||
return $this->run($code);
|
||||
}
|
||||
|
||||
/**
|
||||
* Script [6.8.0]
|
||||
*
|
||||
* @param string $cmd
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function script(string $cmd)
|
||||
{
|
||||
return $this->path('')->run($cmd);
|
||||
}
|
||||
|
||||
/**
|
||||
* Path
|
||||
*
|
||||
* @param string $path = NULL
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function path(?string $path = NULL)
|
||||
{
|
||||
$this->path = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run Cron
|
||||
*
|
||||
* @param string $cmd = NULL
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function run(?string $cmd = NULL)
|
||||
{
|
||||
$this->createExecFileIfNotExists();
|
||||
|
||||
$this->addJobToExecFile($cmd);
|
||||
|
||||
return $this->executeCommand();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get string query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function stringQuery()
|
||||
{
|
||||
return $this->stringQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected create crontab directory if not exists
|
||||
*/
|
||||
protected function createCrontabDirectoryIfNotExists()
|
||||
{
|
||||
if( ! is_dir($crontabDirectory = pathinfo($this->crontabCommands, PATHINFO_DIRNAME)) )
|
||||
{
|
||||
Filesystem::createFolder($crontabDirectory); // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected execute command
|
||||
*/
|
||||
protected function executeCommand()
|
||||
{
|
||||
$this->defaultCommandVariables();
|
||||
|
||||
return $this->processor->exec('crontab ' . $this->crontabCommands);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected execute remove command
|
||||
*/
|
||||
protected function executeRemoveCommand()
|
||||
{
|
||||
$this->processor->exec('crontab -r');
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected create exec file if not exists
|
||||
*/
|
||||
protected function createExecFileIfNotExists()
|
||||
{
|
||||
if( ! is_file($this->crontabCommands) )
|
||||
{
|
||||
Filesystem\Forge::create($this->crontabCommands);
|
||||
|
||||
$this->processor->exec('chmod 0777 ' . $this->crontabCommands);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected add job to exec file
|
||||
*/
|
||||
protected function addJobToExecFile($cmd)
|
||||
{
|
||||
$this->stringQuery = $cmd;
|
||||
|
||||
$content = file_get_contents($this->crontabCommands);
|
||||
|
||||
if( ! stristr($content, $cmd))
|
||||
{
|
||||
$this->stringQuery = $stringQuery = $this->getValidCommand() . $cmd;
|
||||
|
||||
$content = $content . $stringQuery . EOL;
|
||||
|
||||
file_put_contents($this->crontabCommands, $content);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected remove job from exec file
|
||||
*/
|
||||
protected function removeJobFromExecFile($cmd)
|
||||
{
|
||||
$jobs = $this->listArray();
|
||||
|
||||
unset($jobs[$cmd]);
|
||||
|
||||
$this->writeCrontabCommands($jobs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get job id from exec file with term
|
||||
*/
|
||||
public function getJobIdFromExecFileWithTerm($class, $method)
|
||||
{
|
||||
$jobs = [];
|
||||
|
||||
foreach( $this->listArray() as $key => $job )
|
||||
{
|
||||
if( stristr($job, $class . ')') && stristr($job, $method . '(') )
|
||||
{
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
|
||||
return false; // @codeCoverageIgnore
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected remove job from exec file with term
|
||||
*/
|
||||
protected function removeJobFromExecFileWithTerm($cmd)
|
||||
{
|
||||
$jobs = [];
|
||||
|
||||
foreach( $this->listArray() as $key => $job )
|
||||
{
|
||||
if( ! stristr($job, $cmd) )
|
||||
{
|
||||
$jobs[$key] = $job; // @codeCoverageIgnore
|
||||
}
|
||||
}
|
||||
|
||||
$this->writeCrontabCommands($jobs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected write crontab commands
|
||||
*/
|
||||
protected function writeCrontabCommands($jobs)
|
||||
{
|
||||
file_put_contents($this->crontabCommands, implode(EOL, $jobs) . ($jobs ? EOL : NULL));
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected Zerocore
|
||||
*/
|
||||
protected function getDirectoryIndexCommand()
|
||||
{
|
||||
return 'define("CONSOLE_ENABLED", true); require "'.DIRECTORY_INDEX.'"; ';
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected get project command
|
||||
*/
|
||||
protected function getProjectCommand($value)
|
||||
{
|
||||
$this->directoryIndexCommand = 'chdir("'.REAL_BASE_DIR.'"); define("CONSOLE_PROJECT_NAME", "'.$this->user.'"); ' . $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected convert file name
|
||||
*/
|
||||
protected function convertFileName($file)
|
||||
{
|
||||
return str_replace(['/', ':'], '-', $file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected datet time format
|
||||
*/
|
||||
protected function getDatetimeFormat()
|
||||
{
|
||||
if( $this->interval !== '* * * * *' )
|
||||
{
|
||||
$interval = $this->interval.' ';
|
||||
}
|
||||
else
|
||||
{
|
||||
$interval = ( $this->minute ?? '*' ) . ' '.
|
||||
( $this->hour ?? '*' ) . ' '.
|
||||
( $this->dayNumber ?? '*' ) . ' '.
|
||||
( $this->month ?? '*' ) . ' '.
|
||||
( $this->day ?? '*' ) . ' ';
|
||||
}
|
||||
|
||||
$this->defaultIntervalVariables();
|
||||
|
||||
return $interval;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected Command
|
||||
*/
|
||||
protected function getValidCommand()
|
||||
{
|
||||
$datetimeFormat = $this->getDatetimeFormat();
|
||||
$type = $this->type;
|
||||
$path = $this->path;
|
||||
$command = $this->command;
|
||||
$debug = $this->debug;
|
||||
|
||||
$pattern = str_repeat('(\*|[0-9]{1,2}|\*\/[0-9]{1,2}|[0-9]{1,2}\s*\-\s*[0-9]{1,2}|(([0-9]{1,2})*\s*\,\s*[0-9]{1,2})+)\s+', 5);
|
||||
|
||||
if( ! preg_match('/^' . $pattern . '$/', $datetimeFormat) )
|
||||
{
|
||||
throw new InvalidTimeFormatException('Services', 'crontab:timeFormatError');
|
||||
}
|
||||
else
|
||||
{
|
||||
return $datetimeFormat.
|
||||
( ! empty($path) ? $path . ' ' : '' ).
|
||||
( ! empty($command) ? $command . ' ' : '' ).
|
||||
( ! empty($type) ? $type . ' ' : '' ).
|
||||
( $debug === true ? '>> ' . $this->crontabDir . 'debug.log 2>&1' : '' );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected defaul command variables
|
||||
*/
|
||||
protected function defaultCommandVariables()
|
||||
{
|
||||
$this->type = NULL;
|
||||
$this->path = NULL;
|
||||
$this->command = NULL;
|
||||
$this->debug = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected default interval variables
|
||||
*/
|
||||
protected function defaultIntervalVariables()
|
||||
{
|
||||
$this->interval = '* * * * *';
|
||||
$this->minute = '*';
|
||||
$this->hour = '*';
|
||||
$this->dayNumber = '*';
|
||||
$this->month = '*';
|
||||
$this->day = '*';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
interface JobInterface
|
||||
{
|
||||
/**
|
||||
* Crontab Queue
|
||||
*
|
||||
* @param callable $callable
|
||||
* @param int $decrement = 1
|
||||
*/
|
||||
public function queue(callable $callable, int $decrement = 1);
|
||||
|
||||
/**
|
||||
* Crontab limit
|
||||
*
|
||||
* @param int $getLimit = 1
|
||||
*/
|
||||
public function limit(int $getLimit = 1);
|
||||
|
||||
/**
|
||||
* Selects project
|
||||
*
|
||||
* @param string $name
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function driver(string $driver) : Job;
|
||||
|
||||
/**
|
||||
* Gets crontab list array
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function listArray() : array;
|
||||
|
||||
/**
|
||||
* Gets crontab list
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function list() : string;
|
||||
|
||||
/**
|
||||
* Remove cron job
|
||||
*
|
||||
* @param string $key = NULL
|
||||
*/
|
||||
public function remove($key = NULL);
|
||||
|
||||
/**
|
||||
* Run Cron
|
||||
*
|
||||
* @param string $cmd = NULL
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function run(?string $cmd = NULL);
|
||||
|
||||
/**
|
||||
* Get string query
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function stringQuery();
|
||||
|
||||
/**
|
||||
* Debug status
|
||||
*
|
||||
* @param bool $status = true
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function debug(bool $status) : Job;
|
||||
|
||||
/**
|
||||
* Cron Controller
|
||||
*
|
||||
* @param string $file
|
||||
*/
|
||||
public function controller(string $file);
|
||||
|
||||
/**
|
||||
* Cron Command
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $type = 'Project' - options[Project|External]
|
||||
*/
|
||||
public function command(string $file);
|
||||
|
||||
/**
|
||||
* Script [6.8.0]
|
||||
*
|
||||
* @param string $cmd
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function script(string $cmd);
|
||||
|
||||
/**
|
||||
* Cron wget
|
||||
*
|
||||
* @param string $url
|
||||
*/
|
||||
public function wget(string $url);
|
||||
|
||||
/**
|
||||
* Path
|
||||
*
|
||||
* @param string $path = NULL
|
||||
*
|
||||
* @return Job
|
||||
*/
|
||||
public function path(?string $path = NULL);
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
class Limit extends QueueLimitExtends
|
||||
{
|
||||
/**
|
||||
* Keeps queue file name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $file = 'Limit.json';
|
||||
|
||||
/**
|
||||
* Magic constructor
|
||||
*
|
||||
* @param string $path
|
||||
*/
|
||||
public function __construct(int $getLimit, Job $crontab)
|
||||
{
|
||||
$backtrace = (object) debug_backtrace(2)[4];
|
||||
|
||||
$id = $crontab->getJobIdFromExecFileWithTerm($backtrace->class, $backtrace->function);
|
||||
|
||||
if( $id !== false )
|
||||
{
|
||||
$this->start($id, 1, $key, $getJsonData, $limit, $crontab);
|
||||
|
||||
if( $limit === $getLimit )
|
||||
{
|
||||
$this->removeKeyFromJsonData($key, $getJsonData);
|
||||
}
|
||||
else
|
||||
{
|
||||
$getJsonData[$key] = ++$limit;
|
||||
}
|
||||
|
||||
$this->end($getJsonData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
|
||||
class Parameters
|
||||
{
|
||||
/**
|
||||
* Magic Constructor
|
||||
*
|
||||
* @param string $path
|
||||
* @param string & $command
|
||||
*/
|
||||
public static function convert(array $parameters)
|
||||
{
|
||||
return $parameters ? implode(',', array_map(function($data){ return '"'.$data.'"';}, $parameters)) : '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
class Queue extends QueueLimitExtends
|
||||
{
|
||||
/**
|
||||
* Keeps queue file name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $file = 'Queue.json';
|
||||
|
||||
/**
|
||||
* Magic constructor
|
||||
*
|
||||
* @param string $path
|
||||
*/
|
||||
public function __construct(callable $callable, int $increment, Job $crontab)
|
||||
{
|
||||
$backtrace = (object) debug_backtrace(2)[4];
|
||||
|
||||
$id = $crontab->getJobIdFromExecFileWithTerm($backtrace->class, $backtrace->function);
|
||||
|
||||
if( $id !== false )
|
||||
{
|
||||
$this->start($id, 0, $key, $getJsonData, $limit, $crontab);
|
||||
|
||||
if( $callable($limit, $increment) === false )
|
||||
{
|
||||
$this->removeKeyFromJsonData($key, $getJsonData);
|
||||
}
|
||||
else
|
||||
{
|
||||
$limit += $increment;
|
||||
|
||||
$getJsonData[$key] = $limit;
|
||||
}
|
||||
|
||||
$this->end($getJsonData);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?php namespace ZN\Crontab;
|
||||
/**
|
||||
* ZN PHP Web Framework
|
||||
*
|
||||
* "Simplicity is the ultimate sophistication." ~ Da Vinci
|
||||
*
|
||||
* @package ZN
|
||||
* @license MIT [http://opensource.org/licenses/MIT]
|
||||
* @author Ozan UYKUN [ozan@znframework.com]
|
||||
*/
|
||||
class QueueLimitExtends
|
||||
{
|
||||
/**
|
||||
* Keeps Crontab object
|
||||
*
|
||||
* @var Crontab
|
||||
*/
|
||||
protected $crontab;
|
||||
|
||||
/**
|
||||
* Protected start
|
||||
*/
|
||||
protected function start($id, $start, &$key, &$getJsonData, &$limit, $crontab)
|
||||
{
|
||||
$this->crontab = $crontab;
|
||||
|
||||
$this->setFile();
|
||||
|
||||
$key = $this->getProcessId($id);
|
||||
|
||||
if( ! is_file($this->file) )
|
||||
{
|
||||
$this->addDataToFile([$key => $start]);
|
||||
}
|
||||
|
||||
$getJsonData = $this->getJsonDataToFile();
|
||||
|
||||
$limit = (int) ($getJsonData[$key] ?? 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected end
|
||||
*/
|
||||
protected function end($getJsonData)
|
||||
{
|
||||
$this->addDataToFile($getJsonData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected remove key from json data
|
||||
*/
|
||||
protected function removeKeyFromJsonData($key, &$getJsonData)
|
||||
{
|
||||
$this->crontab->remove((int) ltrim($key, 'ID'));
|
||||
|
||||
if( isset($getJsonData[$key]) )
|
||||
{
|
||||
unset($getJsonData[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected get json data to limit/queue file
|
||||
*/
|
||||
protected function getJsonDataToFile()
|
||||
{
|
||||
return json_decode(file_get_contents($this->file), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected set queue file
|
||||
*/
|
||||
protected function setFile()
|
||||
{
|
||||
$this->file = $this->crontab->getCrontabCommands() . $this->file;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected function get process id
|
||||
*/
|
||||
protected function getProcessId($id)
|
||||
{
|
||||
return 'ID' . $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Protected add data to limit/queue file
|
||||
*/
|
||||
protected function addDataToFile($data)
|
||||
{
|
||||
file_put_contents($this->file, json_encode($data) . EOL);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user