Skip to content

Commit

Permalink
fix : remove cyclic dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ipranjal committed Dec 7, 2023
1 parent e0d381d commit c6e32b2
Show file tree
Hide file tree
Showing 12 changed files with 226 additions and 228 deletions.
7 changes: 4 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage>
<coverage/>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/Collection.php</file>
</exclude>
</coverage>
</source>
</phpunit>
35 changes: 16 additions & 19 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/Collection.php</file>
</exclude>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./app</directory>
<directory suffix=".php">./src</directory>
</include>
<exclude>
<file>./src/Collection.php</file>
</exclude>
</source>
</phpunit>
94 changes: 23 additions & 71 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
declare(strict_types=1);

namespace Scrawler\Arca;
use Scrawler\Arca\Manager\TableManager;
use Scrawler\Arca\Manager\RecordManager;
use Scrawler\Arca\Manager\ModelManager;
use \Doctrine\DBAL\Connection;

/**
Expand All @@ -28,21 +25,6 @@ class Database
* @var \Doctrine\DBAL\Schema\AbstractSchemaManager
*/
public \Doctrine\DBAL\Schema\AbstractSchemaManager $manager;
/**
* Instance of table manager responsible for working with tables
* @var \Scrawler\Arca\Manager\TableManager
*/
private TableManager $tableManager;
/**
* Instance of record manager responsible for working with records
* @var \Scrawler\Arca\Manager\RecordManager
*/
private RecordManager $recordManager;
/**
* Model manager rsponisble for bootstrapping model instance
* @var \Scrawler\Arca\Manager\ModelManager
*/
private ModelManager $modelManager;
/**
* When $isFrozen is set to true tables are not updated/created
* @var bool
Expand All @@ -54,28 +36,28 @@ class Database
*/
private bool $useUUID = false;

public function __construct(Connection $connection)
public function __construct(Connection $connection , bool $useUUID = false)
{
$this->connection = $connection;
$this->platform = $this->connection->getDatabasePlatform();
$this->manager = $this->connection->createSchemaManager();

}
$this->useUUID = $useUUID;
Managers::create($connection, $useUUID);
$this->registerEvents();

/**
* Once you initialize database, use this to set all manager for database class to work correctly
* @param TableManager $tableManager
* @param RecordManager $recordManager
* @param ModelManager $modelManager
* @return void
*/
public function setManagers(TableManager $tableManager, RecordManager $recordManager, ModelManager $modelManager){
$this->tableManager = $tableManager;
$this->recordManager = $recordManager;
$this->modelManager = $modelManager;
}

public function registerEvents()
{
Event::subscribeTo('model.save', function ($model) {
return $this->save($model);
});
Event::subscribeTo('model.delete', function ($model) {
return $this->delete($model);
});
}


/**
* Executes an SQL query and returns the number of row affected
*
Expand Down Expand Up @@ -108,7 +90,7 @@ public function getAll(string $sql, array $params=[]): array
*/
public function create(string $name) : Model
{
return $this->modelManager->create($name);
return new Model($name);
}

/**
Expand Down Expand Up @@ -148,18 +130,18 @@ public function save(\Scrawler\Arca\Model $model) : mixed
private function createTables($model)
{
if (!$this->isFroozen) {
$table = $this->tableManager->createTable($model);
$this->tableManager->saveOrUpdateTable($model->getName(), $table);
$table = Managers::tableManager()->createTable($model);
Managers::tableManager()->saveOrUpdateTable($model->getName(), $table);
}
}

private function createRecords($model) : mixed
{
if ($model->isLoaded()) {
return $this->recordManager->update($model);
return Managers::recordManager()->update($model);
}

return $this->recordManager->insert($model);
return Managers::recordManager()->insert($model);
}


Expand Down Expand Up @@ -263,11 +245,6 @@ private function saveForeignMtm(\Scrawler\Arca\Model $model, mixed $id): void
}
}

public function getTableManager()
{
return $this->tableManager;
}

/**
* Delete record from database
*
Expand All @@ -276,15 +253,11 @@ public function getTableManager()
*/
public function delete(\Scrawler\Arca\Model $model) : mixed
{
return $this->recordManager->delete($model);
return Managers::recordManager()->delete($model);
}

