Skip to content

Instantly share code, notes, and snippets.

@drakakisgeo
Created May 6, 2017 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drakakisgeo/81b9208fd9b64076b68a9cd07c73ecb5 to your computer and use it in GitHub Desktop.
Save drakakisgeo/81b9208fd9b64076b68a9cd07c73ecb5 to your computer and use it in GitHub Desktop.
<?php
namespace App\Console\Commands;
use DB;
use Illuminate\Console\Command;
class ModelFactoryHelper extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'generate:factory {model}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate a factory for a certain table';
private $defaultFieldValues;
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$model = '\\App\\' . $this->argument('model');
$table = (new $model)->getTable();
$dbSchema = collect(DB::select(DB::raw('SHOW COLUMNS FROM `' . $table . '`')));
$this->generateDefaults($dbSchema);
$cols = $this->getColumns($dbSchema);
$fields = "";
foreach ($cols as $col) {
$value = $this->fetchCommonValue($col) == "" ? "''" : $this->fetchCommonValue($col);
$fields .= "'{$col}' => " . $value . ',' . PHP_EOL;
}
$textBlock = '$factory->define(' . $model . '::class, function (Faker\Generator $faker) {' . PHP_EOL . ' return [' . PHP_EOL . $fields . PHP_EOL . '];' . PHP_EOL . '});';
$this->info($textBlock);
}
private function fetchCommonValue($fieldName)
{
$targetValue = "";
$fieldName = strtolower($fieldName);
$key = array_key_exists($fieldName, $this->defaultFieldValues);
return $key ? $this->defaultFieldValues[$fieldName] : $targetValue;
}
private function generateDefaults($dbSchema)
{
$commonFieldValues = [
'firstname' => '$faker->firstname',
'lastname' => '$faker->lastname',
'email' => '$faker->email',
'company' => '$faker->company',
'address' => '$faker->streetAddress',
'lang_id' => 1,
'created_at' => '\Carbon\Carbon::now()',
'updated_at' => '\Carbon\Carbon::now()',
];
$fieldValuesFromDb = [];
foreach ($dbSchema as $schema) {
if ($schema->Field !== "id" && !array_key_exists($schema->Field, $commonFieldValues)) {
$fieldValuesFromDb[$schema->Field] = $this->determineDefaultValue($schema);
}
}
$this->defaultFieldValues = array_merge($commonFieldValues, $fieldValuesFromDb);
}
private function determineDefaultValue($schema)
{
if (!is_null($schema->Default)) {
return $schema->Default;
}
if ($schema->Null == "NO") {
return "'-- Type: {$schema->Type} --'";
}
return str_contains($schema->Type,'int') ? 'NULL' : "'-- Type: {$schema->Type} --'";
}
/**
* @param $dbSchema
*
* @return mixed
*/
private function getColumns($dbSchema)
{
$cols = $dbSchema->pluck('Field')->toArray();
$idColumnKey = array_search('id', $cols);
if ($idColumnKey !== false) {
unset($cols[$idColumnKey]);
}
return $cols;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment