Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.6.0 #46

Merged
merged 9 commits into from
Apr 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wip
  • Loading branch information
Okipa committed Apr 26, 2020
commit c7f2e89c485bbe12634b0cd375c0af52fcb072c4
63 changes: 54 additions & 9 deletions src/Console/Commands/MakeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace Okipa\LaravelTable\Console\Commands;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;

class MakeTable extends GeneratorCommand
{
Expand All @@ -28,17 +31,17 @@ class MakeTable extends GeneratorCommand
protected function getStub(): string
{
if ($this->option('model')) {
$stub = '/stubs/export.model.stub';
$stub = '/stubs/table.model.stub';
}
$stub = $stub ?? '/stubs/export.plain.stub';
$stub = $stub ?? '/stubs/table.stub';

return __DIR__ . $stub;
}

/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @param string $rootNamespace
*
* @return string
*/
Expand All @@ -51,32 +54,74 @@ protected function getDefaultNamespace($rootNamespace): string
* Build the class with the given name.
* Remove the base controller import if we are already in base namespace.
*
* @param string $name
* @param string $name
*
* @return string
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
protected function buildClass($name)
protected function buildClass($name): string
{
$replace = [];
if ($this->option('model')) {
$replace = $this->buildModelReplacements($replace);
}

return str_replace(
array_keys($replace), array_values($replace), parent::buildClass($name)
array_keys($replace),
array_values($replace),
parent::buildClass($name)
);
}

/**
* Build the model replacement values.
*
* @param array $replace
*
* @return array
*/
protected function buildModelReplacements(array $replace): array
{
$modelClass = $this->parseModel($this->option('model'));

return array_merge($replace, [
'DummyFullModelClass' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'$dummyModel' => '$' . Str::camel(class_basename($modelClass)),
]);
}

/**
* Get the fully-qualified model class name.
*
* @param string $model
*
* @return string
*/
protected function parseModel(string $model): string
{
$result = preg_match('([^A-Za-z0-9_/\\\\])', $model);
if ($result) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}
$model = trim(str_replace('/', '\\', $model), '\\');
$rootNamespace = $this->laravel->getNamespace();
if (! Str::startsWith($model, $rootNamespace)) {
$model = $rootNamespace . $model;
}

return $model;
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
protected function getOptions(): array
{
return [
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate an export for the given model.'],
['query', '', InputOption::VALUE_NONE, 'Generate an export for a query.'],
['model', 'm', InputOption::VALUE_OPTIONAL, 'Generate a table configuration for the given model.'],
];
}
}
9 changes: 0 additions & 9 deletions src/Console/Commands/stubs/table.model.stub

This file was deleted.

16 changes: 0 additions & 16 deletions src/Console/Commands/stubs/table.stub

This file was deleted.

23 changes: 23 additions & 0 deletions src/Console/Commands/table.model.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace DummyNamespace;

use Okipa\LaravelTable\Table;
use DummyFullModelClass;

class DummyClass extends Table
{
/**
* @return void
* @throws \ErrorException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function configure(): void
{
$this->model(DummyModelClass::class)->routes([
'index' => ['name' => '...'],
]);
$this->column('...')->sortable()->searchable();
}

}
19 changes: 19 additions & 0 deletions src/Console/Commands/table.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace DummyNamespace;

use Okipa\LaravelTable\Table;

class DummyClass extends Table
{
/**
* @return void
* @throws \ErrorException
* @throws \Illuminate\Contracts\Container\BindingResolutionException
*/
public function configure(): void
{
//
}

}
6 changes: 4 additions & 2 deletions src/LaravelTableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

use Illuminate\Support\ServiceProvider;
use Okipa\LaravelHtmlHelper\HtmlHelperServiceProvider;
use Okipa\LaravelTable\Console\Commands\MakeTable;

class LaravelTableServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
$this->loadViewsFrom(__DIR__ . '/../views', 'laravel-table');
$this->loadTranslationsFrom(__DIR__ . '/../lang', 'laravel-table');
Expand All @@ -25,8 +26,9 @@ public function boot()
$this->app->register(HtmlHelperServiceProvider::class);
}

public function register()
public function register(): void
{
$this->mergeConfigFrom(__DIR__ . '/../config/laravel-table.php', 'laravel-table');
$this->commands([MakeTable::class]);
}
}
3 changes: 3 additions & 0 deletions src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public function __construct()
$this->columns = new Collection();
$this->disableRows = new Collection();
$this->results = new Collection();
if (method_exists($this, 'configure')) {
$this->configure();
}
}

/**
Expand Down