Skip to content

Commit

Permalink
Merge pull request #2 from Harmonic/analysis-8jZexJ
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
Craig Harman authored Jul 12, 2019
2 parents e010689 + 0b5f91b commit a3c042a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
38 changes: 21 additions & 17 deletions src/Commands/MakeInertiaTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class MakeInertiaTable extends Command {
class MakeInertiaTable extends Command
{
use \Illuminate\Console\DetectsApplicationNamespace;

/**
Expand All @@ -31,16 +32,18 @@ class MakeInertiaTable extends Command {
*
* @return void
*/
public function __construct() {
public function __construct()
{
parent::__construct();
}

private function inertiaTableJSExists() {
private function inertiaTableJSExists()
{
$process = new Process('npm list --depth=0 | grep inertia-table');
$process->run();

// executes after the command finishes
if (!$process->isSuccessful()) {
if (! $process->isSuccessful()) {
if ($process->getOutput() == '') {
return false;
}
Expand All @@ -61,46 +64,47 @@ private function inertiaTableJSExists() {
*
* @return mixed
*/
public function handle() {
public function handle()
{
$model = ucfirst($this->argument('model'));

if (!file_exists(app_path($model) . '.php') && !file_exists(app_path('Models\\' . $model) . '.php')) {
if (! file_exists(app_path($model).'.php') && ! file_exists(app_path('Models\\'.$model).'.php')) {
// Create Model
$str = file_get_contents(__DIR__ . '/../Stubs/Model.php');
$str = file_get_contents(__DIR__.'/../Stubs/Model.php');
$str = str_replace('@namespace', substr($this->getAppNamespace(), 0, -1), $str);
$str = str_replace('@modelName', $model, $str);
file_put_contents(app_path($model . '.php'), $str);
file_put_contents(app_path($model.'.php'), $str);
}

// Create Controller
$pluralName = Str::plural($model);
$controllerName = $pluralName . 'Controller';
$str = file_get_contents(__DIR__ . '/../Stubs/Controller.php');
$controllerName = $pluralName.'Controller';
$str = file_get_contents(__DIR__.'/../Stubs/Controller.php');
$str = str_replace('@namespace', substr($this->getAppNamespace(), 0, -1), $str);
$str = str_replace('@controllerName', $controllerName, $str);
$str = str_replace('@modelName', $model, $str);
file_put_contents(app_path('Http/Controllers/' . $controllerName . '.php'), $str);
file_put_contents(app_path('Http/Controllers/'.$controllerName.'.php'), $str);

// Add routes
$this->warn('You need to manually add route: Route::inertia(\'' . Str::plural(strtolower($model)) . '\');');
$this->warn('You need to manually add route: Route::inertia(\''.Str::plural(strtolower($model)).'\');');

if ($this->inertiaTableJSExists()) {
$pluralNameLowercase = \strtolower($pluralName);
$modelLowercase = \strtolower($model);

// Get column names from table
$class = 'App\\' . $model;
$class = 'App\\'.$model;
$columns = Schema::getColumnListing(with(new $class)->getTable());

// Create an InertiaTable.vue component and then create Index.vue using it
$tableElementPath = resource_path('js/Pages/' . $pluralName);
$tableElementPath = resource_path('js/Pages/'.$pluralName);
File::makeDirectory($tableElementPath, 0755, true);
$str = file_get_contents(__DIR__ . '/../Stubs/Index.vue');
$str = file_get_contents(__DIR__.'/../Stubs/Index.vue');
$str = str_replace('@pluralNameLowercase', $pluralNameLowercase, $str);
$str = str_replace('@plural', $pluralName, $str);
$str = str_replace('@columns', '"' . implode('","', $columns) . '"', $str);
$str = str_replace('@columns', '"'.implode('","', $columns).'"', $str);
$str = str_replace('@modelLowercase', $modelLowercase, $str);
file_put_contents($tableElementPath . '/Index.vue', $str);
file_put_contents($tableElementPath.'/Index.vue', $str);
}

$this->info('Model, Controller and Vue Components successfully created.');
Expand Down
8 changes: 5 additions & 3 deletions src/InertiaTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Request;

class InertiaTable {
class InertiaTable
{
/**
* Generates inertia view data for model.
*
* @param InertiaModel $model The model to use to retrieve data
* @param array $columns An array of column names to send to front end (null for all columns)
* @return void
*/
public function index(InertiaModel $model, array $columns = null) {
public function index(InertiaModel $model, array $columns = null)
{
$modelName = class_basename($model);

if ($columns == null) { // default to all columns
Expand All @@ -25,7 +27,7 @@ public function index(InertiaModel $model, array $columns = null) {

$modelPlural = Str::plural($modelName);

return Inertia::render($modelPlural . '/Index', [
return Inertia::render($modelPlural.'/Index', [
'filters' => Request::all('search', 'trashed'),
'order' => Request::all('orderColumn', 'orderDirection'),
strtolower($modelPlural) => $model
Expand Down

0 comments on commit a3c042a

Please sign in to comment.