Skip to content
This repository has been archived by the owner on May 17, 2020. It is now read-only.

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Chan committed Feb 12, 2016
2 parents fe84dad + 25ed287 commit a0e4164
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
95 changes: 92 additions & 3 deletions src/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GeneratorCommand extends Command
/**
* @var string
*/
protected $signature = 'riseno:localizable:generate {table}';
protected $signature = 'riseno:localizable:generate {table} {-m}';

/**
* @var string
Expand All @@ -46,9 +46,9 @@ class GeneratorCommand extends Command
*/
public function __construct(Filesystem $file)
{
$this->file = $file;

parent::__construct();

$this->file = $file;
}

/**
Expand All @@ -67,6 +67,87 @@ public function handle()
$this->file->put($migrationPath, $content);

$this->output->success('Migration file : ' . $migrationPath);

if ($this->option('m')) {

$modelPath = $this->getModelPath();

$content = $this->file->get($this->getModelStubPath());

$this->replaceModelClassName($content)
->replaceModelClassMethod($content)
->replaceModelParentClass($content)
->replaceModelField($content);

$this->file->put($modelPath, $content);

$this->output->success('Model file : ' . $modelPath);
}

}

/**
* @return string
*/
private function getModelPath()
{
return base_path().'/app/'.$this->getModelClassName().'.php';
}

/**
* @return string
*/
private function getModelClassName()
{
return studly_case($this->argument('table') . '_' . $this->suffix);
}

/**
* @param $stub
*
* @return $this
*/
private function replaceModelClassName(&$stub)
{
$stub = str_replace('__class__', $this->getModelClassName(), $stub);

return $this;
}

/**
* @param $stub
*
* @return $this
*/
private function replaceModelClassMethod(&$stub)
{
$stub = str_replace('__method__', strtolower($this->argument('table')), $stub);

return $this;
}

/**
* @param $stub
*
* @return $this
*/
private function replaceModelField(&$stub)
{
$stub = str_replace('__field__', strtolower($this->argument('table')), $stub);

return $this;
}

/**
* @param $stub
*
* @return $this
*/
private function replaceModelParentClass(&$stub)
{
$stub = str_replace('__parent_class__', ucfirst($this->argument('table')), $stub);

return $this;
}

/**
Expand Down Expand Up @@ -129,6 +210,14 @@ private function getStubPath()
return __DIR__ . '/../migrations/localizeTableStub.stub';
}

/**
* @return string
*/
private function getModelStubPath()
{
return __DIR__ . '/../migrations/localizeModelStub.stub';
}

/**
* @return string
*/
Expand Down
26 changes: 26 additions & 0 deletions src/migrations/localizeModelStub.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
* Class __class__.
*/
class __class__ extends Model
{
/**
* @var array
*/
protected $fillable = [
'locale',
];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function __method__()
{
return $this->belongsTo(__parent_class__::class, '__field___id', 'id');
}
}
2 changes: 1 addition & 1 deletion src/migrations/localizeTableStub.stub
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class __table_class__ extends Migration {
{
Schema::create('__table__', function(Blueprint $table)
{
$table->increments('id');
$table->bigIncrements('id');
$table->unsignedInteger('__parent_class___id');
$table->string('locale');
$table->boolean('default')->default(false);
Expand Down

0 comments on commit a0e4164

Please sign in to comment.