/**
* Get collection of all records from table
*
* @param String $table
* @param mixed|null $id
* @return mixed
*/
public function get(String $table,mixed $id = null) : Model|Collection
{
Expand All @@ -293,19 +266,16 @@ public function get(String $table,mixed $id = null) : Model|Collection
return $this->getOne($table,$id);
}

return $this->recordManager->getAll($table);
return Managers::recordManager()->getAll($table);
}

/**
* Get single record
*
* @param String $table
* @param mixed $id
* @return mixed
*/
public function getOne(String $table, mixed $id) : Model
{
return $this->recordManager->getById($this->create($table), $id);
return Managers::recordManager()->getById($this->create($table), $id);
}

/**
Expand All @@ -317,7 +287,7 @@ public function getOne(String $table, mixed $id) : Model
*/
public function find(string $name) : QueryBuilder
{
return $this->recordManager->find($name);
return Managers::recordManager()->find($name);
}

/**
Expand All @@ -329,24 +299,6 @@ public function freeze() : void
$this->isFroozen = true;
}

/**
* Call this to useUUID over normal id
* @return void
*/
public function useUUID() : void
{
$this->useUUID = true;
}

/**
* Call this to use id (id is used by default)
* @return void
*/
public function useID() : void
{
$this->useUUID = false;
}

/**
* Checks if database is currently using uuid rather than id
* @return bool
Expand Down
66 changes: 66 additions & 0 deletions src/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Scrawler\Arca;


/**
* A simple event system for any PHP Project
*/
class Event
{
static $events;

/**
* Register an event before it becoming available for use
*/
private static function register(string $eventname): void
{
if (!isset(self::$events[$eventname])) {
self::$events[$eventname] = array();
}
}

/**
* Subscribe to a defined event.
*/
public static function subscribeTo(string $eventname, callable $callback, int $priority = 0): void
{
if (!self::isEvent($eventname)) {
self::register($eventname);
}
self::$events[$eventname][$priority][] = $callback;
}

/**
* Trigger an event, and call all subscribers, giving an array of params.
* returns the data returned by the last subscriber.
*/
public static function dispatch($eventname, $params) : mixed
{
$data = null;
if (!self::isEvent($eventname)) {
self::register($eventname);
}
foreach (self::$events[$eventname] as $key => $weight) {
foreach ($weight as $callback) {
$data = call_user_func_array($callback, $params);
}
}

return $data;
}

/**
* Check that an event is valid before interacting with it.
*
*/
private static function isEvent($eventname)
{
if (!isset(self::$events[$eventname]) || !is_array(self::$events[$eventname])) {
return false;
}
return true;
}
}

9 changes: 3 additions & 6 deletions src/Facade/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@

namespace Scrawler\Arca\Facade;

use Scrawler\Arca\Manager\TableManager;
use Scrawler\Arca\Manager\RecordManager;
use Scrawler\Arca\Manager\ModelManager;

use Scrawler\Arca\Database as DB;

class Database
{
private static $database;

public static function connect(array $connectionParams)
public static function connect(array $connectionParams,$isUsingUUID = false)
{

if (self::$database == null) {
$connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
self::$database = new DB($connection);
self::$database->setManagers(new TableManager(self::$database), new RecordManager(self::$database), new ModelManager(self::$database));
self::$database = new DB($connection,$isUsingUUID);
return self::$database;
}

Expand Down
19 changes: 4 additions & 15 deletions src/Manager/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@

namespace Scrawler\Arca\Manager;

use \Scrawler\Arca\Database;
use \Scrawler\Arca\Model;

/**
* Class for initializing and managing models
*/
class ModelManager {
private Database $db;

/**
* Create ModelManager
* @param \Scrawler\Arca\Database $db
*/
public function __construct(Database $db){
$this->db = $db;
}

/**
* Creates and return models
* @param string $name
* @return Model
*/
public function create(string $name){
return new Model($name,$this->db);
function create(string $name): Model
{
return new Model($name);
}

}
Loading

0 comments on commit c6e32b2

Please sign in to comment.