Skip to content
Closed
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
62 changes: 62 additions & 0 deletions src/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class MigrationGenerator implements Generator
/** @var \Illuminate\Contracts\Filesystem\Filesystem */
private $files;

private $pivotTables = [];

public function __construct($files)
{
$this->files = $files;
Expand All @@ -34,6 +36,26 @@ public function output(array $tree): array
$this->files->put($path, $this->populateStub($stub, $model));

$output['created'][] = $path;

if (!empty($modelPivots = $model->pivotTables())) {
foreach ($modelPivots as $pivotSegments) {
$pivotTable = $this->getPivotTableName($pivotSegments);
if (!isset($this->pivotTables[$pivotTable])) {
$this->pivotTables[$pivotTable] = [
'tableName' => $pivotTable,
'segments' => $pivotSegments
];
}
}
}
}

if (!empty($this->pivotTables)) {
foreach ($this->pivotTables as $pivotTable) {
$path = $this->getPivotTablePath($pivotTable['tableName'], $sequential_timestamp->addSecond());
$this->files->put($path, $this->populatePivotStub($stub, $pivotTable['segments']));
$output['created'][] = $path;
}
}

return $output;
Expand All @@ -48,6 +70,15 @@ protected function populateStub(string $stub, Model $model)
return $stub;
}

protected function populatePivotStub(string $stub, array $segments)
{
$stub = str_replace('DummyClass', $this->getPivotClassName($segments), $stub);
$stub = str_replace('DummyTable', $this->getPivotTableName($segments), $stub);
$stub = str_replace('// definition...', $this->buildPivotTableDefinition($segments), $stub);

return $stub;
}

protected function buildDefinition(Model $model)
{
$definition = '';
Expand Down Expand Up @@ -107,6 +138,18 @@ protected function buildDefinition(Model $model)
return trim($definition);
}

protected function buildPivotTableDefinition(array $segments, $dataType = 'bigIncrements')
{
$definition = '';

foreach ($segments as $segment) {
$column = $segment . '_id';
$definition .= self::INDENT . '$table->' . $dataType . "('{$column}');" . PHP_EOL;
}

return trim($definition);
}

protected function getClassName(Model $model)
{
return 'Create' . Str::studly($model->tableName()) . 'Table';
Expand All @@ -117,8 +160,27 @@ protected function getPath(Model $model, Carbon $timestamp)
return 'database/migrations/' . $timestamp->format('Y_m_d_His') . '_create_' . $model->tableName() . '_table.php';
}

protected function getPivotTablePath($tableName, Carbon $timestamp)
{
return 'database/migrations/' . $timestamp->format('Y_m_d_His') . '_create_' . $tableName . '_table.php';
}

protected function isLaravel7orNewer()
{
return version_compare(App::version(), '7.0.0', '>=');
}

protected function getPivotClassName(array $segments)
{
return 'Create' . Str::studly($this->getPivotTableName($segments)) . 'PivotTable';
}

protected function getPivotTableName(array $segments)
{
$segments = array_map(function ($name) {
return Str::snake($name);
}, $segments);
sort($segments);
return strtolower(implode('_', $segments));
}
}
2 changes: 1 addition & 1 deletion src/Generators/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ private function buildRelationships(Model $model)
$class = Str::studly($class ?? $name);
$relationship = sprintf("\$this->%s(%s::class)", $type, '\\' . $model->fullyQualifiedNamespace() . '\\' . $class);

$method_name = $type === 'hasMany' ? Str::plural($name) : $name;
$method_name = $type === 'hasMany' || $type === 'belongsToMany' ? Str::plural($name) : $name;
$method = str_replace('DummyName', Str::camel($method_name), $template);
$method = str_replace('null', $relationship, $method);
$methods .= PHP_EOL . $method;
Expand Down
1 change: 1 addition & 0 deletions src/Lexers/ModelLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ModelLexer implements Lexer
'belongsto' => 'belongsTo',
'hasone' => 'hasOne',
'hasmany' => 'hasMany',
'belongstomany' => 'belongsToMany'
];

private static $dataTypes = [
Expand Down
17 changes: 17 additions & 0 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Model
private $softDeletes = false;
private $columns = [];
private $relationships = [];
private $pivotTables = [];

/**
* @param $name
Expand Down Expand Up @@ -132,6 +133,22 @@ public function addRelationship(string $type, string $reference)
$this->relationships[$type] = [];
}

if ($type === 'belongsToMany') {
$this->addPivotTable($reference);
}

$this->relationships[$type][] = $reference;
}

public function addPivotTable(string $reference)
{
$segments = [$this->name(), strtolower($reference)];
sort($segments);
$this->pivotTables[] = $segments;
}

public function pivotTables(): array
{
return $this->pivotTables;
}
}