Compare commits

...

27 Commits
master ... 0.9

Author SHA1 Message Date
Michał Sieciechowicz f5faf4df93 fix Model 2022-02-03 09:47:17 +00:00
Sieciech 9060e31a92 fix cast 2021-10-24 00:59:10 +02:00
Sieciech 4cafd2c93b cast 2021-10-23 22:26:30 +02:00
Michal Sieciechowicz 73af54b952 fix 2021-09-29 17:19:48 +02:00
Dariusz Jaski 83f328d8ff 0.6 2021-03-17 22:45:55 +01:00
Michal Sieciechowicz f7d8f76a79 Merge remote-tracking branch 'origin/0.4' into 0.5 2021-03-17 13:44:51 +01:00
Michal Sieciechowicz 766c29c5f3 laravel user provider 2021-03-17 13:08:20 +01:00
Dariusz Jaski e14198e262 fix 2021-03-08 22:26:12 +01:00
Dariusz Jaski 1be8bc5511 fix 2021-03-08 20:55:47 +01:00
Dariusz Jaski 37599e8cf6 fix 2021-03-07 11:34:29 +01:00
Dariusz Jaski fbf2aaae07 0.2.3 2021-03-07 11:30:13 +01:00
Dariusz Jaski 1920374639 ns 2021-03-07 11:20:52 +01:00
Dariusz Jaski 1c886fb385 model 2021-03-07 11:17:21 +01:00
Dariusz Jaski b9d1d1601a d 2021-03-07 11:12:43 +01:00
Dariusz Jaski 3f9758c403 fdn 2021-03-07 11:06:39 +01:00
Dariusz Jaski 661406db67 nullsafe 2021-03-07 10:55:23 +01:00
Dariusz Jaski 9c6c34f870 eq 2021-03-07 10:54:11 +01:00
Dariusz Jaski a026facb7c iface 2021-03-07 09:55:06 +01:00
Dariusz Jaski 3c43612503 rename 2021-03-07 09:52:45 +01:00
Dariusz Jaski d32c7a0b92 use 2021-03-07 09:39:13 +01:00
Dariusz Jaski d9da5559c5 NS 2021-03-07 01:53:29 +01:00
Dariusz Jaski afc2b5a261 rename 2021-03-07 01:49:33 +01:00
Dariusz Jaski 54113fffc0 connectors 2021-03-07 01:15:16 +01:00
Michal Sieciechowicz 9204f70e50 0.2.2 2021-03-03 13:41:02 +01:00
Michal Sieciechowicz a3d8bb6131 fixes 2021-03-01 16:25:31 +01:00
Michal Sieciechowicz a3d71c7bc5 fix 2021-03-01 16:22:54 +01:00
Michal Sieciechowicz a3c38a27c7 fixes 2021-03-01 16:06:02 +01:00
17 changed files with 311 additions and 103 deletions

View File

@ -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());
}
}

View File

@ -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/"
}
}
}

20
demo/Model/Job.php Normal file
View File

@ -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());
}
}

View File

@ -1,6 +1,6 @@
<?php
namespace App\Query;
namespace Fufle\Demo\Query;
use Fufle\ORM\IQuery as IBaseQuery;

View File

@ -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";
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -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);
}

View File

@ -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);
}
}

View File

@ -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) {
}
}

View File

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

View File

@ -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;
private static array $queryModels = [];
public static function getTableName() {
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 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';
}
return static::$table;
}
public static function getPrimaryKey() {
public static function GetPrimaryKey() {
return static::$primaryKey;
}
public static function Get(int $id) {
self::Check();
return self::Query(
static::$query->Get(),
static::QueryModel()->Get(),
[$id]);
}
protected static function Query(string $sql, array|object $params = []) {
self::Check();
return ConnectionManager::Query(
$result = ConnectionManager::Query(
$sql,
$params,
get_called_class());
if (!$result) {
return null;
}
return $result;
}
protected static function QueryAll(string $sql, array|object $params = []) {
self::Check();
return ConnectionManager::QueryAll(
$result = ConnectionManager::QueryAll(
$sql,
$params,
get_called_class());
}
public static function Check() {
if (static::$table === null) {
static::$table = basename(str_replace('\\', '/', get_called_class())) . 's';
if (!$result) {
return [];
}
static::Initialize();
return $result;
}
abstract public static function Initialize();
abstract public static function SetQueryModel(): IQuery;
}

View File

@ -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) {
}
}

View File

@ -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.' = ?';
}

View File

@ -1,15 +1,22 @@
<?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 "
";
@ -17,3 +24,7 @@ var_dump(Job::GetLast10());
echo "
";
var_dump(Job::Get(10));
echo "
";