diff --git a/Utils/TaskSchedule/Cron.php b/Utils/TaskSchedule/Cron.php index 082888d67..424166596 100644 --- a/Utils/TaskSchedule/Cron.php +++ b/Utils/TaskSchedule/Cron.php @@ -25,6 +25,74 @@ namespace phpOMS\Utils\TaskSchedule; */ class Cron extends SchedulerAbstract { + /** + * {@inheritdoc} + */ + public function create(TaskAbstract $task) : void + { + $this->run('-l > tmpcron'); + \file_put_contents('tmpcron', "\n" . $task->__toString(), FILE_APPEND); + $this->run('tmpcron'); + } + + /** + * {@inheritdoc} + */ + public function update(TaskAbstract $task) : void + { + $this->run('-l > tmpcron'); + + $new = ''; + $fp = \fopen('tmpcron', 'r+'); + + if ($fp) { + $line = \fgets($fp); + while ($line !== false) { + if ($line[0] !== '#' && \stripos($line, '/tn = ' . $task->getId()) !== false) { + $new .= $task->__toString(); + } else { + $new .= $line . "\n"; + } + + $line = \fgets($fp); + } + + \fwrite($fp, $new); + \fclose($fp); + } + + $this->run('tmpcron'); + } + + /** + * {@inheritdoc} + */ + public function delete(TaskAbstract $task) : void + { + $this->run('-l > tmpcron'); + + $new = ''; + $fp = \fopen('tmpcron', 'r+'); + + if ($fp) { + $line = \fgets($fp); + while ($line !== false) { + if ($line[0] !== '#' && \stripos($line, '/tn = ' . $task->getId()) !== false) { + $line = \fgets($fp); + continue; + } + + $new .= $line . "\n"; + $line = \fgets($fp); + } + + \fwrite($fp, $new); + \fclose($fp); + } + + $this->run('tmpcron'); + } + /** * {@inheritdoc} */ diff --git a/Utils/TaskSchedule/CronJob.php b/Utils/TaskSchedule/CronJob.php index 821e2e14e..8a825b058 100644 --- a/Utils/TaskSchedule/CronJob.php +++ b/Utils/TaskSchedule/CronJob.php @@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime; */ class CronJob extends TaskAbstract { + /** + * {@inheritdoc} + */ + public function __toString() : string + { + return ''; + } + /** * {@inheritdoc} */ diff --git a/Utils/TaskSchedule/Schedule.php b/Utils/TaskSchedule/Schedule.php index 380afd9cd..c98469305 100644 --- a/Utils/TaskSchedule/Schedule.php +++ b/Utils/TaskSchedule/Schedule.php @@ -26,6 +26,14 @@ use phpOMS\Validation\Base\DateTime; */ class Schedule extends TaskAbstract { + /** + * {@inheritdoc} + */ + public function __toString() : string + { + return ''; + } + /** * {@inheritdoc} */ diff --git a/Utils/TaskSchedule/SchedulerAbstract.php b/Utils/TaskSchedule/SchedulerAbstract.php index 2237e10ec..4873fd18e 100644 --- a/Utils/TaskSchedule/SchedulerAbstract.php +++ b/Utils/TaskSchedule/SchedulerAbstract.php @@ -177,10 +177,7 @@ abstract class SchedulerAbstract * * @since 1.0.0 */ - public function create(TaskAbstract $task) : void - { - $this->run($task->getCommand()); - } + abstract public function create(TaskAbstract $task) : void; /** * Update task diff --git a/Utils/TaskSchedule/TaskAbstract.php b/Utils/TaskSchedule/TaskAbstract.php index 76df199b4..b809b4878 100644 --- a/Utils/TaskSchedule/TaskAbstract.php +++ b/Utils/TaskSchedule/TaskAbstract.php @@ -111,6 +111,15 @@ abstract class TaskAbstract return $this->id; } + /** + * Stringify task for direct handling + * + * @return string + * + * @since 1.0.0 + */ + abstract public function __toString() : string; + /** * Get command to create the task * diff --git a/Utils/TaskSchedule/TaskScheduler.php b/Utils/TaskSchedule/TaskScheduler.php index a57ffc8ae..4248d738a 100644 --- a/Utils/TaskSchedule/TaskScheduler.php +++ b/Utils/TaskSchedule/TaskScheduler.php @@ -25,6 +25,30 @@ namespace phpOMS\Utils\TaskSchedule; */ class TaskScheduler extends SchedulerAbstract { + /** + * {@inheritdoc} + */ + public function create(TaskAbstract $task) : void + { + $this->run('/Create ' . $task->__toString()); + } + + /** + * {@inheritdoc} + */ + public function update(TaskAbstract $task) : void + { + $this->run('/Change ' . $task->__toString()); + } + + /** + * {@inheritdoc} + */ + public function delete(TaskAbstract $task) : void + { + $this->run('/Delete /TN ' . $task->getId()); + } + /** * {@inheritdoc} */ diff --git a/tests/Utils/TaskSchedule/CronTest.php b/tests/Utils/TaskSchedule/CronTest.php index ee7035a95..7e2dc5c1c 100644 --- a/tests/Utils/TaskSchedule/CronTest.php +++ b/tests/Utils/TaskSchedule/CronTest.php @@ -25,23 +25,25 @@ class CronTest extends \PHPUnit\Framework\TestCase public function testCRUD() { - $cron = new Cron(); + if (\stristr(PHP_OS, 'LINUX')) { + $cron = new Cron(); - self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false)); - - $cron->create( - new CronJob('testCronJob', 'testFile') - ); - self::assertEquals('testFile', $cron->getRun()); + self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false)); + + $cron->create( + new CronJob('testCronJob', 'testFile') + ); + self::assertEquals('testFile', $cron->getRun()); - $cron->update( - new CronJob('testCronJob', 'testFile2') - ); - self::assertEquals('testFile2', $cron->getRun()); + $cron->update( + new CronJob('testCronJob', 'testFile2') + ); + self::assertEquals('testFile2', $cron->getRun()); - $cron->delete( - new CronJob('testCronJob', 'testFile2') - ); - self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false)); + $cron->delete( + new CronJob('testCronJob', 'testFile2') + ); + self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false)); + } } } diff --git a/tests/Utils/TaskSchedule/TaskSchedulerTest.php b/tests/Utils/TaskSchedule/TaskSchedulerTest.php index 187ff86a0..6de0a9c16 100644 --- a/tests/Utils/TaskSchedule/TaskSchedulerTest.php +++ b/tests/Utils/TaskSchedule/TaskSchedulerTest.php @@ -21,4 +21,28 @@ class TaskSchedulerTest extends \PHPUnit\Framework\TestCase { self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\SchedulerAbstract', new TaskScheduler()); } + + public function testCRUD() + { + if (\stristr(PHP_OS, 'WIN')) { + $cron = new Cron(); + + self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false)); + + $cron->create( + new CronJob('testCronJob', 'testFile') + ); + self::assertEquals('testFile', $cron->getRun()); + + $cron->update( + new CronJob('testCronJob', 'testFile2') + ); + self::assertEquals('testFile2', $cron->getRun()); + + $cron->delete( + new CronJob('testCronJob', 'testFile2') + ); + self::assertInstanceOf('\phpOMS\Utils\TaskSchedule\NullCronJob', $cron->getAllByName('testCronJob', false)); + } + } }