mirror of
https://codeberg.org/vlw/scaffold.git
synced 2025-11-05 06:02:42 +01:00
Compare commits
10 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 4fa45083d0 | |||
| 00337cd3aa | |||
| feea36b190 | |||
| b1bdf628f2 | |||
| 1940d89352 | |||
| 3cc4984b14 | |||
| 3c8c8561b2 | |||
| e1d3a09966 | |||
| dbdde9fcaf | |||
| 3357416ad0 |
7 changed files with 90 additions and 71 deletions
|
|
@ -9,3 +9,9 @@ Run the install scripts for [Vegvisir](https://vegvisir.vlw.se/install) and/or [
|
|||
```
|
||||
composer require vlw/scaffold
|
||||
```
|
||||
|
||||
Refer to the code comments in each file for documentation about its function. The base namespace is `vlw\Scaffold` and each class and function can be `use`d that way. For example:
|
||||
|
||||
```php
|
||||
use vlw\Scaffold\Database\Model;
|
||||
```
|
||||
|
|
@ -1,18 +1,12 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Read the documentation at:
|
||||
* https://codeberg.org/vlw/scaffold/docs/API/API.md
|
||||
*/
|
||||
|
||||
namespace vlw\Scaffold\API;
|
||||
|
||||
use Reflect\Path;
|
||||
use Reflect\Rules\Ruleset;
|
||||
|
||||
require_once Path::root("vegvisir/src/kernel/Init.php");
|
||||
require_once Path::root("vegvisir/src/request/VV.php");
|
||||
|
||||
/**
|
||||
* Wrapper for Reflect API endpoints
|
||||
*/
|
||||
class API {
|
||||
/**
|
||||
* Create a new API request with automatic request validation
|
||||
|
|
|
|||
|
|
@ -1,18 +1,16 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Read the documentation at:
|
||||
* https://codeberg.org/vlw/scaffold/docs/Database/Database.md
|
||||
*/
|
||||
|
||||
namespace vlw\Scaffold\Database;
|
||||
|
||||
use vlw\MySQL\MySQL;
|
||||
|
||||
use vlw\Scaffold\Database\Paginate;
|
||||
|
||||
require_once "Paginate.php";
|
||||
require_once dirname(__DIR__, 1) . "/Helpers/Paginate.php";
|
||||
|
||||
/**
|
||||
* Wrapper for vlw\MySQL which reads credentials from .env.ini in the project root.
|
||||
*/
|
||||
class Database extends MySQL {
|
||||
public const DATE_FORMAT = "Y-m-d";
|
||||
public const DATETIME_FORMAT = "Y-m-d H:i:s";
|
||||
|
|
|
|||
|
|
@ -1,16 +1,14 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Read the documentation at:
|
||||
* https://codeberg.org/vlw/scaffold/docs/Database/Model.md
|
||||
*/
|
||||
|
||||
namespace vlw\Scaffold\Database;
|
||||
|
||||
use vlw\Scaffold\Database\Database;
|
||||
|
||||
require_once "src/Database/Database.php";
|
||||
require_once "Database.php";
|
||||
|
||||
/**
|
||||
* Abstract reading, creating, and updating of database entities.
|
||||
*/
|
||||
abstract class Model {
|
||||
const DATE_FORMAT = Database::DATE_FORMAT;
|
||||
|
||||
|
|
@ -57,7 +55,7 @@
|
|||
|
||||
$this->_resolved = true;
|
||||
$this->_row = $this->db
|
||||
->for($this->table)
|
||||
->from($this->table)
|
||||
->where($this->where)
|
||||
->limit(1)
|
||||
->select($this->columns)
|
||||
|
|
@ -74,14 +72,13 @@
|
|||
*
|
||||
* @return bool Entity exists
|
||||
*/
|
||||
public bool $isset {
|
||||
// Returns bool if row is set or attempts to resolve and set if null
|
||||
get => $this->_isset ??= new Database()
|
||||
->for($this->table)
|
||||
->where($this->where)
|
||||
->limit(1)
|
||||
->select()
|
||||
->num_rows === 1;
|
||||
public function isset(): bool {
|
||||
return $this->_isset ??= new Database()
|
||||
->from($this->table)
|
||||
->where($this->where)
|
||||
->limit(1)
|
||||
->select()
|
||||
->num_rows === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -105,7 +102,7 @@
|
|||
$this->_row[$key] = $value;
|
||||
|
||||
return $this->db
|
||||
->for($this->table)
|
||||
->from($this->table)
|
||||
->where($this->where)
|
||||
->update([$key => $value]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace vlw\Scaffold\Helpers;
|
||||
|
||||
/**
|
||||
* Read the documentation at:
|
||||
* https://codeberg.org/vlw/scaffold/docs/Database/Paginate.md
|
||||
* Paginate items of any kind. This class is deisgned to work well with
|
||||
* vlw\MySQL and the Scaffold\Database\Database class.
|
||||
*/
|
||||
|
||||
namespace vlw\Scaffold\Helpers\Paginate;
|
||||
|
||||
class Paginate {
|
||||
public const KEY = "p";
|
||||
public const DEFAULT_RANGE = 3;
|
||||
|
|
|
|||
|
|
@ -1,41 +1,43 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* Read the documentation at:
|
||||
* https://codeberg.org/vlw/scaffold/docs/Helpers/UUID.md
|
||||
*/
|
||||
|
||||
namespace vlw\Scaffold\Helpers\UUID;
|
||||
namespace vlw\Scaffold\Helpers;
|
||||
|
||||
/**
|
||||
* Generate an all binary 0:s UUID
|
||||
*
|
||||
* @return string
|
||||
* Generate Universally unique identifiers
|
||||
*/
|
||||
function uuid_nil(): string {
|
||||
return "00000000-0000-0000-0000-000000000000";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an all binary 1:s UUID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function uuid_max(): string {
|
||||
return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a version 4 UUID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
function uuid_v4(): string {
|
||||
return sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
class UUID {
|
||||
public const LENGTH = 36;
|
||||
|
||||
/**
|
||||
* Generate an all binary 0:s UUID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function nil(): string {
|
||||
return "00000000-0000-0000-0000-000000000000";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate an all binary 1:s UUID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function max(): string {
|
||||
return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a version 4 UUID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function v4(): string {
|
||||
return sprintf("%04x%04x-%04x-%04x-%04x-%04x%04x%04x",
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0xffff),
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
23
src/Scaffold.php
Normal file
23
src/Scaffold.php
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
namespace vlw\Scaffold;
|
||||
|
||||
use VV;
|
||||
use Reflect\Path;
|
||||
|
||||
class Scaffold {
|
||||
/**
|
||||
* Include a source file
|
||||
*
|
||||
* @param string $pathname Relative path from project root to a source file
|
||||
*/
|
||||
public static function load(string $pathname): void {
|
||||
// Load the Vegvisir VV class from Reflect
|
||||
if (!class_exists("VV")) {
|
||||
require_once Path::root("vegvisir/src/kernel/Init.php");
|
||||
require_once Path::root("vegvisir/src/request/VV.php");
|
||||
}
|
||||
|
||||
require_once VV::root($pathname);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue