diff --git a/Utils/TaskSchedule/Cron.php b/Utils/TaskSchedule/Cron.php index 8279c7f63..c0f333e89 100644 --- a/Utils/TaskSchedule/Cron.php +++ b/Utils/TaskSchedule/Cron.php @@ -26,48 +26,6 @@ namespace phpOMS\Utils\TaskSchedule; class Cron extends SchedulerAbstract { - /** - * Run command - * - * @param string $cmd Command to run - * - * @return string - * - * @throws \Exception - * - * @since 1.0.0 - */ - private function run(string $cmd) : string - { - $cmd = 'cd ' . \escapeshellarg(\dirname(self::$bin)) . ' && ' . \basename(self::$bin) . ' ' . $cmd; - - $pipes = []; - $desc = [ - 1 => ['pipe', 'w'], - 2 => ['pipe', 'w'], - ]; - - $resource = \proc_open($cmd, $desc, $pipes, __DIR__, null); - $stdout = \stream_get_contents($pipes[1]); - $stderr = \stream_get_contents($pipes[2]); - - foreach ($pipes as $pipe) { - \fclose($pipe); - } - - if ($resource === false) { - throw new \Exception(); - } - - $status = \proc_close($resource); - - if ($status == -1) { - throw new \Exception($stderr); - } - - return trim($stdout); - } - /** * Normalize run result for easier parsing * diff --git a/Utils/TaskSchedule/SchedulerAbstract.php b/Utils/TaskSchedule/SchedulerAbstract.php index cd7ed7436..19647a3c2 100644 --- a/Utils/TaskSchedule/SchedulerAbstract.php +++ b/Utils/TaskSchedule/SchedulerAbstract.php @@ -61,11 +61,42 @@ abstract class SchedulerAbstract */ public static function setBin(string $path) : void { - if (realpath($path) === false) { + if (\realpath($path) === false) { throw new PathException($path); } - self::$bin = realpath($path); + self::$bin = \realpath($path); + } + + /** + * Gues git binary. + * + * @return bool + * + * @since 1.0.0 + */ + public static function guessBin() : bool + { + $paths = [ + 'c:/WINDOWS/system32/schtasks.exe', + 'd:/WINDOWS/system32/schtasks.exe', + 'e:/WINDOWS/system32/schtasks.exe', + 'f:/WINDOWS/system32/schtasks.exe', + '/usr/bin/crontab', + '/usr/sbin/crontab', + '/bin/crontab', + '/sbin/crontab', + ]; + + foreach ($paths as $path) { + if (\file_exists($path)) { + self::setBin($path); + + return true; + } + } + + return false; } /** @@ -79,16 +110,62 @@ abstract class SchedulerAbstract public static function test() : bool { $pipes = []; - $resource = proc_open(escapeshellarg(self::$bin), [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); + $resource = \proc_open(\escapeshellarg(self::$bin), [1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); - $stdout = stream_get_contents($pipes[1]); - $stderr = stream_get_contents($pipes[2]); - - foreach ($pipes as $pipe) { - fclose($pipe); + if ($resource === false) { + return false; } - return trim(proc_close($resource)) !== 127; + $stdout = \stream_get_contents($pipes[1]); + $stderr = \stream_get_contents($pipes[2]); + + foreach ($pipes as $pipe) { + \fclose($pipe); + } + + return \proc_close($resource) !== 127; + } + + /** + * Run command + * + * @param string $cmd Command to run + * + * @return string + * + * @throws \Exception + * + * @since 1.0.0 + */ + protected function run(string $cmd) : string + { + $cmd = 'cd ' . \escapeshellarg(\dirname(self::$bin)) . ' && ' . \basename(self::$bin) . ' ' . $cmd; + + $pipes = []; + $desc = [ + 1 => ['pipe', 'w'], + 2 => ['pipe', 'w'], + ]; + + $resource = \proc_open($cmd, $desc, $pipes, __DIR__, null); + if ($resource === false) { + return ''; + } + + $stdout = \stream_get_contents($pipes[1]); + $stderr = \stream_get_contents($pipes[2]); + + foreach ($pipes as $pipe) { + \fclose($pipe); + } + + $status = \proc_close($resource); + + if ($status === -1) { + throw new \Exception($stderr); + } + + return trim($stdout); } /** diff --git a/Utils/TaskSchedule/TaskScheduler.php b/Utils/TaskSchedule/TaskScheduler.php index f662541c3..bc0544237 100644 --- a/Utils/TaskSchedule/TaskScheduler.php +++ b/Utils/TaskSchedule/TaskScheduler.php @@ -25,48 +25,6 @@ namespace phpOMS\Utils\TaskSchedule; */ class TaskScheduler extends SchedulerAbstract { - /** - * Run command - * - * @param string $cmd Command to run - * - * @return string - * - * @throws \Exception - * - * @since 1.0.0 - */ - private function run(string $cmd) : string - { - $cmd = 'cd ' . \escapeshellarg(\dirname(self::$bin)) . ' && ' . \basename(self::$bin) . ' ' . $cmd; - - $pipes = []; - $desc = [ - 1 => ['pipe', 'w'], - 2 => ['pipe', 'w'], - ]; - - $resource = \proc_open($cmd, $desc, $pipes, __DIR__, null); - if ($resource === false) { - return ''; - } - - $stdout = \stream_get_contents($pipes[1]); - $stderr = \stream_get_contents($pipes[2]); - - foreach ($pipes as $pipe) { - \fclose($pipe); - } - - $status = \proc_close($resource); - - if ($status == -1) { - throw new \Exception($stderr); - } - - return trim($stdout); - } - /** * Normalize run result for easier parsing *