Compare commits
27 Commits
Author | SHA1 | Date |
---|---|---|
Michał Sieciechowicz | f5faf4df93 | |
Sieciech | 9060e31a92 | |
Sieciech | 4cafd2c93b | |
Michal Sieciechowicz | 73af54b952 | |
Dariusz Jaski | 83f328d8ff | |
Michal Sieciechowicz | f7d8f76a79 | |
Michal Sieciechowicz | 766c29c5f3 | |
Dariusz Jaski | e14198e262 | |
Dariusz Jaski | 1be8bc5511 | |
Dariusz Jaski | 37599e8cf6 | |
Dariusz Jaski | fbf2aaae07 | |
Dariusz Jaski | 1920374639 | |
Dariusz Jaski | 1c886fb385 | |
Dariusz Jaski | b9d1d1601a | |
Dariusz Jaski | 3f9758c403 | |
Dariusz Jaski | 661406db67 | |
Dariusz Jaski | 9c6c34f870 | |
Dariusz Jaski | a026facb7c | |
Dariusz Jaski | 3c43612503 | |
Dariusz Jaski | d32c7a0b92 | |
Dariusz Jaski | d9da5559c5 | |
Dariusz Jaski | afc2b5a261 | |
Dariusz Jaski | 54113fffc0 | |
Michal Sieciechowicz | 9204f70e50 | |
Michal Sieciechowicz | a3d8bb6131 | |
Michal Sieciechowicz | a3d71c7bc5 | |
Michal Sieciechowicz | a3c38a27c7 |
|
@ -1,22 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use App\Query\IJobQuery;
|
||||
use App\Query\JobQuery;
|
||||
use Fufle\ORM\Model as BaseModel;
|
||||
|
||||
class Job extends BaseModel {
|
||||
//private string $table = 'jobs';
|
||||
//protected static ?IJobQuery $query;
|
||||
public int $id;
|
||||
public float $budget;
|
||||
public static function Initialize() {
|
||||
if (static::$query === null) {
|
||||
static::$query = new JobQuery(self::class);
|
||||
}
|
||||
}
|
||||
public static function GetLast10() {
|
||||
return self::QueryAll(self::$query->GetLast10());
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "Fufle/SiBase2",
|
||||
"name": "fufle/micro-orm",
|
||||
"description": "MicroORM for php8",
|
||||
"type": "orm",
|
||||
"require": {
|
||||
|
@ -16,7 +16,8 @@
|
|||
"minimum-stability": "dev",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Fufle\\": "src/Fufle/"
|
||||
"Fufle\\": "src/Fufle/",
|
||||
"Fufle\\Demo\\": "demo/"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\Demo\Model;
|
||||
|
||||
use Fufle\Demo\Query\IJobQuery;
|
||||
use Fufle\Demo\Query\JobQuery;
|
||||
use Fufle\ORM\Model as BaseModel;
|
||||
use Fufle\ORM\IQuery;
|
||||
|
||||
class Job extends BaseModel {
|
||||
public int $id;
|
||||
public float $budget;
|
||||
public static function SetQueryModel(): IQuery {
|
||||
return new JobQuery( static::class );
|
||||
}
|
||||
|
||||
public static function GetLast10() {
|
||||
return self::QueryAll(self::QueryModel()->GetLast10());
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace App\Query;
|
||||
namespace Fufle\Demo\Query;
|
||||
|
||||
use Fufle\ORM\IQuery as IBaseQuery;
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Query;
|
||||
namespace Fufle\Demo\Query;
|
||||
|
||||
use App\Query\IJobQuery;
|
||||
use Fufle\Demo\Query\IJobQuery;
|
||||
use Fufle\ORM\Query as BaseQuery;
|
||||
|
||||
class JobQuery extends BaseQuery implements IJobQuery {
|
||||
public function GetLast10(): string {
|
||||
return "select * from jobs order by id desc limit 10";
|
||||
return "SELECT * from jobs order by id desc limit 10";
|
||||
}
|
||||
}
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\Database;
|
||||
|
||||
use PDO;
|
||||
|
||||
class ConnectionManager {
|
||||
private static $connection;
|
||||
|
||||
public static function connect(string $connectionString) {
|
||||
self::$connection = new PDO($connectionString);
|
||||
}
|
||||
|
||||
public static function Query(string $query, array|object $params = [], string $output = 'stdObject') {
|
||||
$stmt = self::$connection->prepare($query);
|
||||
$stmt->execute($params);
|
||||
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
public static function QueryAll(string $query, array|object $params = [], string $output = 'stdObject') {
|
||||
$stmt = self::$connection->prepare($query);
|
||||
$stmt->execute($params);
|
||||
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database;
|
||||
|
||||
use PDO;
|
||||
use Fufle\ORM\Database\QueryData;
|
||||
use Fufle\ORM\Database\Connectors\IConnector;
|
||||
|
||||
class ConnectionManager {
|
||||
private static $connection;
|
||||
private static $connectors = [];
|
||||
private static string $connectionString;
|
||||
private static Credentials $credentials;
|
||||
|
||||
public static function RegisterConnector(IConnector $connector) {
|
||||
self::$connectors[] = $connector;
|
||||
}
|
||||
|
||||
public static function Connect(string $driver, $host, $port = null, $database = null, $user = null, $password = null) {
|
||||
foreach (self::$connectors as $c) {
|
||||
if ($c->driverEquals($driver)) {
|
||||
$credentials = new Credentials($host, $port, $database, $user, $password);
|
||||
self::$connectionString = $c->getConnectionString($credentials);
|
||||
self::ConnectWithString(self::$connectionString);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw new Exception('Driver not found');
|
||||
}
|
||||
|
||||
public static function ConnectWithString(string $connectionString) {
|
||||
self::$connection = new PDO($connectionString);
|
||||
}
|
||||
|
||||
public static function Query(string $query, array|object $params = [], string $output = 'stdObject') {
|
||||
$data = new QueryData($query, $params);
|
||||
$data->normalize();
|
||||
$stmt = self::$connection->prepare($data->query);
|
||||
$stmt->execute($data->params);
|
||||
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||
return $stmt->fetch();
|
||||
}
|
||||
|
||||
public static function QueryAll(string $query, array|object $params = [], string $output = 'stdObject') {
|
||||
$data = new QueryData($query, $params);
|
||||
$data->normalize();
|
||||
$stmt = self::$connection->prepare($data->query);
|
||||
$stmt->execute($data->params);
|
||||
$stmt->setFetchMode(PDO::FETCH_CLASS, $output);
|
||||
return $stmt->fetchAll();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database\Connectors;
|
||||
|
||||
use Fufle\ORM\Database\Credentials;
|
||||
|
||||
interface IConnector {
|
||||
public function getConnectionString(Credentials $credentials);
|
||||
public function driverEquals(string $driver);
|
||||
public static function Connect(string $host, int $port, string $database, string $user, string $password);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database\Connectors;
|
||||
|
||||
use Fufle\ORM\Database\Credentials;
|
||||
use Fufle\ORM\Database\ConnectionManager;
|
||||
|
||||
class PostgresConnector implements IConnector {
|
||||
public function getConnectionString(Credentials $c) {
|
||||
return "pgsql:host={$c->host};port={$c->port};dbname={$c->database};user={$c->user};password={$c->password}";
|
||||
}
|
||||
public function driverEquals(string $driver) {
|
||||
return $driver === 'pgsql';
|
||||
}
|
||||
|
||||
public static function Connect(string $host, int $port, string $database, string $user, string $password) {
|
||||
ConnectionManager::RegisterConnector(new PostgresConnector());
|
||||
ConnectionManager::Connect('pgsql', $host, $port, $database, $user, $password);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database;
|
||||
|
||||
class Credentials {
|
||||
|
||||
public function __construct(
|
||||
public string $host,
|
||||
public ?int $port = null,
|
||||
public ?string $database = null,
|
||||
public ?string $user = null,
|
||||
public ?string $password = null) {
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Database;
|
||||
|
||||
class QueryData {
|
||||
public function __construct(
|
||||
public string $query,
|
||||
public array|object $params
|
||||
) {
|
||||
|
||||
}
|
||||
|
||||
public function normalize() {
|
||||
$params = (array)$this->params;
|
||||
$replaces = [];
|
||||
foreach($params as $name => $param) {
|
||||
if (is_array($param)) {
|
||||
$replaces2 = [];
|
||||
foreach($param as $i => $p) {
|
||||
$params[$name.$i.'x'] = $p;
|
||||
$replaces[] = ':'.$name.$i.'x';
|
||||
}
|
||||
unset($params[$name]);
|
||||
$this->query = str_replace(':'.$name, implode(', ', $replaces), $this->query);
|
||||
}
|
||||
}
|
||||
$this->params = $params;
|
||||
}
|
||||
}
|
|
@ -2,50 +2,82 @@
|
|||
|
||||
namespace Fufle\ORM;
|
||||
|
||||
use Fufle\Database\ConnectionManager;
|
||||
use Fufle\ORM\Database\ConnectionManager;
|
||||
|
||||
abstract class Model {
|
||||
protected static string $primaryKey = 'id';
|
||||
protected static ?string $table = null;
|
||||
protected static ?IQuery $query = null;
|
||||
|
||||
public static function getTableName() {
|
||||
return static::$table;
|
||||
}
|
||||
|
||||
public static function getPrimaryKey() {
|
||||
return static::$primaryKey;
|
||||
}
|
||||
|
||||
public static function Get(int $id) {
|
||||
self::Check();
|
||||
return self::Query(
|
||||
static::$query->Get(),
|
||||
[$id]);
|
||||
}
|
||||
|
||||
protected static function Query(string $sql, array|object $params = []) {
|
||||
self::Check();
|
||||
return ConnectionManager::Query(
|
||||
$sql,
|
||||
$params,
|
||||
get_called_class());
|
||||
}
|
||||
|
||||
protected static function QueryAll(string $sql, array|object $params = []) {
|
||||
self::Check();
|
||||
return ConnectionManager::QueryAll(
|
||||
$sql,
|
||||
$params,
|
||||
get_called_class());
|
||||
private static array $queryModels = [];
|
||||
|
||||
protected static array $fields = [];
|
||||
|
||||
public function toArray(): array {
|
||||
$array = [];
|
||||
$fields = static::$fields;
|
||||
foreach($fields as $field) {
|
||||
if (isset($this->{$field})) {
|
||||
$array[$field] = $this->{$field};
|
||||
}
|
||||
}
|
||||
return $array;
|
||||
}
|
||||
|
||||
public static function Check() {
|
||||
public static function Instance(object $object): static {
|
||||
$obj = new static;
|
||||
$params = get_object_vars($object);
|
||||
foreach ($params as $key => $value) {
|
||||
$obj->{$key} = $value;
|
||||
}
|
||||
return $obj;
|
||||
}
|
||||
|
||||
protected static function QueryModel() {
|
||||
if (!isset(static::$queryModels[static::class]))
|
||||
if (static::$query === null) {
|
||||
static::$queryModels[static::class] = static::SetQueryModel();
|
||||
}
|
||||
return static::$queryModels[static::class];
|
||||
}
|
||||
|
||||
public static function GetTableName() {
|
||||
if (static::$table === null) {
|
||||
static::$table = basename(str_replace('\\', '/', get_called_class())) . 's';
|
||||
}
|
||||
static::Initialize();
|
||||
return static::$table;
|
||||
}
|
||||
|
||||
abstract public static function Initialize();
|
||||
|
||||
public static function GetPrimaryKey() {
|
||||
return static::$primaryKey;
|
||||
}
|
||||
|
||||
public static function Get(int $id) {
|
||||
return self::Query(
|
||||
static::QueryModel()->Get(),
|
||||
[$id]);
|
||||
}
|
||||
|
||||
protected static function Query(string $sql, array|object $params = []) {
|
||||
$result = ConnectionManager::Query(
|
||||
$sql,
|
||||
$params,
|
||||
get_called_class());
|
||||
if (!$result) {
|
||||
return null;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected static function QueryAll(string $sql, array|object $params = []) {
|
||||
$result = ConnectionManager::QueryAll(
|
||||
$sql,
|
||||
$params,
|
||||
get_called_class());
|
||||
if (!$result) {
|
||||
return [];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
abstract public static function SetQueryModel(): IQuery;
|
||||
}
|
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
|
||||
namespace Fufle\ORM\Providers;
|
||||
|
||||
use Fufle\ORM\Model;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Contracts\Auth\UserProvider;
|
||||
use Illuminate\Contracts\Auth\Authenticatable;
|
||||
|
||||
class PassportUserProvider implements UserProvider {
|
||||
/**
|
||||
* The ORM User Model
|
||||
*/
|
||||
private $model;
|
||||
|
||||
/**
|
||||
* Create a new ORM user provider.
|
||||
*
|
||||
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Model $userModel) {
|
||||
$this->model = $userModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a user by the given credentials.
|
||||
*
|
||||
* @param array $credentials
|
||||
* @return \Illuminate\Contracts\Auth\Authenticatable|null
|
||||
*/
|
||||
public function retrieveByCredentials(array $credentials) {
|
||||
if (empty($credentials)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$user = $this->model->findForPassport($credentials['username']);
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a user against the given credentials.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Auth\Authenticatable $user
|
||||
* @param array $credentials Request credentials
|
||||
* @return bool
|
||||
*/
|
||||
public function validateCredentials(Authenticatable $user, Array $credentials) {
|
||||
return ($credentials['username'] == $user->getAuthIdentifier() &&
|
||||
md5($credentials['password']) == $user->getAuthPassword());
|
||||
}
|
||||
|
||||
public function retrieveById($identifier) {
|
||||
$userClass = get_class($this->model);
|
||||
return $userClass::FindOrFail($identifier);
|
||||
}
|
||||
|
||||
public function retrieveByToken($identifier, $token) {
|
||||
}
|
||||
|
||||
public function updateRememberToken(Authenticatable $user, $token) {
|
||||
}
|
||||
}
|
|
@ -1,13 +1,15 @@
|
|||
<?php
|
||||
namespace Fufle\ORM;
|
||||
|
||||
class Query {
|
||||
private string $table;
|
||||
private string $primaryKey;
|
||||
class Query implements IQuery {
|
||||
protected string $table;
|
||||
protected string $primaryKey;
|
||||
|
||||
public function __construct(string $model) {
|
||||
$this->table = $model::getTableName();
|
||||
$this->primaryKey = $model::getPrimaryKey();
|
||||
$this->table = $model::GetTableName();
|
||||
$this->primaryKey = $model::GetPrimaryKey();
|
||||
}
|
||||
|
||||
public function Get(): string {
|
||||
return 'SELECT * FROM '.$this->table.' WHERE '.$this->primaryKey.' = ?';
|
||||
}
|
||||
|
|
|
@ -1,19 +1,30 @@
|
|||
<?php
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
use App\Model\Job;
|
||||
use Fufle\Database\ConnectionManager;
|
||||
use Fufle\Demo\Model\Job;
|
||||
//use Fufle\ORM\Database\ConnectionManager;
|
||||
use Fufle\ORM\Database\Connectors\PostgresConnector;
|
||||
|
||||
$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic_dev;user=test;password=pass123';
|
||||
ConnectionManager::Connect($dns);
|
||||
echo "
|
||||
|
||||
";
|
||||
var_dump(Job::Get(10));
|
||||
//$dns = 'pgsql:host=localhost;port=5432;dbname=dynamic;user=dynamic;password=password123';
|
||||
$params = [
|
||||
// 'pgsql',
|
||||
'localhost',
|
||||
5432,
|
||||
'dynamic_dev',
|
||||
'dynamic',
|
||||
'Pass@Dyna21',
|
||||
];
|
||||
PostgresConnector::Connect(...$params);
|
||||
//ConnectionManager::RegisterConnector(new PostgresConnector());
|
||||
//ConnectionManager::Connect(...$params);
|
||||
echo "
|
||||
|
||||
";
|
||||
var_dump(Job::GetLast10());
|
||||
echo "
|
||||
|
||||
";
|
||||
var_dump(Job::Get(10));
|
||||
echo "
|
||||
|
||||
";
|
Loading…
Reference in New Issue