Skip to content

Feature manager and facade #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions example/db_config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use Dotenv\Dotenv;
use React\EventLoop\Loop;
use Saraf\QB\QueryBuilder\Core\DBFactory;
use Saraf\QB\QueryBuilder\Facades\Database;

$loop = Loop::get();

// Environments
$env = Dotenv::createImmutable(__DIR__ . "/../");
$env->load();

// Env Loader
$DB_NAME = $_ENV['DB_NAME'];
$DB_USER = $_ENV['DB_USER'];
$DB_PASS = $_ENV['DB_PASS'];
$DB_HOST = $_ENV['DB_HOST'];
$DB_PORT_READ = $_ENV['DB_PORT_READ'];
$DB_PORT_WRITE = $_ENV['DB_PORT_WRITE'];



Database::initConnection(
new DBFactory(
$loop,
$DB_HOST,
$DB_NAME,
$DB_USER,
$DB_PASS,
$DB_PORT_WRITE,
$DB_PORT_READ,
5,
5,
2,
2
)
);
18 changes: 18 additions & 0 deletions src/QueryBuilder/Abstracts/Facade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Saraf\QB\QueryBuilder\Abstracts;

abstract class Facade
{
protected static function getFacadeAccessor()
{
throw new \RuntimeException("Facade access not implemented");
}

public static function __callStatic(string $name, array $arguments)
{
$accessor = static::getFacadeAccessor();

return $accessor->$name(...$arguments);
}
}
20 changes: 20 additions & 0 deletions src/QueryBuilder/Facades/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Saraf\QB\QueryBuilder\Facades;

use Saraf\QB\QueryBuilder\Abstracts\Facade;
use Saraf\QB\QueryBuilder\Core\DBFactory;
use Saraf\QB\QueryBuilder\QueryBuilder;
use Saraf\QB\QueryBuilder\QueryBuilderManager;

/**
* @method static void initConnection(DBFactory $DBFactory)
* @method static QueryBuilder query()
*/
class Database extends Facade
{
protected static function getFacadeAccessor(): QueryBuilderManager
{
return QueryBuilderManager::instance();
}
}
42 changes: 42 additions & 0 deletions src/QueryBuilder/QueryBuilderManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Saraf\QB\QueryBuilder;

use Saraf\QB\QueryBuilder\Core\DBFactory;
use Saraf\QB\QueryBuilder\Exceptions\DBFactoryException;

class QueryBuilderManager
{
private static ?self $instance = null;
private DBFactory $connection;

private function __construct()
{
}

public static function instance(): QueryBuilderManager
{
if (is_null(self::$instance)) {

self::$instance = new self();
}

return self::$instance;
}

public function initConnection(
DBFactory $DBFactory,
): void
{
$this->connection = $DBFactory;
}


/**
* @throws DBFactoryException
*/
public function query(): QueryBuilder
{
return $this->connection->getQueryBuilder();
}
}