-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e68f060
commit 51c8e50
Showing
16 changed files
with
608 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
153
app/Commands/Foundation/Migrations/MigrateMakeCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
{ | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
// | ||
}); | ||
} | ||
} |
Oops, something went wrong.