Compare commits

...

8 commits

Author SHA1 Message Date
4fa45083d0 feat: expose UUID helper as a class of static methods (#10)
Let's expose the uuid helpers as a class of static methods instead. I'm not sure if this approach is actually better but we're going to live with it for a while. I have done UUID helpers before this library as a class. So let's run with it again.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/10
2025-11-02 09:50:03 +01:00
00337cd3aa refactor: replace the Model->isset property with a public instance method (#9)
This change is primarily to accommodate the new `Reflect::serialize()` method since we don't want to include the isset property when returning serialized objects.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/9
2025-11-01 18:34:48 +01:00
feea36b190 feat: add UUID string length constant (#8)
Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/8
2025-11-01 16:48:38 +01:00
b1bdf628f2 refactor: replace for() with from() in database Model (#7)
This PR replaces the `vlw\MySQL->for()` with `vlw\MySQL->from()` since for() it's deprecated.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/7
2025-11-01 16:37:26 +01:00
1940d89352 refactor: add class for crosstalk between Vegvisir and Reflect (#4)
Restores the class method approach for loading source files from #2, which undoes the changes introduced in #3. I can't figure out a way to make the function load before initialization of downstream classes.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/4
2025-11-01 16:27:17 +01:00
3cc4984b14 docs: replace docs links with comments (#5)
Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/5
2025-11-01 16:27:01 +01:00
vlw
3c8c8561b2 docs: add note about documentation in README (#6)
Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/6
Co-authored-by: vlw <victor@vlw.se>
Co-committed-by: vlw <victor@vlw.se>
2025-11-01 16:26:41 +01:00
e1d3a09966 refactor: include Vegvisir source files from Reflect as a function (#3)
I know we just merged #2 and released verison 1.1.0 where the Vegvisir/Reflect crosstalk was supposed to be a class. But after using this briefly, I feel like it gets too verbose. This function will be called _a lot_ and therefore I think it should be pretty quick to type - just like the Vegvisir `VV` class.

Instead of typing
```php
use vlw\Scaffold\Scaffold;

Scaffold::include("some/file.php");
```
we can instead type
```php
use function vlw\Scaffold\load;

load("some/file.php");
```

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/3
2025-10-31 10:08:32 +01:00
7 changed files with 67 additions and 73 deletions

View file

@ -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;
```

View file

@ -1,14 +1,12 @@
<?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/API/API.md
*/
namespace vlw\Scaffold\API;
use Reflect\Rules\Ruleset;
/**
* Wrapper for Reflect API endpoints
*/
class API {
/**
* Create a new API request with automatic request validation

View file

@ -1,10 +1,5 @@
<?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/Database/Database.md
*/
namespace vlw\Scaffold\Database;
use vlw\MySQL\MySQL;
@ -13,6 +8,9 @@
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";

View file

@ -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 "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,10 +72,9 @@
*
* @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)
public function isset(): bool {
return $this->_isset ??= new Database()
->from($this->table)
->where($this->where)
->limit(1)
->select()
@ -105,7 +102,7 @@
$this->_row[$key] = $value;
return $this->db
->for($this->table)
->from($this->table)
->where($this->where)
->update([$key => $value]);
}

View file

@ -1,12 +1,11 @@
<?php
/**
* Read the documentation at:
* https://codeberg.org/vlw/scaffold/docs/Database/Paginate.md
*/
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 {
public const KEY = "p";
public const DEFAULT_RANGE = 3;

View file

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

View file

@ -11,19 +11,13 @@
*
* @param string $pathname Relative path from project root to a source file
*/
public static function include(string $pathname): void {
public static function load(string $pathname): void {
// Load the Vegvisir VV class from Reflect
if (!class_exists("VV")) {
self::load_vegvisir_kernel();
require_once Path::root("vegvisir/src/kernel/Init.php");
require_once Path::root("vegvisir/src/request/VV.php");
}
require_once VV::root($pathname);
}
/**
* Load the Vegvisir VV class from Reflect
*/
private static function load_vegvisir_kernel(): void {
require_once Path::root("vegvisir/src/kernel/Init.php");
require_once Path::root("vegvisir/src/request/VV.php");
}
}