Skip to content
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

Expand Blueprint into DatabaseBlueprint, SchemaBlueprint, and TableBlueprint #201

Open
wants to merge 5 commits into
base: v1.x
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 80 additions & 0 deletions src/Blueprint/BlueprintFactory.php
@@ -0,0 +1,80 @@
<?php

namespace Reliese\Blueprint;

use Illuminate\Database\DatabaseManager;
use Illuminate\Database\MySqlConnection;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\SQLiteConnection;
use Reliese\Coders\Model\Config;

use Reliese\Meta\AdapterFactory;
use Reliese\Meta\DatabaseInterface;
use Reliese\Meta\MySql\Database as MySqlDatabase;
use Reliese\Meta\Postgres\Database as PostgresDatabase;
use Reliese\Meta\Sqlite\Database as SqliteDatabase;

use function get_class;

/**
* Class DatabaseFactory
*/
class BlueprintFactory
{
/**
* @var DatabaseBlueprint
*/
private $laravelDatabaseManager;

/**
* @var DatabaseBlueprint[]
*/
private $databaseBlueprints = [];

/**
* @var Config
*/
private $config;

/**
* @var AdapterFactory
*/
private $adapterFactory;

/**
* BlueprintFactory constructor.
* @param DatabaseManager $databaseManager
* @param AdapterFactory $adapterFactory
* @param Config $config
*/
public function __construct(
$adapterFactory,
$databaseManager,
$config
) {
$this->laravelDatabaseManager = $databaseManager;
$this->config = $config;
$this->adapterFactory = $adapterFactory;
}

/**
* @param $connectionName
* @return DatabaseBlueprint
*/
public function database($connectionName)
{
if (!empty($this->databaseBlueprints[$connectionName])) {
return $this->databaseBlueprints[$connectionName];
}

$connection = $this->laravelDatabaseManager->connection($connectionName);

$databaseBlueprint = new DatabaseBlueprint(
$this->adapterFactory->database($connection),
$connectionName,
$connection
);

return $this->databaseBlueprints[$connectionName] = $databaseBlueprint;
}
}
77 changes: 77 additions & 0 deletions src/Blueprint/DatabaseBlueprint.php
@@ -0,0 +1,77 @@
<?php

namespace Reliese\Blueprint;

use Reliese\Meta\DatabaseInterface;
use RuntimeException;

/**
* Class DatabaseManager
*/
class DatabaseBlueprint
{
/**
* The name of the connection from the Laravel config/database.php file
*
* @var string
*/
private $connectionName;

/**
* @var \Illuminate\Database\Connection
*/
private $connection;

/**
* @var SchemaBlueprint[]
*/
private $schemaBlueprints;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even though PHP might work this way, we should give an initial value to our variables.

Suggested change
private $schemaBlueprints;
private $schemaBlueprints = [];

/**
* @var DatabaseInterface
*/
private $databaseAdapter;

/**
* DatabaseBlueprint constructor.
* @param DatabaseInterface $databaseAdapter
* @param string $connectionName
* @param \Illuminate\Database\Connection $connection
*/
public function __construct(
$databaseAdapter,
$connectionName,
$connection
) {
$this->connectionName = $connectionName;
$this->connection = $connection;
$this->databaseAdapter = $databaseAdapter;
}

/**
* If a schema name is not provided, then the default schema for the connection will be used
* @param string|null $schemaName
* @return SchemaBlueprint
*/
public function schema($schemaName)
{
if (!empty($this->schemaBlueprints[$schemaName])) {
return $this->schemaBlueprints[$schemaName];
}

return $this->schemaBlueprints[$schemaName] = new SchemaBlueprint(
$this,
$this->databaseAdapter->getSchema($schemaName),
$schemaName
);
}

# region Accessors
/**
* @return \Illuminate\Database\Connection
*/
public function getConnection()
{
return $this->connection;
}
# endregion Accessors
}
85 changes: 85 additions & 0 deletions src/Blueprint/SchemaBlueprint.php
@@ -0,0 +1,85 @@
<?php

namespace Reliese\Blueprint;

use Reliese\Meta\Schema;
use Reliese\Meta\SchemaManager;

use function get_class;

