Compare commits

..

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

5 changed files with 11 additions and 58 deletions

View file

@ -1,5 +1,5 @@
# Scaffold
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.
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.
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 (primarily) for Reflect and Vegvisir projects",
"description": "Project scaffolding for Reflect and Vegvisir projects",
"type": "library",
"license": "GPL-3.0-or-later",
"authors": [

View file

@ -15,49 +15,6 @@
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
*/
@ -68,8 +25,6 @@
$_ENV["mariadb"]["pass"],
$_ENV["mariadb"]["db"],
);
self::$instance = $this;
}
/**

View file

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

View file

@ -1,6 +1,6 @@
<?php
namespace vlw\Scaffold;
namespace vlw;
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";
}