-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 7b0d42f
Showing
20 changed files
with
707 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 kingmaj0r | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "pulseframe/database", | ||
"description": "PulseFrame's database functionality for php.", | ||
"type": "library", | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"PulseFrame\\": "src/" | ||
} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "KingMaj0r" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace PulseFrame\Database; | ||
|
||
class Blueprint | ||
{ | ||
protected $table; | ||
protected $columns = []; | ||
|
||
public function __construct($tableName) | ||
{ | ||
$this->table = $tableName; | ||
} | ||
|
||
public function id($type = 'int') | ||
{ | ||
$this->addColumn('id', $type, ['primary' => true]); | ||
return $this; | ||
} | ||
|
||
public function text($name) | ||
{ | ||
$this->addColumn($name, 'text'); | ||
return $this; | ||
} | ||
|
||
public function timestamp($name = 'timestamp') | ||
{ | ||
$this->addColumn($name, 'timestamp'); | ||
return $this; | ||
} | ||
|
||
public function nullable() | ||
{ | ||
$this->setAttribute('nullable', true); | ||
return $this; | ||
} | ||
|
||
public function primary() | ||
{ | ||
$this->setAttribute('primary', true); | ||
return $this; | ||
} | ||
|
||
public function unique() | ||
{ | ||
$this->setAttribute('unique', true); | ||
return $this; | ||
} | ||
|
||
protected function addColumn($name, $type, $attributes = []) | ||
{ | ||
$this->columns[$name] = array_merge(['type' => $type], $attributes); | ||
} | ||
|
||
protected function setAttribute($attribute, $value) | ||
{ | ||
$lastColumn = array_key_last($this->columns); | ||
if ($lastColumn !== null) { | ||
$this->columns[$lastColumn][$attribute] = $value; | ||
} | ||
} | ||
|
||
public function getColumns() | ||
{ | ||
return $this->columns; | ||
} | ||
|
||
public function getTable() | ||
{ | ||
return $this->table; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace PulseFrame\database; | ||
|
||
abstract class Migration | ||
{ | ||
abstract public function up(); | ||
abstract public function down(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace PulseFrame\Database\Migrations; | ||
|
||
use PulseFrame\Database\Migration; | ||
use PulseFrame\Facades\Schema; | ||
use PulseFrame\Database\Blueprint; | ||
|
||
class CreatePulseFrame extends Migration | ||
{ | ||
public function up() | ||
{ | ||
Schema::createTable('pulseframe', function (Blueprint $table) { | ||
$table->id('uuid'); | ||
$table->text('data')->nullable(); | ||
$table->timestamp()->nullable(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropTable('pulseframe'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
namespace PulseFrame\Database\Models; | ||
|
||
use PulseFrame\Model; | ||
|
||
class PulseFrameModel extends Model | ||
{ | ||
public $table = 'pulseframe'; | ||
public $primaryKey = 'id'; | ||
public $connection = 'default'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace PulseFrame\Database; | ||
|
||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
abstract class Seeder | ||
{ | ||
abstract public function run(OutputInterface $output); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace PulseFrame\Database\Seeders; | ||
|
||
use PulseFrame\Database\Seeder; | ||
use PulseFrame\Facades\Env; | ||
use PulseFrame\Facades\Config; | ||
use PulseFrame\Facades\Database; | ||
use PulseFrame\Facades\Schema; | ||
use PulseFrame\Database\Models\PulseFrameModel; | ||
|
||
class InsertPulseFrame extends Seeder | ||
{ | ||
public function run($output) | ||
{ | ||
if (empty(Env::get('app.key'))) { | ||
echo "It looks like you forgot to generate the app key!"; | ||
exit; | ||
} | ||
|
||
$ifExist = Database::find(PulseFrameModel::class, "328ef6b3-68d0-4f47-9ffe-7529d5d392b3"); | ||
|
||
if (!$ifExist) { | ||
Schema::insert('pulseframe', [ | ||
'id' => '328ef6b3-68d0-4f47-9ffe-7529d5d392b3', | ||
'data' => json_encode([ | ||
'name' => Env::get('app.name'), | ||
'version' => Config::get('app', 'version'), | ||
'stage' => Config::get('app', 'stage'), | ||
'key' => Env::get('app.key') | ||
]), | ||
'timestamp' => date('Y-m-d H:i:s') | ||
]); | ||
} else { | ||
exit; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace PulseFrame\Facades; | ||
|
||
use PDO; | ||
|
||
class Database | ||
{ | ||
protected static $conn = null; | ||
|
||
public static function getConnection($model) | ||
{ | ||
$connectionName = self::resolveConnectionName($model); | ||
$databaseConfig = Config::get('database'); | ||
|
||
if (!isset(self::$conn[$connectionName]) || self::$conn[$connectionName]->getAttribute(PDO::ATTR_DRIVER_NAME) !== $databaseConfig[$connectionName]['driver']) { | ||
$host = $databaseConfig[$connectionName]['host']; | ||
$username = $databaseConfig[$connectionName]['username']; | ||
$password = $databaseConfig[$connectionName]['password']; | ||
$database = $databaseConfig[$connectionName]['database']; | ||
$port = $databaseConfig[$connectionName]['port']; | ||
$driver = $databaseConfig[$connectionName]['driver']; | ||
|
||
try { | ||
self::$conn[$connectionName] = new PDO("$driver:host=$host;dbname=$database;port=$port", $username, $password); | ||
self::$conn[$connectionName]->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} catch (\PDOException $e) { | ||
throw new \Exception("Connection failed: " . $e->getMessage()); | ||
} | ||
} | ||
|
||
return self::$conn[$connectionName]; | ||
} | ||
|
||
public static function getModelInstance($model) | ||
{ | ||
if (is_string($model) && class_exists($model)) { | ||
$className = $model; | ||
} elseif (is_string($model)) { | ||
$className = "\\App\\Models\\" . $model; | ||
} elseif (is_object($model) && method_exists($model, 'class')) { | ||
$className = get_class($model); | ||
} | ||
|
||
if (!class_exists($className)) { | ||
throw new \Exception("Model not found: {$className}"); | ||
} | ||
|
||
return new $className(); | ||
} | ||
|
||
public static function __callStatic($method, $args) | ||
{ | ||
$methodClass = "\\PulseFrame\\Methods\\Static\\Database\\" . ucfirst($method); | ||
if (class_exists($methodClass) && method_exists($methodClass, 'handle')) { | ||
$instance = new $methodClass(...$args); | ||
if (method_exists($instance, $method)) { | ||
return $instance->$method(...$args); | ||
} elseif (method_exists($instance, 'handle')) { | ||
return $instance->handle(...$args); | ||
} else { | ||
throw new \BadMethodCallException("Method {$method} does not exist in {$methodClass}."); | ||
} | ||
} else { | ||
throw new \BadMethodCallException("Method {$method} does not exist."); | ||
} | ||
} | ||
|
||
public function __call($method, $args) | ||
{ | ||
$methodClass = "\\PulseFrame\\Methods\\Static\\Database\\" . ucfirst($method); | ||
if (class_exists($methodClass)) { | ||
$instance = new $methodClass($this->instance); | ||
if (method_exists($instance, $method)) { | ||
return $instance->$method(...$args); | ||
} elseif (method_exists($instance, 'handle')) { | ||
return $instance->handle($instance, ...$args); | ||
} else { | ||
throw new \BadMethodCallException("Method {$method} does not exist in {$methodClass}."); | ||
} | ||
} else { | ||
throw new \BadMethodCallException("Class {$methodClass} does not exist."); | ||
} | ||
} | ||
|
||
protected static function resolveConnectionName($model) | ||
{ | ||
if (is_string($model)) { | ||
$connectionName = $model; | ||
} elseif (is_object($model) && property_exists($model, 'connection')) { | ||
$connectionName = $model->connection; | ||
} else { | ||
throw new \InvalidArgumentException("Invalid model type. Must be an object with a 'connection' property or a string."); | ||
} | ||
return $connectionName; | ||
} | ||
} |
Oops, something went wrong.