-
Notifications
You must be signed in to change notification settings - Fork 323
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
AdamLenda
wants to merge
5
commits into
reliese:v1.x
Choose a base branch
from
JVZoo:add_blueprints_namespace
base: v1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0f0ad85
Created DatabaseInterface and simple implementations
AdamLenda dd89526
added file omitted from prior commit
AdamLenda c42d798
added implements statements for DatabaseAdapterInterface
AdamLenda ce917b6
Added Adapter Factory
AdamLenda 90b439a
Added Blueprints namespace to expand the existing Blueprint class int…
AdamLenda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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; | ||
} | ||
} |
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,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; | ||
/** | ||
* @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 | ||
} |
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,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 | ||
} |
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,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; | ||
} | ||
} |
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,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); | ||
} | ||
} |
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 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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.