Skip to content

Commit

Permalink
Almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
devtronic committed Nov 1, 2022
1 parent 24b9dc0 commit 79d9048
Show file tree
Hide file tree
Showing 10 changed files with 542 additions and 11 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Julian Finkler <julian@mintware.de>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
74 changes: 74 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Native Cron

This package helps you to manage and modify your native *nix crontabs.

## Features

- View / add / edit / delete system cron jobs
- View / add / edit / delete drop in cron jobs
- View / add / edit / delete user cron jobs
- Fully tested (except the adapter for native file system functions)
- Flexible / you can easily replace any important part of the package

## Installation
```bash
composer require mintware-de/native-cron
```

## Usage
```php
<?php

declare(strict_types=1);

use MintwareDe\NativeCron\Content\BlankLine;
use MintwareDe\NativeCron\Content\CommentLine;
use MintwareDe\NativeCron\Content\CronJobLine;
use MintwareDe\NativeCron\CrontabManager;
use MintwareDe\NativeCron\Filesystem\DarwinCrontabFileLocator;
use MintwareDe\NativeCron\Filesystem\FileHandler;

require_once __DIR__.'/vendor/autoload.php';

// Create a new crontab manager
$manager = new CrontabManager(
// with a file locator for macOS; Use DebianCrontabFileLocator for debian based distros
new DarwinCrontabFileLocator(),

// and a default file handler
new FileHandler(),
);

// Read the crontab for the user max
$crontab = $manager->readUserCrontab('max');

// Display the current content of the crontab
echo $crontab->build();

$crontab
// Add a new cronjob
->add(new CronJobLine('* * * * * echo "Hello World" >> /tmp/mylog'))
// Remove all comments and blank lines
->removeWhere(fn ($line) => $line instanceof CommentLine || $line instanceof BlankLine);

// Display the new content of the crontab
echo $crontab->build();

// Write the crontab for the user max
// Keep in mind that reading or writing crontab files may require higher user privileges.
$manager->writeUserCrontab($crontab, 'max');
```

## Compatibility matrix

| Feature | Linux | macOS | Win |
|-------------------|:-----:|:-----:|-----|
| System Cron jobs | Yes | Yes | No |
| User Cron jobs | Yes | Yes | No |
| Drop-In Cron jobs | Yes | No | No |


## Supported platforms
At the moment are Debian based distros and macOS supported.
If you need to add support for a different platform, take a look at the [CrontabFileLocatorInterface](./src/Filesystem/CrontabFileLocatorInterface.php) and implement it for your platform.

8 changes: 1 addition & 7 deletions src/Crontab.php → src/Content/Crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

declare(strict_types=1);

namespace MintwareDe\NativeCron;

use MintwareDe\NativeCron\Content\BlankLine;
use MintwareDe\NativeCron\Content\CommentLine;
use MintwareDe\NativeCron\Content\CronJobLine;
use MintwareDe\NativeCron\Content\CrontabLineInterface;
use MintwareDe\NativeCron\Content\EnvironmentSetting;
namespace MintwareDe\NativeCron\Content;

