Skip to content
Open
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
174 changes: 117 additions & 57 deletions src/commands/MigrationCommand.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
<?php namespace Zizaco\Entrust;
<?php

/**
* This file is part of Entrust,
* a role & permission management solution for Laravel.
*
* @license MIT
* @package Zizaco\Entrust
*/
namespace Zizaco\Entrust;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
Expand All @@ -26,86 +20,152 @@ class MigrationCommand extends Command
* @var string
*/
protected $description = 'Creates a migration following the Entrust specifications.';

/**
* Execute the console command.
*
* @return void
* Execute the console command (compatibility with Laravel < 5.5).
*/
public function fire()
{
$this->handle();
}

/**
* Execute the console command for Laravel 5.5+.
*
* @return void
* Execute the console command.
*/
public function handle()
public function handle(): void
{
$this->laravel->view->addNamespace('entrust', substr(__DIR__, 0, -8).'views');
$this->loadViews();

$tables = [
'roles' => Config::get('entrust.roles_table'),
'role_user' => Config::get('entrust.role_user_table'),
'permissions' => Config::get('entrust.permissions_table'),
'permission_role' => Config::get('entrust.permission_role_table')
];

$this->displayTableInfo($tables);

if ($this->confirmCreation()) {
$this->createMigrationFile($tables);
}
}

$rolesTable = Config::get('entrust.roles_table');
$roleUserTable = Config::get('entrust.role_user_table');
$permissionsTable = Config::get('entrust.permissions_table');
$permissionRoleTable = Config::get('entrust.permission_role_table');
/**
* Load the package views.
*/
protected function loadViews(): void
{
$this->laravel->view->addNamespace('entrust', substr(__DIR__, 0, -8).'views');
}

/**
* Display table information to the user.
*/
protected function displayTableInfo(array $tables): void
{
$this->line('');
$this->info( "Tables: $rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable" );

$message = "A migration that creates '$rolesTable', '$roleUserTable', '$permissionsTable', '$permissionRoleTable'".
" tables will be created in database/migrations directory";

$this->info('Tables: '.implode(', ', $tables));
$message = "A migration that creates '".implode("', '", $tables).
"' tables will be created in database/migrations directory";
$this->comment($message);
$this->line('');
}

if ($this->confirm("Proceed with the migration creation? [Yes|no]", "Yes")) {

$this->line('');

$this->info("Creating migration...");
if ($this->createMigration($rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable)) {
/**
* Confirm with user before creating migration.
*/
protected function confirmCreation(): bool
{
return $this->confirm(
"Proceed with the migration creation? [Yes|no]",
"Yes"
);
}

$this->info("Migration successfully created!");
} else {
$this->error(
"Couldn't create migration.\n Check the write permissions".
" within the database/migrations directory."
);
}
/**
* Create the migration file.
*/
protected function createMigrationFile(array $tables): void
{
$this->info("Creating migration...");

if ($this->createMigration(
$tables['roles'],
$tables['role_user'],
$tables['permissions'],
$tables['permission_role']
)) {
$this->info("Migration successfully created!");
} else {
$this->error(
"Couldn't create migration.\nCheck the write permissions ".
"within the database/migrations directory."
);
}

$this->line('');
}

$this->line('');
/**
* Generate the migration file content.
*/
protected function createMigration(
string $rolesTable,
string $roleUserTable,
string $permissionsTable,
string $permissionRoleTable
): bool {
$migrationPath = $this->getMigrationPath();
$data = $this->getMigrationData($rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable);

$output = $this->laravel->view->make('entrust::generators.migration')->with($data)->render();

}
return $this->writeMigrationFile($migrationPath, $output);
}

/**
* Create the migration.
*
* @param string $name
*
* @return bool
* Get the path for the migration file.
*/
protected function createMigration($rolesTable, $roleUserTable, $permissionsTable, $permissionRoleTable)
protected function getMigrationPath(): string
{
$migrationFile = base_path("/database/migrations")."/".date('Y_m_d_His')."_entrust_setup_tables.php";
return database_path('migrations/'.date('Y_m_d_His').'_entrust_setup_tables.php');
}

/**
* Prepare data for the migration template.
*/
protected function getMigrationData(
string $rolesTable,
string $roleUserTable,
string $permissionsTable,
string $permissionRoleTable
): array {
$userModelName = Config::get('auth.providers.users.model');
$userModel = new $userModelName();
$usersTable = $userModel->getTable();
$userKeyName = $userModel->getKeyName();

$data = compact('rolesTable', 'roleUserTable', 'permissionsTable', 'permissionRoleTable', 'usersTable', 'userKeyName');

$output = $this->laravel->view->make('entrust::generators.migration')->with($data)->render();

return [
'rolesTable' => $rolesTable,
'roleUserTable' => $roleUserTable,
'permissionsTable' => $permissionsTable,
'permissionRoleTable' => $permissionRoleTable,
'usersTable' => $userModel->getTable(),
'userKeyName' => $userModel->getKeyName()
];
}

if (!file_exists($migrationFile) && $fs = fopen($migrationFile, 'x')) {
fwrite($fs, $output);
/**
* Write the migration file to disk.
*/
protected function writeMigrationFile(string $path, string $content): bool
{
if (!file_exists($path) && $fs = fopen($path, 'x')) {
fwrite($fs, $content);
fclose($fs);
return true;
}

return false;
}
}