$e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTrace() ]; self::report($error, 'error'); } } self::close(); } /** * Remove process ID * * @param string $procId = current-process * * @return bool */ public static function remove(string $procId = '') : bool { $procFile = self::getProcFile($procId); if( is_file($procFile) ) { return unlink($procFile); } return false; } /** * Create report * * @param array $data * * @return int */ public static function report(array $data, string $suffix = 'report') : int { return file_put_contents(self::$procId . '-' . $suffix, Json::encode($data)); } /** * Output * * @param array $data * * @return int */ public static function output(array $data) : int { return self::report($data, 'output'); } /** * Creates socket */ public static function socket() { $originFile = self::getProcFile($_POST['procId']); $procFile = Base::suffix($originFile, '-output'); if( is_file($originFile) ) { echo json_encode(['status' => 'processing']); } else if( is_file($procFile) ) { echo file_get_contents($procFile); unlink($procFile); } else { echo json_encode([]); } exit; } /** * Sets socket success * * @param callable $callback */ public static function success(callable $callable) { self::$success = $callable; return new self; } /** * Sets socket error * * @param callable $callback */ public static function error(callable $callable) { self::$error = $callable; return new self; } /** * @param string $procId * @param int $time = 1000 * * @return string */ public static function listen($procId, int $time = 1000) : string { $var = 'socket' . uniqid(); $procData = ( is_scalar($procId) ? '"' . str_replace(self::$procDir, '', $procId) . '"' : Buffering\Callback::do($procId) ); $return = ' var ' . $var . ' = setInterval(function() { $.ajax ({ url: "' . Request::getSiteURL(self::$socketURI) . '", type: "post", dataType: "json", data: {procId: ' . $procData . '}, success: function(data) { ' . (self::$success ? Buffering\Callback::do(self::$success) : '') . ' if( data.status != "processing" ) { clearInterval(' . $var . '); } }, error: function(data) { ' . (self::$error ? Buffering\Callback::do(self::$error) : '') . ' } }) }, ' . $time . '); '; self::$success = ''; return $return; } /** * Status * * @param string ...$procIds * * @return array */ public static function status(string ...$procIds) : array { $pending = []; foreach( $procIds as $procId ) { $procFile = self::getProcFile($procId); if( is_file($procFile) ) { $pending[$procFile] = 1; # pending. } } return $pending; } /** * Is finish * * @param string ...$procIds * * @return bool */ public static function isFinish(string ...$procIds) : bool { return ! in_array(1, self::status(...$procIds)); } /** * Dispay Report * * @param string $errorFile * * @return string */ public static function displayError(string $errorFile) : string { if( is_file($file = self::$procDir . $errorFile) && ( $fileContent = file_get_contents($file) ) ) { $data = Json::decodeArray($fileContent); return Buffering\Callback::do(function() use($data) { Exceptions::table($data['code'] ?? NULL, $data['message'], $data['file'], $data['line'], $data['trace']); }); } return Errors::message('File not found!'); } /** * Loop * * @param int $count * @param int $waitSecond * @param callable $callback */ public static function loop(int $count, int $waitSecond, callable $callback) { $i = 1; while( true ) { $callback($i, $waitSecond); if( $i == $count ) { self::close(); } $i++; sleep($waitSecond); } } /** * Loop Every Hour * * @param callable $callback * @param bool $firstTrigger = true */ public static function loopEveryHour(callable $callback, bool $firstTrigger = true) { $check = false; while( true ) { if( ! $check ) { $minute = (int) date('i'); $second = (int) date('s'); $remaining = ((60 - $minute) * 60) - $second; } else { $remaining = 3600; } if( $firstTrigger === true ) { $callback(); } sleep($remaining); if( $firstTrigger === false ) { $callback(); } $check = true; } } /** * Loop Every Minute * * @param callable $callback * @param bool $firstTrigger = true */ public static function loopEveryMinute(callable $callback, bool $firstTrigger = true) { $check = false; while( true ) { if( ! $check ) { $second = (int) date('s'); $remaining = 60 - $second; } else { $remaining = 60; } if( $firstTrigger === true ) { $callback(); } sleep($remaining); if( $firstTrigger === false ) { $callback(); } $check = true; } } /** * Is Run * * @param string $procId = current-process * * @return bool */ public static function isRun(string $procId = '') : bool { if( $pid = self::getData($procId)['status']['pid'] ?? NULL ) { if( stripos(php_uname('s'), 'win') > -1 ) { $output = []; exec("tasklist /FI \"PID eq $pid\"", $output); foreach( $output as $line) { if( strpos($line, (string)$pid) !== false ) { return true; } } } else { exec("ps -p $pid", $output); if( count($output) > 1 ) { return true; } } } return false; } /** * protected get proc file */ protected static function getProcFile(string $procId = '') { return $procId ? Base::prefix($procId, self::$procDir) : self::$procId; } /** * protected close process */ protected static function closeProcess($pid) { return stripos(php_uname('s'), 'win') > -1 ? exec("taskkill /F /T /PID $pid") : exec("kill -9 $pid"); } }