Compare commits

..

12 commits

Author SHA1 Message Date
ffd809f76d refactor: database class singleton to use existing connection (#32)
This is a half-way fix for #31 so it does not close it. But it does solve the issue of `Too many connections` if the `Database` class when the `Database` class is instanced a lot. The issue of credentials still remain.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/32
2026-02-24 10:44:05 +01:00
71a9f12b02 fix: use func_num_args() to determine if $return should be used for Database\Model (#30)
It was not possible to set the return type explicitly as `null`, since that was the value we checked for if we should return the value of `$return` or not. We're not checking the total amount of arguments provided to check if a `$return` value was passed or not.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/30
2026-02-22 14:41:41 +01:00
2d55d954a1 refactor: expose the Database property on Model as readonly public (#29)
Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/29
2026-02-22 14:41:22 +01:00
fd0c8cbbcd fix: pass an optional return value to Database\Model->set() (#28)
This PR addresses a bug that can arise (for example) if an object is passed to a database column that accepts a string. Passing this object (or something else) to the `$return` parameter will return the value of that variable instead of the value of `$value`

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/28
2026-02-16 15:07:11 +01:00
19e15c0b23 fix: allow int type for abstract $id property in Database\Model (#27)
It's not guaranteed that the primary key ($id here) is a string. We primarily use UUIDs for the primary key, but it could also be an integer.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/27
2026-02-12 15:25:49 +01:00
c858cd111a fix: use array_key_exists() on $_ENV to determine if MariaDB key is set (#26)
Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/26
2026-02-12 08:56:31 +01:00
8a04cab4b5 fix: database as optional parameter for set_credentials (#25)
This is another follow-up PR for #22 which fixes a deprecation issue about defining optional parameters that are then called as named.

```php
Database::set_credentials("db");
```
```
vlw\Scaffold\Database::set_credentials(): Optional parameter $host declared before required parameter $database is implicitly treated as a required parameter
```

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/25
2026-02-12 08:48:37 +01:00
f4b8f8673e fix: set Database env credentials without returning a new instance (#24)
This is a follow-up PR from #22. Instead of returning a new instance after setting the environment variables we simply do nothing. Most of the extending classes will not take advantage of the new instance anyways, and will just call `new Database()` as before. Of course, as mentioned in #22 this setup is not ideal, and another solution should be implemented to instance this class.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/24
2026-02-11 17:39:01 +01:00
1536079fe3 feat: add static method to create new Database instance from credentials (#22)
In this PR we add a new static method for creating a `vlw\Scaffold\Database\Database` instance from a set of provided credentials. This is of course not ideal and a better way to instance this class should be added in the future.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/22
2026-02-11 17:25:22 +01:00
c501201f11 doc: add text that this library is primarily for Vegvisir and Reflect scaffolding (#23)
Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/23
2026-02-11 17:25:05 +01:00
723b048c8f fix: correct append php ext load condition (#21)
Fix of the conditional check introduced in #19.

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/21
2026-01-25 10:24:11 +01:00
4f4191c658 fix: restore main Scaffold class namespace (#20)
a77c0ac6a3/composer.json (L15)

Follow-up PR from #18. Apparently we can't put things in the root namespace because of this. Maybe we can investigate this more but for now we have to keep the main Scaffold class under the original namespace `vlw\Scaffold\Scaffold`

Reviewed-on: https://codeberg.org/vlw/scaffold/pulls/20
2026-01-25 10:14:16 +01:00
5 changed files with 58 additions and 11 deletions

View file

@ -1,5 +1,5 @@
# Scaffold
This composer package contains my project boilerplate code for working with [Vegvisir](https://vegvisir.vlw.se) and [Reflect](https://reflect.vlw.se) projects. It contains my most commonly used classes and functions which I use to make development of new project in these frameworks easier.
This composer package (primarily) contains my project boilerplate code for working with [Vegvisir](https://vegvisir.vlw.se) and [Reflect](https://reflect.vlw.se) projects. It contains my most commonly used classes and functions which I use to make development of new project in these frameworks easier.
Refer to the documentation in each source file for more information about its function.

View file

@ -1,6 +1,6 @@
{
"name": "vlw/scaffold",
"description": "Project scaffolding for Reflect and Vegvisir projects",
"description": "Project scaffolding (primarily) for Reflect and Vegvisir projects",
"type": "library",
"license": "GPL-3.0-or-later",
"authors": [

View file

@ -15,6 +15,49 @@
public const DATE_FORMAT = "Y-m-d";
public const DATETIME_FORMAT = "Y-m-d H:i:s";
private const DEFAULT_HOSTNAME = "localhost";
private const DEFAULT_USERNAME = "www-data";
private const DEFAULT_PASSWORD = "";
private static ?Database $instance = null;
/**
* Create a new Database instance from credentials
*
* @param string $host
* @param string $username
* @param string $password
* @param string $database
* @return void
*/
public static function set_credentials(
?string $host = self::DEFAULT_HOSTNAME,
?string $username = self::DEFAULT_USERNAME,
?string $password = self::DEFAULT_PASSWORD,
?string $database = ""
) {
// Create key if it does not exist
if (!array_key_exists("mariadb", $_ENV)) {
$_ENV["mariadb"] = [];
}
// Set environment variables from credentials
$_ENV["mariadb"]["host"] = $host;
$_ENV["mariadb"]["user"] = $username;
$_ENV["mariadb"]["pass"] = $password;
$_ENV["mariadb"]["db"] = $database;
self::$instance = null;
}
public static function instance(): static {
if (self::$instance) {
return self::$instance;
}
return new static();
}
/**
* Wrap a new vlw/MySQL instance with credentials from project .env.ini
*/
@ -25,6 +68,8 @@
$_ENV["mariadb"]["pass"],
$_ENV["mariadb"]["db"],
);
self::$instance = $this;
}
/**

View file

@ -13,9 +13,10 @@
public const DATE_FORMAT = Database::DATE_FORMAT;
public const DATETIME_FORMAT = Database::DATETIME_FORMAT;
abstract public string $id { get; }
abstract public int|string $id { get; }
public readonly Database $db;
protected readonly Database $db;
private bool $_resolved = false;
private bool $_isset;
private ?array $_row;
@ -26,7 +27,7 @@
* @param string $table The target database table
*/
protected static function create(string $table, array $values): bool {
return new Database()->from($table)->insert($values);
return Database::instance()->from($table)->insert($values);
}
/**
@ -41,7 +42,7 @@
public readonly array $columns,
public readonly array $where
) {
$this->db = new Database();
$this->db = Database::instance();
}
/**
@ -74,7 +75,7 @@
* @return bool Entity exists
*/
public function isset(): bool {
return $this->_isset ??= new Database()
return $this->_isset ??= Database::instance()
->from($this->table)
->where($this->where)
->limit(1)
@ -97,9 +98,10 @@
*
* @param string $key Target column to update
* @param mixed $value New value of target column
* @param return $return (optional) Return this instead of $value when set
* @return mixed The value that was sent to $value
*/
public function set(string $key, mixed $value): mixed {
public function set(string $key, mixed $value, mixed $return = null): mixed {
$this->_row[$key] = $value;
$this->db
@ -107,6 +109,6 @@
->where($this->where)
->update([$key => $value]);
return $value;
return func_num_args() === 2 ? $value : $return;
}
}

View file

@ -1,6 +1,6 @@
<?php
namespace vlw;
namespace vlw\Scaffold;
use VV;
use Reflect\Path;
@ -19,7 +19,7 @@
}
// Append php extension if omitted
if (!substr($pathname, -4) === ".php") {
if (substr($pathname, -4) !== ".php") {
$pathname .= ".php";
}