- Clone this repository.
- Run
composer install
in project root. - Make
/data
folder writable.
- Request
GET /
for obtaining current fake version. - Request
PATCH /
for increasing current version.
{ | |
"require": { | |
"slim/slim": "^3.11", | |
"league/flysystem": "^1.0" | |
} | |
} |
<?php | |
use League\Flysystem\Adapter\Local; | |
use League\Flysystem\Filesystem; | |
use Slim\App; | |
use Slim\Http\Request; | |
use Slim\Http\Response; | |
require_once __DIR__ . '/vendor/autoload.php'; | |
final class Faker | |
{ | |
const DATA_DIRECTORY = '/data'; | |
const CURRENT_VERSION_FILE_NAME = 'current-version'; | |
/** @var Filesystem */ | |
private $fileSystem; | |
/** @var App */ | |
private $app; | |
public function __construct() | |
{ | |
$this->fileSystem = new Filesystem(new Local(__DIR__ . self::DATA_DIRECTORY)); | |
$this->app = new App(); | |
} | |
public function fake() | |
{ | |
$fileSystem = $this->fileSystem; | |
// get current version | |
$this->app->get('/', function (Request $request, Response $response, $args) use ($fileSystem) { | |
if ( ! $fileSystem->has(self::CURRENT_VERSION_FILE_NAME)) { | |
return $response->withStatus(500)->write('Current version is not available.'); | |
} | |
$currentVersion = $fileSystem->read(self::CURRENT_VERSION_FILE_NAME); | |
if ($currentVersion === '') { | |
return $response->withStatus(500)->write('Version sequence was not started. Request PATCH / first.'); | |
} | |
return $response->write('0.0.' . $currentVersion); | |
}); | |
// increase current version | |
$this->app->patch('/', function (Request $request, Response $response, $args) use ($fileSystem) { | |
if ( ! $fileSystem->has(self::CURRENT_VERSION_FILE_NAME)) { | |
$currentVersion = 1; | |
$fileSystem->write(self::CURRENT_VERSION_FILE_NAME, $currentVersion); | |
} else { | |
$currentVersion = $fileSystem->read(self::CURRENT_VERSION_FILE_NAME); | |
if ($currentVersion === '') { | |
$currentVersion = 0; | |
} | |
$currentVersion = ((int) $currentVersion) + 1; | |
$fileSystem->update(self::CURRENT_VERSION_FILE_NAME, $currentVersion); | |
} | |
return $response->write('0.0.' . $currentVersion); | |
}); | |
$this->app->run(); | |
} | |
} | |
(new Faker())->fake(); |