This commit is contained in:
2021-06-28 23:15:13 -04:00
parent 0061a3d920
commit f4a8db56ff
93 changed files with 2422 additions and 0 deletions

20
provm/Migrator.md Normal file
View File

@ -0,0 +1,20 @@
# Migrator
## Needs
+ To detect changes in migration schema and create migrations with Phinx
+ Keep track of incremental changes in schema
+ Create migration for Phinx
## Usage
Check for changes:
<code>vendor/bin/migrator check</code>
Create migrations:
<code>vendor/bin/migrator create</code>
Check the create migrations and then:
<code>vendor/bin/migrator migrate</code>
or
<code>vendor/bin/phinx migrate</code>

View File

@ -0,0 +1,13 @@
<?php
namespace ProVM\Common\Define\Controller;
use Psr\Http\Message\ResponseInterface as Response;
trait JSON {
public function withJson(Response $response, $data, $status_code = 200) {
$response->getBody()->write(json_encode($data));
return $response
->withHeader('Content-Type', 'application/json')
->withStatus($status_code);
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait Date {
public function date(\DateTime $date = null) {
if ($date === null) {
return Carbon::parse($this->date);
}
$this->date = $date->format('Y-m-d');
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait DateTime {
public function dateTime(\DateTime $date_time = null) {
if ($date_time === null) {
return Carbon::parse($this->date_time);
}
$this->date_time = $date_time->format('Y-m-d H:i:s');
}
}

View File

@ -0,0 +1,13 @@
<?php
namespace ProVM\Common\Define\Model;
use Carbon\Carbon;
trait Time {
public function date(\DateTime $time = null) {
if ($time === null) {
return Carbon::parse($this->time);
}
$this->time = $time->format('H:i:s');
}
}

View File

@ -0,0 +1,23 @@
<?php
namespace ProVM\Common\Middleware;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface as Handler;
use Psr\Http\Message\ResponseInterface as Response;
use Nyholm\Psr7\Factory\Psr17Factory;
class Auth {
protected $factory;
public function __construct(Psr17Factory $factory) {
$this->factory = $factory;
}
public function __invoke(Request $request, Handler $handler): Response {
$response = $handler->handle($request);
if ($this->service->isLoggedIn()) {
return $response;
}
$content = $response->getBody();
$response = $factory->createResponse(200)->withBody($content);
return $response;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace ProVM\Common\Service;
use \ORM;
use \Model;
use ProVM\Common\Service\Database\MySQL;
class Database {
protected $settings;
public function __construct($settings) {
$this->settings = $settings;
}
public function load() {
foreach ($this->settings->dbs as $name => $data) {
switch (strtolower($data->engine)) {
case 'mysql':
$obj = new MySQL($data);
break;
}
ORM::configure($obj->dsn(), null, $name);
if ($obj->hasUser()) {
ORM::configure('username', $data->user->name, $name);
ORM::configure('password', $data->user->password, $name);
}
}
if (isset($this->settings->short_names)) {
Model::$short_table_names = $this->settings->short_names;
}
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace ProVM\Common\Service\Database;
class DSN {
public $engine;
public function __construct(string $engine) {
$this->engine = $engine;
}
public $pairs;
public function addPair($name, $value) {
if ($this->pairs === null) {
$this->pairs = [];
}
$this->pairs []= [$name, $value];
return $this;
}
public function __toString() {
return implode(':', [
$this->engine,
implode(';', array_map(function($item) {
return implode('=', $item);
}, $this->pairs))
]);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace ProVM\Common\Service\Database;
class MySQL {
protected $settings;
public function __construct($settings) {
$this->settings = $settings;
}
public function dsn(): string {
$dsn = (new DSN($this->settings->engine))
->addPair('host', $this->settings->host->name);
if (isset($this->settings->host->port)) {
$dsn->addPair('port', $this->settings->host->port);
}
$dsn->addPair('dbname', $this->settings->name);
return '' . $dsn;
}
public function hasUser(): bool {
return true;
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace ProVM\Common\Service;
class Migrator {
protected $schema_filename;
protected $migrations_folder;
protected $seeds_folder;
public function __construct(string $schema_filename, string $migrations_folder, string $seeds_folder) {
$this->schema_filename = $schema_filename;
$this->migrations_folder = $migrations_folder;
$this->seeds_folder = $seeds_folder;
}
protected $schema;
public function schema() {
if ($this->schema === null) {
$file = new \File($this->schema_filename);
}
}
}

22
provm/src/Login.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace ProVM;
use ProVM\Common\Alias\Model;
use ProVM\Common\Define\Model\DateTime as DT;
/**
* @property User $user_id
* @property \DateTime $date_time
* @property string $token
*/
class Login extends Model {
use DT;
protected $user;
public function user() {
if ($this->user === null) {
$this->user = $this->childOf(User::class, [Model::SELF_KEY => 'user_id']);
}
return $this->user;
}
}

15
provm/src/User.php Normal file
View File

@ -0,0 +1,15 @@
<?php
namespace ProVM;
use ProVM\Common\Alias\Model;
/**
* @property int $id
* @property string $name
* @property string $password
*/
class User extends Model {
public function checkPassword(string $password): bool {
return password_verify($this->password, $password);
}
}