Compare commits

..

No commits in common. "master" and "1.0.1" have entirely different histories.

7 changed files with 68 additions and 87 deletions

View file

@ -8,10 +8,4 @@ Run the install scripts for [Vegvisir](https://vegvisir.vlw.se/install) and/or [
``` ```
composer require vlw/scaffold 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;
``` ```

View file

@ -1,12 +1,18 @@
<?php <?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/API/API.md
*/
namespace vlw\Scaffold\API; namespace vlw\Scaffold\API;
use Reflect\Path;
use Reflect\Rules\Ruleset; use Reflect\Rules\Ruleset;
/** require_once Path::root("vegvisir/src/kernel/Init.php");
* Wrapper for Reflect API endpoints require_once Path::root("vegvisir/src/request/VV.php");
*/
class API { class API {
/** /**
* Create a new API request with automatic request validation * Create a new API request with automatic request validation

View file

@ -1,5 +1,10 @@
<?php <?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/Database/Database.md
*/
namespace vlw\Scaffold\Database; namespace vlw\Scaffold\Database;
use vlw\MySQL\MySQL; use vlw\MySQL\MySQL;
@ -8,9 +13,6 @@
require_once dirname(__DIR__, 1) . "/Helpers/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 { class Database extends MySQL {
public const DATE_FORMAT = "Y-m-d"; public const DATE_FORMAT = "Y-m-d";
public const DATETIME_FORMAT = "Y-m-d H:i:s"; public const DATETIME_FORMAT = "Y-m-d H:i:s";

View file

@ -1,14 +1,16 @@
<?php <?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/Database/Model.md
*/
namespace vlw\Scaffold\Database; namespace vlw\Scaffold\Database;
use vlw\Scaffold\Database\Database; use vlw\Scaffold\Database\Database;
require_once "Database.php"; require_once "Database.php";
/**
* Abstract reading, creating, and updating of database entities.
*/
abstract class Model { abstract class Model {
const DATE_FORMAT = Database::DATE_FORMAT; const DATE_FORMAT = Database::DATE_FORMAT;
@ -55,7 +57,7 @@
$this->_resolved = true; $this->_resolved = true;
$this->_row = $this->db $this->_row = $this->db
->from($this->table) ->for($this->table)
->where($this->where) ->where($this->where)
->limit(1) ->limit(1)
->select($this->columns) ->select($this->columns)
@ -72,13 +74,14 @@
* *
* @return bool Entity exists * @return bool Entity exists
*/ */
public function isset(): bool { public bool $isset {
return $this->_isset ??= new Database() // Returns bool if row is set or attempts to resolve and set if null
->from($this->table) get => $this->_isset ??= new Database()
->where($this->where) ->for($this->table)
->limit(1) ->where($this->where)
->select() ->limit(1)
->num_rows === 1; ->select()
->num_rows === 1;
} }
/** /**
@ -102,7 +105,7 @@
$this->_row[$key] = $value; $this->_row[$key] = $value;
return $this->db return $this->db
->from($this->table) ->for($this->table)
->where($this->where) ->where($this->where)
->update([$key => $value]); ->update([$key => $value]);
} }

View file

@ -1,11 +1,12 @@
<?php <?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/Database/Paginate.md
*/
namespace vlw\Scaffold\Helpers; namespace vlw\Scaffold\Helpers;
/**
* Paginate items of any kind. This class is deisgned to work well with
* vlw\MySQL and the Scaffold\Database\Database class.
*/
class Paginate { class Paginate {
public const KEY = "p"; public const KEY = "p";
public const DEFAULT_RANGE = 3; public const DEFAULT_RANGE = 3;

View file

@ -1,43 +1,41 @@
<?php <?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/Helpers/UUID.md
*/
namespace vlw\Scaffold\Helpers; namespace vlw\Scaffold\Helpers;
/** /**
* Generate Universally unique identifiers * Generate an all binary 0:s UUID
*
* @return string
*/ */
class UUID { function uuid_nil(): string {
public const LENGTH = 36; return "00000000-0000-0000-0000-000000000000";
}
/**
* Generate an all binary 0:s UUID /**
* * Generate an all binary 1:s UUID
* @return string *
*/ * @return string
public static function nil(): string { */
return "00000000-0000-0000-0000-000000000000"; function uuid_max(): string {
} return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF";
}
/**
* Generate an all binary 1:s UUID /**
* * Generate a version 4 UUID
* @return string *
*/ * @return string
public static function max(): string { */
return "FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF"; 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),
* Generate a version 4 UUID mt_rand(0, 0x0fff) | 0x4000,
* mt_rand(0, 0x3fff) | 0x8000,
* @return string mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
*/ );
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)
);
}
} }

View file

@ -1,23 +0,0 @@
<?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);
}
}