123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- namespace Dcat\Admin\Console;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Illuminate\Console\Command;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\File;
- /**
- * Class IdeHelperCommand.
- *
- * @authr jqh <841324345@qq.com>
- */
- class IdeHelperCommand extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'admin:ide-helper {--c|controller= : Controller class. } ';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Create the ide-helper file';
- /**
- * @var array
- */
- protected $patterns = [
- 'grid' => '/(?:grid)->([\w0-9_]+)(?:\(|;|->|\s)/i',
- 'show' => '/show->([\w0-9_]+)(?:\(|;|->|\s)/i',
- 'grid-column' => '/@method[\s]+\$this[\s]+([\w0-9_]+)/i',
- 'form-field' => '/@method[\s]+[\\\\\w0-9_]+[\s]+([\w0-9_]+)/i',
- 'grid-filter' => '/@method[\s]+[\\\\\w0-9_]+[\s]+([\w0-9_]+)/i',
- ];
- /**
- * @var array
- */
- protected $templates = [
- 'grid' => [
- 'method' => '* @method Grid\Column|Collection %s(string $label = null)',
- 'property' => '* @property Grid\Column|Collection %s',
- ],
- 'show' => [
- 'method' => '* @method Show\Field|Collection %s(string $label = null)',
- 'property' => '* @property Show\Field|Collection %s',
- ],
- 'form' => '* @method %s %s(...$params)',
- 'grid-column' => '* @method $this %s(...$params)',
- 'grid-filter' => '* @method %s %s(...$params)',
- 'show-column' => '* @method $this %s(...$params)',
- ];
- protected $path = 'dcat_admin_ide_helper.php';
- /**
- * Execute the console command.
- */
- public function handle()
- {
- if (! config('app.debug')) {
- $this->error('Permission deny!');
- return;
- }
- if (is_file($bootstrap = admin_path('bootstrap.php'))) {
- require $bootstrap;
- }
- $builders = $this->getBuilderMethods();
- // Get all fields.
- $fields = $this->getFieldsFromControllerFiles($builders)
- ->merge($this->getFieldsFromDatabase($builders))
- ->unique();
- $this->write($fields);
- $path = basename($this->path);
- $this->info("The helper file [$path] created successfully.");
- }
- /**
- * @param array $reject
- *
- * @return Collection
- */
- protected function getFieldsFromDatabase(array $reject = [])
- {
- $databases = Arr::where(config('database.connections', []), function ($value) {
- $supports = ['mysql'];
- return in_array(strtolower(Arr::get($value, 'driver')), $supports);
- });
- $exceptTables = [
- 'migrations',
- 'phinxlog',
- ];
- $data = collect();
- try {
- foreach ($databases as $connectName => $value) {
- $sql = sprintf('SELECT * FROM information_schema.columns WHERE table_schema = "%s"', $value['database']);
- $each = collect(DB::connection($connectName)->select($sql))
- ->map(function ($v) use ($exceptTables, &$reject) {
- $v = (array) $v;
- if (in_array($v['TABLE_NAME'], $exceptTables) || in_array($v['COLUMN_NAME'], $reject)) {
- return;
- }
- return $v['COLUMN_NAME'];
- })
- ->filter();
- $data = $data->merge($each);
- }
- } catch (\Throwable $e) {
- }
- return $data->unique();
- }
- /**
- * @param array $reject
- *
- * @return Collection
- */
- protected function getFieldsFromControllerFiles(array $reject = [])
- {
- $option = $this->option('controller');
- return $this->getAllControllers()
- ->merge(explode(',', $option))
- ->map(function ($controller) use (&$reject) {
- if (! $controller || ! $content = $this->getClassContent($controller)) {
- return [];
- }
- preg_match_all($this->patterns['grid'], $content, $grid);
- preg_match_all($this->patterns['show'], $content, $show);
- $grid = $grid[1];
- $show = $show[1];
- return collect(array_merge($grid, $show))->reject(function ($name) use (&$reject) {
- return in_array($name, $reject);
- });
- })
- ->flatten()
- ->unique()
- ->filter();
- }
- /**
- * @param Collection $fields
- */
- protected function write(Collection $fields)
- {
- $content = str_replace(
- [
- '{grid}',
- '{show}',
- '{form}',
- '{grid-column}',
- '{grid-filter}',
- '{show-column}',
- ],
- [
- $this->generate('grid', $fields),
- $this->generate('show', $fields),
- $this->generateFormFields(),
- $this->generateGridColumns(),
- $this->generateGridFilters(),
- $this->generateShowFields(),
- ],
- File::get($this->getStub())
- );
- File::put(base_path($this->path), $content);
- }
- /**
- * @param string $type
- * @param Collection $fields
- *
- * @return string
- */
- public function generate(string $type, Collection $fields)
- {
- $methods = $properties = [];
- $space = str_repeat(' ', 5);
- $fields->each(function ($name) use ($type, &$methods, &$properties, $space) {
- $properties[] = $space.sprintf($this->templates[$type]['property'], $name);
- $methods[] = $space.sprintf($this->templates[$type]['method'], $name);
- });
- return trim(implode("\r\n", array_merge($properties, [$space.'*'], $methods)));
- }
- /**
- * @return string
- */
- protected function generateGridFilters()
- {
- $content = $this->getClassContent(Grid\Filter::class);
- preg_match_all($this->patterns['grid-filter'], $content, $fields);
- $reject = $fields[1];
- $fields = collect(Grid\Filter::extensions())->reject(function ($value, $key) use (&$reject) {
- return in_array($key, $reject);
- });
- $space = str_repeat(' ', 5);
- return trim(
- $fields
- ->map(function ($value, $key) use (&$space) {
- return $space.sprintf($this->templates['grid-filter'], '\\'.$value, $key);
- })
- ->implode("\r\n")
- );
- }
- /**
- * @return string
- */
- protected function generateShowFields()
- {
- $extensions = collect(Show\Field::extensions());
- $space = str_repeat(' ', 5);
- return trim(
- $extensions
- ->map(function ($value, $key) use (&$space) {
- return $space.sprintf($this->templates['show-column'], $key);
- })
- ->implode("\r\n")
- );
- }
- /**
- * @return string
- */
- protected function generateFormFields()
- {
- $content = $this->getClassContent(Form::class);
- preg_match_all($this->patterns['form-field'], $content, $fields);
- $reject = $fields[1];
- $fields = collect(Form::extensions())->reject(function ($value, $key) use (&$reject) {
- return in_array($key, $reject);
- });
- $space = str_repeat(' ', 5);
- return trim(
- $fields
- ->map(function ($value, $key) use (&$space) {
- return $space.sprintf($this->templates['form'], '\\'.$value, $key);
- })
- ->implode("\r\n")
- );
- }
- /**
- * @return string
- */
- protected function generateGridColumns()
- {
- $content = $this->getClassContent(Grid\Column::class);
- preg_match_all($this->patterns['grid-column'], $content, $column);
- $reject = $column[1];
- $columns = collect(array_keys(Grid\Column::extensions()))->reject(function ($displayer) use (&$reject) {
- return in_array($displayer, $reject);
- });
- $space = str_repeat(' ', 5);
- return trim(
- $columns
- ->map(function ($value) use (&$space) {
- return $space.sprintf($this->templates['grid-column'], $value);
- })
- ->implode("\r\n")
- );
- }
- /**
- * @return array
- */
- protected function getBuilderMethods()
- {
- $grid = new \ReflectionClass(Grid::class);
- $grids = collect($grid->getMethods())
- ->pluck('name')
- ->merge(collect($grid->getProperties())->pluck('name'))
- ->all();
- $show = new \ReflectionClass(Show::class);
- return collect($show->getMethods())
- ->pluck('name')
- ->merge(collect($show->getProperties())->pluck('name'))
- ->merge($grids)
- ->unique()
- ->all();
- }
- /**
- * @return string
- */
- protected function getStub()
- {
- return __DIR__.'/stubs/ide-helper.stub';
- }
- /**
- * Get all registered controllers.
- *
- * @return Collection
- */
- public function getAllControllers()
- {
- return collect(\Route::getRoutes())->map(function ($route) {
- try {
- $action = $route->getActionName();
- if ($action == 'Closure') {
- return;
- }
- return explode('@', $action)[0];
- } catch (\Exception $e) {
- }
- })->filter();
- }
- public function getClassContent($class)
- {
- if ($file = $this->getFileNameByClass($class)) {
- return File::get($file);
- }
- }
- /**
- * @param string $class
- *
- * @return string
- */
- public function getFileNameByClass($class)
- {
- if (! class_exists($class)) {
- return;
- }
- return (new \ReflectionClass($class))->getFileName();
- }
- }
|