mirror of
https://github.com/demostf/migrate.git
synced 2026-06-03 17:24:10 +02:00
initial version
This commit is contained in:
commit
8b64b8c31c
8 changed files with 463 additions and 0 deletions
40
src/Api.php
Normal file
40
src/Api.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\Migrate;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class Api {
|
||||
private $endpoint;
|
||||
private $client;
|
||||
|
||||
public function __construct($endpoint) {
|
||||
$this->endpoint = $endpoint;
|
||||
$this->client = new Client();
|
||||
}
|
||||
|
||||
public function listDemos(int $page = 0, string $order = 'DESC', $backend = ''): array {
|
||||
$url = $this->endpoint . '/demos?page=' . $page . '&order=' . $order . '&backend=' . $backend;
|
||||
$content = file_get_contents($url);
|
||||
$demos = json_decode($content, true);
|
||||
foreach ($demos as &$data) {
|
||||
$date = new \DateTime();
|
||||
$date->setTimestamp($data['time']);
|
||||
$data['time'] = $date;
|
||||
}
|
||||
return $demos;
|
||||
}
|
||||
|
||||
public function changeDemo(int $id, string $backend, string $path, string $url, string $hash, string $key): bool {
|
||||
$request = $this->client->post($this->endpoint . '/demos/' . $id . '/url', [
|
||||
'form_params' => [
|
||||
'hash' => $hash,
|
||||
'backend' => $backend,
|
||||
'url' => $url,
|
||||
'path' => $path,
|
||||
'key' => $key
|
||||
]
|
||||
]);
|
||||
return $request->getStatusCode() === 200;
|
||||
}
|
||||
}
|
||||
38
src/Migrate.php
Normal file
38
src/Migrate.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\Migrate;
|
||||
|
||||
class Migrate {
|
||||
private $api;
|
||||
private $store;
|
||||
private $backend;
|
||||
private $key;
|
||||
|
||||
public function __construct(Api $api, Store $store, string $backend, string $key) {
|
||||
$this->api = $api;
|
||||
$this->store = $store;
|
||||
$this->backend = $backend;
|
||||
$this->key = $key;
|
||||
}
|
||||
|
||||
public function migrateDemo(array $demo): bool {
|
||||
if (!$this->store->exists($demo['name'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$hash = $this->store->hash($demo['name']);
|
||||
|
||||
if ($hash !== $demo['hash']) {
|
||||
throw new \Exception('hash mismatch');
|
||||
}
|
||||
|
||||
return $this->api->changeDemo(
|
||||
$demo['id'],
|
||||
$this->backend,
|
||||
$this->store->generatePath($demo['name']),
|
||||
$this->store->generateUrl($demo['name']),
|
||||
$hash,
|
||||
$this->key
|
||||
);
|
||||
}
|
||||
}
|
||||
33
src/Store.php
Normal file
33
src/Store.php
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Demostf\Migrate;
|
||||
|
||||
class Store {
|
||||
private $baseDir;
|
||||
private $baseUrl;
|
||||
|
||||
public function __construct($baseDir, $baseUrl) {
|
||||
$this->baseDir = $baseDir;
|
||||
$this->baseUrl = $baseUrl;
|
||||
}
|
||||
|
||||
public function hash(string $name): string {
|
||||
return md5_file($this->generatePath($name));
|
||||
}
|
||||
|
||||
public function generatePath(string $name): string {
|
||||
return $this->baseDir . $this->getPrefix($name) . $name;
|
||||
}
|
||||
|
||||
private function getPrefix(string $name): string {
|
||||
return '/' . substr($name, 0, 2) . '/' . substr($name, 2, 2) . '/';
|
||||
}
|
||||
|
||||
public function exists(string $name): bool {
|
||||
return file_exists($this->generatePath($name));
|
||||
}
|
||||
|
||||
public function generateUrl(string $name) {
|
||||
return $this->baseUrl . $this->getPrefix($name) . $name;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue