Skip to content

Commit

Permalink
Merge pull request #49 from ucan-lab/refactor/relation
Browse files Browse the repository at this point in the history
ForeignKey を Relation の名称に統合
  • Loading branch information
ucan-lab authored Sep 2, 2019
2 parents 2333430 + 7fbf12c commit a155db1
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 24 deletions.
1 change: 0 additions & 1 deletion src/Console/DacapoGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace UcanLab\LaravelDacapo\Console;

use Exception;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use UcanLab\LaravelDacapo\Migrations\DacapoGenerator;
Expand Down
2 changes: 1 addition & 1 deletion src/Migrations/DacapoGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function run(): void
foreach ($tables as $table) {
(new GenerateCreateTableMigration($table, $this->migrationsStorage))->run();
(new GenerateCreateIndexMigration($table, $this->migrationsStorage))->run();
(new GenerateConstraintForeignKeyMigration($table, $this->migrationsStorage))->run();
(new GenerateConstraintRelationMigration($table, $this->migrationsStorage))->run();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use UcanLab\LaravelDacapo\Migrations\Schema\Table;
use UcanLab\LaravelDacapo\Storage\Storage;

class GenerateConstraintForeignKeyMigration
class GenerateConstraintRelationMigration
{
private $table;
private $migrationsStorage;
Expand All @@ -21,9 +21,9 @@ public function __construct(Table $table, Storage $migrationsStorage)
*/
public function run(): void
{
if ($this->table->existsForeignKeys()) {
if ($this->table->existsRelations()) {
$stub = $this->getStub();
$path = $this->migrationsStorage->getPath($this->table->getConstraintForeignKeyMigrationFileName());
$path = $this->migrationsStorage->getPath($this->table->getConstraintRelationMigrationFileName());
file_put_contents($path, $stub);
}
}
Expand All @@ -34,7 +34,7 @@ public function run(): void
protected function getStub(): string
{
$stub = file_get_contents(__DIR__ . '/../Storage/stubs/update.stub');
$stub = str_replace('DummyClass', $this->table->getConstraintForeignKeyMigrationClassName(), $stub);
$stub = str_replace('DummyClass', $this->table->getConstraintRelationMigrationClassName(), $stub);
$stub = str_replace('DummyTableName', $this->table->getTableName(), $stub);
$stub = str_replace('DummyTableUpColumn', $this->table->getUpForeignKeyList(), $stub);
$stub = str_replace('DummyTableDownColumn', $this->table->getDownForeignKeyList(), $stub);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace UcanLab\LaravelDacapo\Migrations\Schema;

class ForeignKey
class Relation
{
private $name;
private $foreign;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use IteratorAggregate;
use ArrayIterator;

class ForeignKeys implements IteratorAggregate
class Relations implements IteratorAggregate
{
private $attributes;

Expand All @@ -17,15 +17,15 @@ public function __construct(array $relationsAttributes)
$this->attributes = [];

foreach ($relationsAttributes as $relationAttributes) {
$this->add(new ForeignKey($relationAttributes));
$this->add(new Relation($relationAttributes));
}
}

/**
* @param ForeignKey $foreignKey
* @param Relation $foreignKey
* @return self
*/
public function add(ForeignKey $foreignKey): self
public function add(Relation $foreignKey): self
{
$this->attributes[] = $foreignKey;

Expand Down
24 changes: 12 additions & 12 deletions src/Migrations/Schema/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Table
{
const PREFIX_CREATE_TABLE = 0;
const PREFIX_CREATE_INDEX = 1;
const PREFIX_CONSTRAINT_FOREIGN_KEY = 2;
const PREFIX_CONSTRAINT_RELATION = 2;

private $name;
private $attributes;
Expand All @@ -18,7 +18,7 @@ class Table
private $collation;
private $columns;
private $indexes;
private $foreignKeys;
private $relations;

/**
* @param string $name
Expand All @@ -34,7 +34,7 @@ public function __construct(string $name, array $attributes)
$this->collation = $attributes['collation'] ?? '';
$this->columns = new Columns($attributes['columns'] ?? []);
$this->indexes = new Indexes($this->name, $attributes['indexes'] ?? []);
$this->foreignKeys = new ForeignKeys($attributes['relations'] ?? []);
$this->relations = new Relations($attributes['relations'] ?? []);
}

/**
Expand Down Expand Up @@ -124,17 +124,17 @@ public function getCreateIndexMigrationFileName(): string
/**
* @return string
*/
public function getConstraintForeignKeyMigrationClassName(): string
public function getConstraintRelationMigrationClassName(): string
{
return Str::studly('constraint_' . $this->name . '_foreign_key');
return Str::studly('constraint_' . $this->name . '_relation');
}

/**
* @return string
*/
public function getConstraintForeignKeyMigrationFileName(): string
public function getConstraintRelationMigrationFileName(): string
{
return $this->getDatePrefix(self::PREFIX_CONSTRAINT_FOREIGN_KEY) . '_constraint_' . $this->name . '_foreign_key.php';
return $this->getDatePrefix(self::PREFIX_CONSTRAINT_RELATION) . '_constraint_' . $this->name . '_relation.php';
}

/**
Expand Down Expand Up @@ -192,7 +192,7 @@ public function getDownIndexList(): string
public function getUpForeignKeyList(): string
{
$list = [];
foreach ($this->foreignKeys as $foreignKey) {
foreach ($this->relations as $foreignKey) {
if ($line = $foreignKey->getUpForeignKeyLine()) {
$list[] = $line;
}
Expand All @@ -209,8 +209,8 @@ public function getUpForeignKeyList(): string
public function getDownForeignKeyList(): string
{
$list = [];
foreach ($this->foreignKeys as $foreignKey) {
if ($line = $foreignKey->getDownForeignKeyLine()) {
foreach ($this->relations as $relation) {
if ($line = $relation->getDownForeignKeyLine()) {
$list[] = $line;
}
}
Expand All @@ -231,9 +231,9 @@ public function existsIndexModifiers(): bool
/**
* @return bool
*/
public function existsForeignKeys(): bool
public function existsRelations(): bool
{
return $this->foreignKeys->count() > 0;
return $this->relations->count() > 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class ConstraintTasksForeignKey extends Migration
class ConstraintTasksRelation extends Migration
{
/**
* Run the migrations.
Expand Down
File renamed without changes.

0 comments on commit a155db1

Please sign in to comment.