-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
542 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.