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

feature/append connection #213

Open
wants to merge 3 commits into
base: v2.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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/reliese.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@
|
*/

'connection' => false,
'AppendConnection' => false,

/*
|--------------------------------------------------------------------------
Expand Down
11 changes: 11 additions & 0 deletions src/Analyser/Doctrine/DoctrineDatabaseAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,15 @@ protected function getSchemaAnalyser(DatabaseBlueprint $databaseBlueprint, strin
$schemaSpecificDoctrineSchemaManager
);
}

/**
* @return string
*/
public function getConnectionName(): string
{
return $this->doctrineDatabaseAssistant->getRelieseConfiguration()
->getDatabaseAnalyserConfiguration()
->getConnectionName()
;
}
}
6 changes: 6 additions & 0 deletions src/Analyser/Doctrine/DoctrineDatabaseAssistantInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Illuminate\Database\ConnectionInterface;
use Reliese\Configuration\RelieseConfiguration;

/**
* Interface DoctrineDatabaseAssistantInterface
Expand All @@ -30,4 +31,9 @@ public function getConnection(string $schemaName): ConnectionInterface;
* @throws Exception
*/
public function getDoctrineSchemaManager(?string $schemaName = null): AbstractSchemaManager;

/**
* @return RelieseConfiguration
*/
public function getRelieseConfiguration(): RelieseConfiguration;
}
14 changes: 13 additions & 1 deletion src/Analyser/Doctrine/DoctrineSchemaAnalyser.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ public function getSchemaBlueprint(): SchemaBlueprint
return $this->schemaBlueprint;
}

return $this->schemaBlueprint = new SchemaBlueprint($this->getDatabaseBlueprint(), $this->getSchemaName());
return $this->schemaBlueprint = new SchemaBlueprint(
$this->getDatabaseBlueprint(),
$this->getSchemaName(),
$this->getConnectionName()
);
}

/**
Expand Down Expand Up @@ -317,4 +321,12 @@ protected function getDatabaseBlueprint(): DatabaseBlueprint
{
return $this->databaseBlueprint;
}

/**
* @return string
*/
public function getConnectionName(): string
{
return $this->doctrineDatabaseAnalyser->getConnectionName();
}
}
14 changes: 14 additions & 0 deletions src/Analyser/Doctrine/MySqlDoctrineDatabaseAssistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
*/
class MySqlDoctrineDatabaseAssistant implements DoctrineDatabaseAssistantInterface
{
/**
* @var RelieseConfiguration
*/
private RelieseConfiguration $relieseConfiguration;

/**
* @var ConnectionInterface
*/
Expand Down Expand Up @@ -40,6 +45,7 @@ public function __construct(
$this->connectionFactory = $connectionFactory;
$this->configuredConnection = $configuredConnection;
$this->schemaConnections[$configuredConnection->getConfig('database')] = $configuredConnection;
$this->relieseConfiguration = $relieseConfiguration;
}

public function getSchemaNames(): array
Expand Down Expand Up @@ -92,4 +98,12 @@ public function getDoctrineSchemaManager(?string $schemaName = null): AbstractSc

return $this->doctrineSchemaManagers[$schemaName] = $doctrineSchemaManager;
}

/**
* @return RelieseConfiguration
*/
public function getRelieseConfiguration(): RelieseConfiguration
{
return $this->relieseConfiguration;
}
}
8 changes: 8 additions & 0 deletions src/Analyser/Doctrine/SqliteDoctrineDatabaseAssistant.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,12 @@ public function getDoctrineSchemaManager(?string $schemaName = null): AbstractSc
{
return $this->doctrineSchemaManagers['default'] ??= $this->getConnection()->getDoctrineSchemaManager();
}

/**
* @return RelieseConfiguration
*/
public function getRelieseConfiguration(): RelieseConfiguration
{
return $this->relieseConfiguration;
}
}
35 changes: 31 additions & 4 deletions src/Blueprint/SchemaBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@
namespace Reliese\Blueprint;

use InvalidArgumentException;

/**
* Class SchemaBlueprint
*/
class SchemaBlueprint
{
/**
* @var string
*/
private string $connectionName;

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

/**
* @var string
*/
private $schemaName;
private string $schemaName;

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

/**
* @var ViewBlueprint[]
Expand All @@ -33,11 +39,13 @@ class SchemaBlueprint
*
* @param DatabaseBlueprint $databaseBlueprint
* @param string $schemaName
* @param string $connectionName
*/
public function __construct(DatabaseBlueprint $databaseBlueprint, string $schemaName)
public function __construct(DatabaseBlueprint $databaseBlueprint, string $schemaName, string $connectionName)
{
$this->schemaName = $schemaName;
$this->databaseBlueprint = $databaseBlueprint;
$this->connectionName = $connectionName;
}

/**
Expand Down Expand Up @@ -121,4 +129,23 @@ public function hasTableBlueprint(string $tableName) : bool
{
return \array_key_exists($tableName, $this->tableBlueprints);
}

/**
* @return string
*/
public function getConnectionName(): string
{
return $this->connectionName;
}

/**
* @param string $connectionName
*
* @return SchemaBlueprint
*/
public function setConnectionName(string $connectionName): static
{
$this->connectionName = $connectionName;
return $this;
}
}
14 changes: 7 additions & 7 deletions src/Blueprint/SchemaMemberTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ trait SchemaMemberTrait
/**
* @var SchemaBlueprint
*/
private $schemaBlueprint;
private SchemaBlueprint $schemaBlueprint;

/**
* @return SchemaBlueprint|null
* @var string
*/
private string $name;

/**
* @return SchemaBlueprint
*/
public function getSchemaBlueprint(): SchemaBlueprint
{
Expand All @@ -32,11 +37,6 @@ public function setSchemaBlueprint(SchemaBlueprint $schemaBlueprint)
$this->schemaBlueprint = $schemaBlueprint;
}

/**
* @var string
*/
private $name;

/**
* Returns the name of that uniquely identifies this schema member
*
Expand Down
3 changes: 1 addition & 2 deletions src/Blueprint/TableBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ class TableBlueprint implements SchemaMemberInterface, ColumnOwnerInterface
* @param SchemaBlueprint $schemaBlueprint
* @param string $tableName
*/
public function __construct(SchemaBlueprint $schemaBlueprint,
string $tableName)
public function __construct(SchemaBlueprint $schemaBlueprint, string $tableName)
{
$this->setSchemaBlueprint($schemaBlueprint);
$this->setName($tableName);
Expand Down
16 changes: 10 additions & 6 deletions src/Blueprint/ViewBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Reliese\Blueprint;

use Doctrine\DBAL\Schema\View;
/**
* Class ViewBlueprint
*/
Expand All @@ -11,11 +10,6 @@ class ViewBlueprint implements SchemaMemberInterface, ColumnOwnerInterface
use SchemaMemberTrait;
use ColumnOwnerTrait;

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

/**
* ViewBlueprint constructor.
*
Expand All @@ -35,4 +29,14 @@ public function getSchemaMemberType(): SchemaMemberType
{
return SchemaMemberType::View();
}

/**
* @inheritDoc
*/
public function getUniqueName(): string
{
return sprintf('%s.%s',
$this->getSchemaBlueprint()->getSchemaName(),
$this->getName());
}
}
38 changes: 31 additions & 7 deletions src/Configuration/ModelGeneratorConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Reliese\Configuration;

use Illuminate\Database\Eloquent\Model;

/**
* Class ModelGeneratorConfiguration
*/
Expand All @@ -13,6 +15,7 @@ class ModelGeneratorConfiguration
const KEY_PARENT_CLASS_PREFIX = 'ParentClassPrefix';
const KEY_PARENT = 'Parent';
const KEY_TRAITS = 'Traits';
const KEY_APPEND_CONNECTION = 'AppendConnection';

/**
* @var string
Expand Down Expand Up @@ -44,23 +47,25 @@ class ModelGeneratorConfiguration
*/
private array $traits = [];

/**
* @var bool
*/
private bool $appendConnection;

/**
* ModelGeneratorConfiguration constructor.
*
* @param array $configuration
*/
public function __construct(array $configuration = [])
{
if (empty($configuration)) {
return ;
}

$this->setPath($configuration[static::KEY_PATH]);
$this->setNamespace($configuration[static::KEY_NAMESPACE]);
$this->setPath($configuration[static::KEY_PATH] ?? '');
$this->setNamespace($configuration[static::KEY_NAMESPACE] ?? '');
$this->setClassSuffix($configuration[static::KEY_CLASS_SUFFIX] ?? '');
$this->setParentClassPrefix($configuration[static::KEY_PARENT_CLASS_PREFIX] ?? '');
$this->setParent($configuration[static::KEY_PARENT]);
$this->setParent($configuration[static::KEY_PARENT] ?? Model::class);
$this->setTraits($configuration[static::KEY_TRAITS] ?? []);
$this->appendConnection($configuration[static::KEY_APPEND_CONNECTION] ?? false);
}

/**
Expand Down Expand Up @@ -181,4 +186,23 @@ public function getTraits(): array
{
return $this->traits;
}

/**
* @param bool $append
*
* @return $this
*/
public function appendConnection(bool $append): static
{
$this->appendConnection = $append;
return $this;
}

/**
* @return bool
*/
public function appendsConnection(): bool
{
return $this->appendConnection;
}
}
25 changes: 24 additions & 1 deletion src/Generator/Model/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
class ModelGenerator
{
const PROPERTY_TABLE = 'table';
const PROPERTY_CONNECTION = 'connection';

private MySqlDataTypeMap $dataTypeMap;

Expand Down Expand Up @@ -221,6 +222,10 @@ private function generateProperties(TableBlueprint $tableBlueprint): array
$properties[] = $this->generateTableProperty($tableBlueprint);
}

if ($this->modelGeneratorConfiguration->appendsConnection()) {
$properties[] = $this->generateConnectionProperty($tableBlueprint);
}

return $properties;
}

Expand All @@ -241,7 +246,7 @@ private function generateTableProperty(TableBlueprint $tableBlueprint): ClassPro
{
$property = new ClassPropertyDefinition(
static::PROPERTY_TABLE,
PhpTypeEnum::stringType(),
PhpTypeEnum::absentTypeEnum(),
VisibilityEnum::protectedEnum()
);

Expand Down Expand Up @@ -272,4 +277,22 @@ private function generateTraits(TableBlueprint $tableBlueprint): array

return $traitDefinitions;
}

/**
* @param TableBlueprint $tableBlueprint
*
* @return ClassPropertyDefinition
*/
private function generateConnectionProperty(TableBlueprint $tableBlueprint): ClassPropertyDefinition
{
$property = new ClassPropertyDefinition(
static::PROPERTY_CONNECTION,
PhpTypeEnum::absentTypeEnum(),
VisibilityEnum::protectedEnum()
);

$property->setValue($tableBlueprint->getSchemaBlueprint()->getConnectionName());

return $property;
}
}
Loading