Skip to content

Commit

Permalink
Migration and Factory make commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sarthaksavvy committed Oct 10, 2018
1 parent e68f060 commit 51c8e50
Show file tree
Hide file tree
Showing 16 changed files with 608 additions and 15 deletions.
87 changes: 87 additions & 0 deletions app/Commands/Foundation/Factories/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace App\Commands\Foundation\Factories;

use Illuminate\Console\GeneratorCommand;
use Symfony\Component\Console\Input\InputOption;
use App\Commands\Helpers\PackageDetail;
use Illuminate\Support\Str;

class FactoryMakeCommand extends GeneratorCommand
{
use PackageDetail;
/**
* The console command name.
*
* @var string
*/
protected $name = 'make:factory';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new model factory';

/**
* The type of class being generated.
*
* @var string
*/
protected $type = 'Factory';

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStub()
{
return __DIR__ . '/stubs/factory.stub';
}

/**
* Build the class with the given name.
*
* @param string $name
* @return string
*/
protected function buildClass($name)
{
$model = $this->option('model')
? $this->qualifyClass($this->option('model'))
: 'Model';

return str_replace(
'DummyModel',
$model,
parent::buildClass($name)
);
}

/**
* Get the destination class path.
*
* @param string $name
* @return string
*/
protected function getPath($name)
{
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
$path = getcwd() . $this->devPath() . '/src/';
return $path . '/database/factories/' . str_replace('\\', '/', $name) . '.php';
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'],
];
}
}
9 changes: 9 additions & 0 deletions app/Commands/Foundation/Factories/stubs/factory.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

use Faker\Generator as Faker;

$factory->define(DummyModel::class, function (Faker $faker) {
return [
//
];
});
153 changes: 153 additions & 0 deletions app/Commands/Foundation/Migrations/MigrateMakeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php

namespace App\Commands\Foundation\Migrations;

use Illuminate\Support\Str;
use Illuminate\Support\Composer;
use Illuminate\Database\Console\Migrations\BaseCommand;

class MigrateMakeCommand extends BaseCommand
{
/**
* The console command signature.
*
* @var string
*/
protected $signature = 'make:migration {name : The name of the migration}
{--create= : The table to be created}
{--table= : The table to migrate}
{--path= : The location where the migration file should be created}
{--realpath : Indicate any provided migration file paths are pre-resolved absolute paths}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a new migration file';

/**
* The migration creator instance.
*
* @var \Illuminate\Database\Migrations\MigrationCreator
*/
protected $creator;

/**
* The Composer instance.
*
* @var \Illuminate\Support\Composer
*/
protected $composer;

/**
* Create a new migration install command instance.
*
* @param \Illuminate\Database\Migrations\MigrationCreator $creator
* @param \Illuminate\Support\Composer $composer
* @return void
*/
public function __construct(MigrationCreator $creator, Composer $composer)
{
parent::__construct();

$this->creator = $creator;
$this->composer = $composer;
}

/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
// It's possible for the developer to specify the tables to modify in this
// schema operation. The developer may also specify if this table needs
// to be freshly created so we can create the appropriate migrations.
$name = Str::snake(trim($this->input->getArgument('name')));

$table = $this->input->getOption('table');

$create = $this->input->getOption('create') ?: false;

// If no table was given as an option but a create option is given then we
// will use the "create" option as the table name. This allows the devs
// to pass a table name into this option as a short-cut for creating.
if (!$table && is_string($create)) {
$table = $create;

$create = true;
}

// Next, we will attempt to guess the table name if this the migration has
// "create" in the name. This will allow us to provide a convenient way
// of creating migrations that create new tables for the application.
if (!$table) {
[$table, $create] = TableGuesser::guess($name);
}

// Now we are ready to write the migration out to disk. Once we've written
// the migration out, we will dump-autoload for the entire framework to
// make sure that the migrations are registered by the class loaders.
$this->writeMigration($name, $table, $create);

$this->composer->dumpAutoloads();
}

/**
* Write the migration file to disk.
*
* @param string $name
* @param string $table
* @param bool $create
* @return string
*/
protected function writeMigration($name, $table, $create)
{
$file = pathinfo($this->creator->create(
$name,
$this->getMigrationPath(),
$table,
$create
), PATHINFO_FILENAME);

$this->line("<info>Created Migration:</info> {$file}");
}

/**
* Get migration path (either specified by '--path' option or default location).
*
* @return string
*/
protected function getMigrationPath()
{
if (!is_null($targetPath = $this->input->getOption('path'))) {
return !$this->usingRealPath()
? $this->laravel->basePath() . '/' . $targetPath
: $targetPath;
}

return parent::getMigrationPath();
}

/**
* Determine if the given path(s) are pre-resolved "real" paths.
*
* @return bool
*/
protected function usingRealPath()
{
return $this->input->hasOption('realpath') && $this->option('realpath');
}

/**
* Get the path to the stubs.
*
* @return string
*/
public function stubPath()
{
return __DIR__ . '/stubs';
}
}
27 changes: 27 additions & 0 deletions app/Commands/Foundation/Migrations/MigrationCreator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Commands\Foundation\Migrations;

use Illuminate\Database\Migrations\MigrationCreator as RealMigrationCreator;
use App\Commands\Helpers\PackageDetail;

class MigrationCreator extends RealMigrationCreator
{
use PackageDetail;

/**
* Get the full path to the migration.
*
* @param string $name
* @param string $path
* @return string
*/
protected function getPath($name, $path)
{
$path = getcwd() . $this->devPath() . '/src/database/migrations';
if (!$this->files->isDirectory($path)) {
$this->files->makeDirectory($path);
}
return $path . '/' . $this->getDatePrefix() . '_' . $name . '.php';
}
}
23 changes: 23 additions & 0 deletions app/Commands/Foundation/Migrations/TableGuesser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Commands\Foundation\Migrations;

class TableGuesser
{
/**
* Attempt to guess the table name and "creation" status of the given migration.
*
* @param string $migration
* @return array
*/
public static function guess($migration)
{
if (preg_match('/^create_(\w+)_table$/', $migration, $matches)) {
return [$matches[1], $create = true];
}

if (preg_match('/_(to|from|in)_(\w+)_table$/', $migration, $matches)) {
return [$matches[2], $create = false];
}
}
}
28 changes: 28 additions & 0 deletions app/Commands/Foundation/Migrations/stubs/blank.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
31 changes: 31 additions & 0 deletions app/Commands/Foundation/Migrations/stubs/create.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('DummyTable', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('DummyTable');
}
}
32 changes: 32 additions & 0 deletions app/Commands/Foundation/Migrations/stubs/update.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('DummyTable', function (Blueprint $table) {
//
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('DummyTable', function (Blueprint $table) {
//
});
}
}
Loading

0 comments on commit 51c8e50

Please sign in to comment.