class Crontab
{
Expand Down
114 changes: 114 additions & 0 deletions src/CrontabManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace MintwareDe\NativeCron;

use MintwareDe\NativeCron\Content\Crontab;
use MintwareDe\NativeCron\Filesystem\CrontabFileLocatorInterface;
use MintwareDe\NativeCron\Filesystem\FileHandlerInterface;

class CrontabManager
{
public function __construct(
private readonly CrontabFileLocatorInterface $fileLocator,
private readonly FileHandlerInterface $fileHandler,
) {
}

/**
* Reads the system-wide crontab file.
*
* @return Crontab
*/
public function readSystemCrontab(): Crontab
{
$crontabFile = $this->fileLocator->locateSystemCrontab();

return $this->readCrontabInternal($crontabFile, true);
}

/**
* Reads the crontab file for a user.
*
* @param string $username The username of the user
*
* @return Crontab
*/
public function readUserCrontab(string $username): Crontab
{
$crontabFile = $this->fileLocator->locateUserCrontab($username);

return $this->readCrontabInternal($crontabFile, false);
}

/**
* Reads a drop-in crontab file.
*
* @param string $name The name of the drop-in
*
* @return Crontab
*/
public function readDropInCrontab(string $name): Crontab
{
$crontabFile = $this->fileLocator->locateDropInCrontab($name);

return $this->readCrontabInternal($crontabFile, true);
}

private function readCrontabInternal(string $crontabFile, bool $isSystemCrontab): Crontab
{
$crontabContent = $this->fileHandler->read($crontabFile);

$crontab = new Crontab($isSystemCrontab);
if ($crontabContent !== null) {
$crontab->parse($crontabContent);
}

return $crontab;
}

public function writeSystemCrontab(Crontab $crontab): void
{
$crontabFile = $this->fileLocator->locateSystemCrontab();

$this->writeCrontabFileInternal($crontabFile, $crontab, 'root', true);
}

public function writeDropInCrontab(Crontab $crontab, string $name): void
{
$crontabFile = $this->fileLocator->locateDropInCrontab($name);

$this->writeCrontabFileInternal($crontabFile, $crontab, 'root', true);
}

public function writeUserCrontab(Crontab $crontab, string $username): void
{
$crontabFile = $this->fileLocator->locateUserCrontab($username);

$this->writeCrontabFileInternal($crontabFile, $crontab, $username, false);
}

private function writeCrontabFileInternal(
string $crontabFile,
Crontab $crontab,
string $owner,
bool $isSystemFile
): void {
$isSystemCrontab = $crontab->isSystemCrontab();
if ($isSystemCrontab != $isSystemFile) {
if ($isSystemCrontab) {
throw new \RuntimeException('The crontab is a system crontab.');
} else {
throw new \RuntimeException('The crontab is not a system crontab.');
}
}

$this->fileHandler->createFile($crontabFile);
$this->fileHandler->setPermissions($crontabFile, 0600);
$this->fileHandler->setOwner($crontabFile, $owner);

$crontabContent = $crontab->build();
$this->fileHandler->write($crontabFile, $crontabContent);
}
}
1 change: 0 additions & 1 deletion src/Filesystem/DarwinCrontabFileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

class DarwinCrontabFileLocator implements CrontabFileLocatorInterface
{

public function locateDropInCrontab(string $name): string
{
throw new \RuntimeException('This platform does not support drop-in cron tabs');
Expand Down
58 changes: 58 additions & 0 deletions src/Filesystem/FileHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace MintwareDe\NativeCron\Filesystem;

class FileHandler implements FileHandlerInterface
{
public function createFile(string $filename): void
{
if (is_file($filename)) {
return;
}

$dir = dirname($filename);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
touch($filename);
}

public function setPermissions(string $filename, int $permissions): void
{
chmod($filename, $permissions);
}

public function setOwner(string $filename, string $owner): void
{
chown($filename, $owner);
}

public function read(string $filename): ?string
{
if (!is_file($filename)) {
return null;
}

if (!is_readable($filename)) {
throw new \RuntimeException('The file is not readable.');
}

$content = file_get_contents($filename);
if ($content === false) {
throw new \RuntimeException('Error while reading the file.');
}

return $content;
}

public function write(string $filename, string $contents): void
{
if (!is_writable($filename)) {
throw new \RuntimeException('The file is not writable.');
}

file_put_contents($filename, $contents);
}
}
18 changes: 18 additions & 0 deletions src/Filesystem/FileHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace MintwareDe\NativeCron\Filesystem;

interface FileHandlerInterface
{
public function createFile(string $filename): void;

public function setPermissions(string $filename, int $permissions): void;

public function setOwner(string $filename, string $owner): void;

public function read(string $filename): ?string;

public function write(string $filename, string $contents): void;
}
4 changes: 2 additions & 2 deletions tests/CrontabTest.php → tests/Content/CrontabTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

declare(strict_types=1);

namespace MintwareDe\NativeCron\Tests;
namespace MintwareDe\NativeCron\Tests\Content;

use MintwareDe\NativeCron\Content\BlankLine;
use MintwareDe\NativeCron\Content\CommentLine;
use MintwareDe\NativeCron\Content\CronJobLine;
use MintwareDe\NativeCron\Content\Crontab;
use MintwareDe\NativeCron\Content\CrontabLineInterface;
use MintwareDe\NativeCron\Crontab;
use PHPUnit\Framework\TestCase;

class CrontabTest extends TestCase
Expand Down
Loading

0 comments on commit 79d9048

Please sign in to comment.