Skip to content

Commit

Permalink
pint
Browse files Browse the repository at this point in the history
  • Loading branch information
recca0120 committed Jan 18, 2023
1 parent f2d02d6 commit bfc1a90
Show file tree
Hide file tree
Showing 41 changed files with 150 additions and 156 deletions.
3 changes: 2 additions & 1 deletion database/migrations/2022_11_17_000001_create_cars_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
14 changes: 7 additions & 7 deletions database/migrations/2022_12_07_000004_add_teams_fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Illuminate\Support\Facades\Schema;
use Spatie\Permission\PermissionRegistrar;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand All @@ -18,7 +19,7 @@ public function up()
$tableNames = config('permission.table_names');
$columnNames = config('permission.column_names');

if (!$teams) {
if (! $teams) {
return;
}
if (empty($tableNames)) {
Expand All @@ -28,7 +29,7 @@ public function up()
throw new \Exception('Error: team_foreign_key on config/permission.php not loaded. Run [php artisan config:clear] and try again.');
}

if (!Schema::hasColumn($tableNames['roles'], $columnNames['team_foreign_key'])) {
if (! Schema::hasColumn($tableNames['roles'], $columnNames['team_foreign_key'])) {
Schema::table($tableNames['roles'], function (Blueprint $table) use ($columnNames) {
$table->unsignedBigInteger($columnNames['team_foreign_key'])->nullable()->after('id');
$table->index($columnNames['team_foreign_key'], 'roles_team_foreign_key_index');
Expand All @@ -38,7 +39,7 @@ public function up()
});
}

if (!Schema::hasColumn($tableNames['model_has_permissions'], $columnNames['team_foreign_key'])) {
if (! Schema::hasColumn($tableNames['model_has_permissions'], $columnNames['team_foreign_key'])) {
Schema::table($tableNames['model_has_permissions'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');
$table->index($columnNames['team_foreign_key'], 'model_has_permissions_team_foreign_key_index');
Expand All @@ -57,9 +58,9 @@ public function up()
});
}

if (!Schema::hasColumn($tableNames['model_has_roles'], $columnNames['team_foreign_key'])) {
if (! Schema::hasColumn($tableNames['model_has_roles'], $columnNames['team_foreign_key'])) {
Schema::table($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames) {
$table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');;
$table->unsignedBigInteger($columnNames['team_foreign_key'])->default('1');
$table->index($columnNames['team_foreign_key'], 'model_has_roles_team_foreign_key_index');

if (DB::getDriverName() !== 'sqlite') {
Expand Down Expand Up @@ -88,6 +89,5 @@ public function up()
*/
public function down()
{

}
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Illuminate\Support\Facades\Schema;
use Spatie\Permission\PermissionRegistrar;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down Expand Up @@ -70,7 +71,6 @@ public function up()
$table->primary([PermissionRegistrar::$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
}

});

Schema::create($tableNames['model_has_roles'], function (Blueprint $table) use ($tableNames, $columnNames, $teams) {
Expand Down Expand Up @@ -137,4 +137,4 @@ public function down()
Schema::drop($tableNames['roles']);
Schema::drop($tableNames['permissions']);
}
};
};
3 changes: 2 additions & 1 deletion database/migrations/2022_12_09_000006_create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
/**
* Run the migrations.
*
Expand Down
4 changes: 2 additions & 2 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use Illuminate\Support\Facades\Route;
use Recca0120\LaravelErd\Http\Controllers\LaravelErdController;

Route::get(config('laravel-erd.uri') . '/{file?}', [LaravelErdController::class, 'index'])
Route::get(config('laravel-erd.uri').'/{file?}', [LaravelErdController::class, 'index'])
->name('laravel-erd.show')
->middleware(config('laravel-erd.middleware'))
->where('file', '.*');
->where('file', '.*');
6 changes: 3 additions & 3 deletions src/Console/Commands/LaravelErdCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ private function setConnection(string $connection): void
{
config(
collect(Arr::dot(config()->all()))
->filter(fn($value, $key) => $value && Str::endsWith($key, 'database.connection'))
->map(fn() => $connection)
->filter(fn ($value, $key) => $value && Str::endsWith($key, 'database.connection'))
->map(fn () => $connection)
->merge(['database.default' => $connection])
->toArray()
);
}
}
}
11 changes: 4 additions & 7 deletions src/Console/Commands/LaravelErdInitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function handle(): int

return self::FAILURE;
}

}

/**
Expand All @@ -49,7 +48,6 @@ private function downloadDot(string $os, string $arch, string $path): void
$this->download($url, $path);
}


/**
* @throws RequestException
*/
Expand All @@ -59,17 +57,17 @@ private function download(string $url, string $path): void
return;
}

$this->line('download: ' . $url);
$this->line('download: '.$url);
File::ensureDirectoryExists(dirname($path));
File::put($path, Http::timeout(300)->get($url)->throw()->body());
File::chmod($path, 0777);
}

private function arch(): string
{
$name = php_uname("m");
$name = php_uname('m');

if (false !== stripos($name, "aarch64") || false !== stripos($name, "arm64")) {
if (false !== stripos($name, 'aarch64') || false !== stripos($name, 'arm64')) {
return 'arm';
}

Expand All @@ -93,5 +91,4 @@ private function os(): string

return 'linux';
}

}
}
48 changes: 25 additions & 23 deletions src/ErdFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
class ErdFinder
{
private AbstractSchemaManager $schemaManager;

private ModelFinder $modelFinder;

private RelationFinder $relationFinder;

private string $directory;

public function __construct(
AbstractSchemaManager $schemaManager,
ModelFinder $modelFinder,
RelationFinder $relationFinder
)
{
ModelFinder $modelFinder,
RelationFinder $relationFinder
) {
$this->schemaManager = $schemaManager;
$this->modelFinder = $modelFinder;
$this->relationFinder = $relationFinder;
Expand All @@ -31,8 +33,8 @@ public function in(string $directory): ErdFinder
}

/**
* @param string|string[] $patterns
* @param string[] $excludes
* @param string|string[] $patterns
* @param string[] $excludes
* @return Collection<int|string, Table>
*/
public function find($patterns = '*.php', array $excludes = []): Collection
Expand All @@ -43,8 +45,8 @@ public function find($patterns = '*.php', array $excludes = []): Collection
}

/**
* @param string|string[] $file
* @param string[] $excludes
* @param string|string[] $file
* @param string[] $excludes
* @return Collection<int|string, Table>
*/
public function findByFile($file, array $excludes = []): Collection
Expand All @@ -55,8 +57,8 @@ public function findByFile($file, array $excludes = []): Collection
}

/**
* @param string $className
* @param string[] $excludes
* @param string $className
* @param string[] $excludes
* @return Collection<int|string, Table>
*/
public function findByModel(string $className, array $excludes = []): Collection
Expand All @@ -65,38 +67,38 @@ public function findByModel(string $className, array $excludes = []): Collection
}