/**
* Class SchemaBlueprint
*/
class SchemaBlueprint
{
/**
* @var
*/
private $schemaName;

/**
* @var
*/
private $databaseBlueprint;

/**
* @var TableBlueprint[]
*/
private $tableBlueprints;

/**
* @deprecated The SchemaBlueprint class should replace usage of SchemaManager. To maintain backwards compatibility,
* SchemaBlueprint wraps SchemaManager
*
* @var SchemaManager
*/
private $schemaManager;
/**
* @var Schema
*/
private $schemaAdapter;

/**
* SchemaBlueprint constructor.
* @param Schema $schemaAdapter
* @param string $schemaName
* @param DatabaseBlueprint $databaseBlueprint
*/
public function __construct(
$databaseBlueprint,
Schema $schemaAdapter,
$schemaName
) {
$this->schemaAdapter = $schemaAdapter;
$this->schemaName = $schemaName;
$this->databaseBlueprint = $databaseBlueprint;

$this->schemaManager = new SchemaManager(
$databaseBlueprint->getConnection()
);
}

public function table($tableName)
{
if (!empty($this->tableBlueprints[$tableName])) {
return $this->tableBlueprints[$tableName];
}

$blueprint = $this->schemaAdapter->table($tableName);

return $this->tableBlueprints[$tableName] = new TableBlueprint(
$this,
$tableName,
$blueprint
);
}

public function schemaAdapter()
{

}

# region Accessors

# endregion Accessors
}
40 changes: 40 additions & 0 deletions src/Blueprint/TableBlueprint.php
@@ -0,0 +1,40 @@
<?php

namespace Reliese\Blueprint;

use Reliese\Meta\Blueprint as DeprecatedTableBlueprint;

/**
* Class TableBlueprint
*/
class TableBlueprint
{
/**
* @var string
*/
private $tableName;

/**
* @var SchemaBlueprint
*/
private $schemaBlueprint;
/**
* @var DeprecatedTableBlueprint
*/
private $deprecatedTableBlueprint;

/**
* TableBlueprint constructor.
* @param $tableName
* @param SchemaBlueprint $schemaBlueprint
*/
public function __construct(
SchemaBlueprint $schemaBlueprint,
$tableName,
DeprecatedTableBlueprint $depricatedTableBlueprint
) {
$this->tableName = $tableName;
$this->schemaBlueprint = $schemaBlueprint;
$this->deprecatedTableBlueprint = $depricatedTableBlueprint;
}
}
43 changes: 43 additions & 0 deletions src/Meta/AdapterFactory.php
@@ -0,0 +1,43 @@
<?php

namespace Reliese\Meta;

use Illuminate\Database\MySqlConnection;
use Illuminate\Database\PostgresConnection;
use Illuminate\Database\SQLiteConnection;
use Reliese\Meta\MySql\Database as MySqlDatabase;
use Reliese\Meta\Postgres\Database as PostgresDatabase;
use Reliese\Meta\Sqlite\Database as SqliteDatabase;
use Reliese\Meta\MySql\Schema as MySqlSchema;
use Reliese\Meta\Postgres\Schema as PostgresSchema;
use Reliese\Meta\Sqlite\Schema as SqliteSchema;

use function get_class;

/**
* Class AdapterFactory
*/
class AdapterFactory
{
/**
* @param \Illuminate\Database\Connection $connection
* @return DatabaseInterface
*/
public function database($connection)
{
switch (get_class($connection)) {
case \Larapack\DoctrineSupport\Connections\MySqlConnection::class:
case MySqlConnection::class:
/** @noinspection PhpParamsInspection */
return new MySqlDatabase($connection);
case SQLiteConnection::class:
/** @noinspection PhpParamsInspection */
return new SqliteDatabase($connection);
case PostgresConnection::class:
/** @noinspection PhpParamsInspection */
return new PostgresDatabase($connection);
}

throw new \RuntimeException(__METHOD__." does not define an implementation for ".DatabaseInterface::class);
}
}
24 changes: 24 additions & 0 deletions src/Meta/DatabaseInterface.php
@@ -0,0 +1,24 @@
<?php

namespace Reliese\Meta;

use Reliese\Meta\MySql\Schema;

/**
* Interface DatabaseInterface
*/
interface DatabaseInterface
{
/**
* Returns an array of accessible schema names
*
* @return string[]
*/
public function getSchemaNames();

/**
* @param string $schemaName
* @return Schema
*/
public function getSchema($schemaName);
}