/**
* @param Collection $models
* @param string[] $excludes
* @param Collection $models
* @param string[] $excludes
* @return Collection<int|string, Table>
*/
private function findByModels(Collection $models, array $excludes = []): Collection
{
$missing = $models
->flatMap(fn(string $model) => $this->relationFinder->generate($model)->collapse())
->map(fn(Relation $relation) => $relation->related())
->flatMap(fn (string $model) => $this->relationFinder->generate($model)->collapse())
->map(fn (Relation $relation) => $relation->related())
->filter()
->diff($models);

return $models
->merge($missing)
->flatMap(fn(string $model) => $this->relationFinder->generate($model)->collapse())
->flatMap(fn(Relation $relation) => [$relation, $relation->relatedRelation()])
->reject(fn(Relation $relation) => $relation->includes($excludes))
->sortBy(fn(Relation $relation) => $this->uniqueRelation($relation))
->unique(fn(Relation $relation) => $this->uniqueRelation($relation))
->groupBy(fn(Relation $relation) => $relation->table())
->sortBy(fn(Collection $relations, string $table) => $table)
->flatMap(fn (string $model) => $this->relationFinder->generate($model)->collapse())
->flatMap(fn (Relation $relation) => [$relation, $relation->relatedRelation()])
->reject(fn (Relation $relation) => $relation->includes($excludes))
->sortBy(fn (Relation $relation) => $this->uniqueRelation($relation))
->unique(fn (Relation $relation) => $this->uniqueRelation($relation))
->groupBy(fn (Relation $relation) => $relation->table())
->sortBy(fn (Collection $relations, string $table) => $table)
->map(function (Collection $relations, $table) {
return new Table($this->schemaManager->introspectTable($table), $relations);
});
}

/**
* @param Relation $relation
* @param Relation $relation
* @return string[]
*/
private function uniqueRelation(Relation $relation): array
{
return [$relation->type(), $relation->localKey(), $relation->foreignKey()];
}
}
}
3 changes: 1 addition & 2 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ public static function getColumnType(Column $column): string
return 'unknown';
}
}

}
}
6 changes: 3 additions & 3 deletions src/Http/Controllers/LaravelErdController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public function index(string $file = 'laravel-erd.sql'): View
{
$storagePath = config('laravel-erd.storage_path');
$extension = substr($file, strrpos($file, '.') + 1);
$path = $storagePath . '/' . $file;
$path = $storagePath.'/'.$file;

abort_unless(File::exists($path), 404);

$view = $extension === 'svg' ? 'svg' : 'vuerd';

return view('laravel-erd::' . $view, ['path' => $path]);
return view('laravel-erd::'.$view, ['path' => $path]);
}
}
}
2 changes: 1 addition & 1 deletion src/LaravelErdServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ public function register()
LaravelErdCommand::class,
]);
}
}
}
Loading

0 comments on commit bfc1a90

Please sign in to comment.