diff --git a/src/Commands/BaseCommand.php b/src/Commands/BaseCommand.php index 1454ca4..3d48928 100644 --- a/src/Commands/BaseCommand.php +++ b/src/Commands/BaseCommand.php @@ -202,6 +202,24 @@ protected function isValidFilename(string $filename): bool return (bool) preg_match('#^[A-Za-z0-9_\\\\/\.-]+$#', $filename); } + /** + * Create folders if necessary for file paths. + * + * @param array $pathParts + * @param string $basePath + * @return void + */ + protected function createFoldersIfNecessary(array $pathParts, string $basePath): void + { + $folder = $basePath; + for ($i = 0; $i < count($pathParts) - 1; $i++) { + $folder .= DIRECTORY_SEPARATOR . $pathParts[$i]; + if (!is_dir($folder)) { + mkdir($folder, 0755, true); + } + } + } + /** * Check if a repository file exists. * diff --git a/src/Commands/ClassMakeCommand.php b/src/Commands/ClassMakeCommand.php index 1574c19..f1d0cbf 100644 --- a/src/Commands/ClassMakeCommand.php +++ b/src/Commands/ClassMakeCommand.php @@ -1,7 +1,5 @@ isCorrectFilename($this->argument('filename'))) { $extension = $this->getExtension(); $path = base_path(str_replace('.', '/', $this->argument('filename')).'.'.$extension); - if ($this->replaceExistingFile($path, 'There is already a file with this name do you want to replace it ? [y/n]')) { + if ($this->shouldReplaceFile($path, 'There is already a file with this name do you want to replace it ? [y/n]')) { $filename = explode('.', $this->argument('filename')); - - $this->createFoldersIfNecessary($filename); - + $this->createFoldersIfNecessary($filename, base_path()); file_put_contents($path, ''); $this->info('File created successfully'); } - } else - $this->error('The filename is not correct.'); + return self::SUCCESS; + } + $this->error('The filename is not correct.'); + return self::FAILURE; } /** @@ -70,23 +59,6 @@ protected function getExtension() return 'php'; } - /** - * Create a set of folders if necessary. - * - * @param $filename - * @return void - */ - protected function createFoldersIfNecessary($filename) - { - $folder = base_path(''); - for ($i = 0; $i < count($filename) - 1; $i++) { - if (!is_dir($folder . '/' . $filename[$i])) { - mkdir($folder . '/' . $filename[$i]); - } - $folder .= '/' . $filename[$i]; - } - } - /** * Check if the filename is correct. * @@ -97,25 +69,4 @@ protected function isCorrectFilename($name) { return (bool) preg_match('#^[a-zA-Z][a-zA-Z0-9._\-]+$#', $name); } - - /** - * Check if the filename exists and if it could be replaced. - * - * @param $filename - * @param $question - * @return bool - */ - protected function replaceExistingFile($filename, $question) - { - $replaceExistingFile = true; - if (file_exists($filename)) { - do { - $input = $this->ask($question); - } while (strtolower($input) != 'y' && strtolower($input) != 'n'); - - if (strtolower($input) == 'n') - $replaceExistingFile = false; - } - return $replaceExistingFile; - } } diff --git a/src/Commands/Lang.php b/src/Commands/Lang.php index 0d42d65..2a73c41 100644 --- a/src/Commands/Lang.php +++ b/src/Commands/Lang.php @@ -1,7 +1,5 @@ writeFile($path, $stub); } + protected function generateTailwindConfig(): void + { + $path = base_path('tailwind.config.js'); + if (file_exists($path)) return; + $stub = $this->getStubContent('tailwind.config'); + if (!$this->shouldReplaceFile($path, 'Tailwind config exists. Replace it? [y/n]')) return; + $this->writeFile($path, $stub); + } + protected function generateResourceFormFields(array $fields): string { if (empty($fields)) { return " // Form::text('name', 'Name'),"; } diff --git a/src/Commands/Service.php b/src/Commands/Service.php index ce9c579..39add9c 100644 --- a/src/Commands/Service.php +++ b/src/Commands/Service.php @@ -54,9 +54,9 @@ protected function replaceClassName($name, $stub) /** * Rewrite actually the content in the file. * - * @param null $module - * @param $filename - * @param $content + * @param string $filename + * @param string $content + * @param string $module */ protected function putInFile($filename, $content, $module = null) { diff --git a/src/Commands/View.php b/src/Commands/View.php index 8560f52..922c7e3 100644 --- a/src/Commands/View.php +++ b/src/Commands/View.php @@ -2,9 +2,7 @@ namespace Kernel243\Artisan\Commands; -use Illuminate\Console\Command; - -class View extends Command +class View extends BaseCommand { /** * The name and signature of the console command. diff --git a/src/Commands/stubs/crud.resource.stub b/src/Commands/stubs/crud.resource.stub new file mode 100644 index 0000000..438d945 --- /dev/null +++ b/src/Commands/stubs/crud.resource.stub @@ -0,0 +1,56 @@ +schema([ + // Define your form fields here +DummyFormFields + ]); + } + + public function table($table) + { + return $table->columns([ + // Define your table columns here +DummyTableColumns + ]); + } + + public function actions() + { + return [ + // Define your custom actions here + // Example: 'export', 'bulk_delete', etc. + ]; + } + + public function rules() + { + return [ + // Define validation rules here +DummyValidationRules + ]; + } + + public function getFillable() + { + return app($this->model)->getFillable(); + } +} + diff --git a/src/Commands/stubs/crud.service.stub b/src/Commands/stubs/crud.service.stub new file mode 100644 index 0000000..039ace2 --- /dev/null +++ b/src/Commands/stubs/crud.service.stub @@ -0,0 +1,47 @@ +repository = $repository; + } + + public function getAll() + { + return $this->repository->getAll(); + } + + public function getPaginated(int $perPage = 15) + { + return $this->repository->getPaginate($perPage); + } + + public function getById(int $id) + { + return $this->repository->getById($id); + } + + public function create(array $data) + { + return $this->repository->store($data); + } + + public function update(int $id, array $data) + { + return $this->repository->update($data, $id); + } + + public function delete(int $id): void + { + $this->repository->delete($id); + } +} + diff --git a/src/Commands/stubs/crud.views.default.create.stub b/src/Commands/stubs/crud.views.default.create.stub new file mode 100644 index 0000000..ba24c65 --- /dev/null +++ b/src/Commands/stubs/crud.views.default.create.stub @@ -0,0 +1,39 @@ +@extends('layouts.app') + +@section('title', 'Create {{ $resourceName }}') + +@section('content') +
+
+

Create {{ $resourceName }}

+ + @if($errors->any()) +
+ Please fix the following errors: +
    + @foreach($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + +
+ @csrf + + @include('crud._form') + +
+ + + Cancel + +
+
+
+
+@endsection + + diff --git a/src/Commands/stubs/crud.views.default.edit.stub b/src/Commands/stubs/crud.views.default.edit.stub new file mode 100644 index 0000000..86dedbb --- /dev/null +++ b/src/Commands/stubs/crud.views.default.edit.stub @@ -0,0 +1,40 @@ +@extends('layouts.app') + +@section('title', 'Edit {{ $resourceName }}') + +@section('content') +
+
+

Edit {{ $resourceName }}

+ + @if($errors->any()) +
+ Please fix the following errors: +
    + @foreach($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + +
+ @csrf + @method('PUT') + + @include('crud._form', ['item' => $item]) + +
+ + + Cancel + +
+
+
+
+@endsection + + diff --git a/src/Commands/stubs/crud.views.default.show.stub b/src/Commands/stubs/crud.views.default.show.stub new file mode 100644 index 0000000..f847b47 --- /dev/null +++ b/src/Commands/stubs/crud.views.default.show.stub @@ -0,0 +1,57 @@ +@extends('layouts.app') + +@section('title', 'View {{ $resourceName }}') + +@section('content') +
+
+
+

View {{ $resourceName }}

+ +
+ +
+
+ @foreach($detailFields as $field) +
+
{{ $field['label'] }}:
+
+ @if($field['type'] === 'boolean') + {{ $item->{$field['name']} ? + ' Yes' : + ' No' + }} + @else + {{ $item->{$field['name']} }} + @endif +
+
+ @endforeach +
+
+ +
+
+ @csrf + @method('DELETE') + +
+
+
+
+@endsection + + diff --git a/src/Commands/stubs/crud.views.edit.stub b/src/Commands/stubs/crud.views.edit.stub new file mode 100644 index 0000000..fb2db40 --- /dev/null +++ b/src/Commands/stubs/crud.views.edit.stub @@ -0,0 +1,42 @@ +@extends('layouts.app') + +@section('title', 'Edit DummyResource') + +@section('content') +
+
+

Edit DummyResource

+ + @if($errors->any()) +
+
    + @foreach($errors->all() as $error) +
  • {{ $error }}
  • + @endforeach +
+
+ @endif + +
+ @csrf + @method('PUT') + + DummyFormFields + +
+ + + Cancel + +
+
+
+
+@endsection + + diff --git a/src/Commands/stubs/crud.views.index.stub b/src/Commands/stubs/crud.views.index.stub new file mode 100644 index 0000000..d64ff87 --- /dev/null +++ b/src/Commands/stubs/crud.views.index.stub @@ -0,0 +1,73 @@ +@extends('layouts.app') + +@section('title', 'DummyResource') + +@section('content') +
+
+

DummyResource

+ + Create New + +
+ + @if(session('success')) +
+ {{ session('success') }} +
+ @endif + +
+ + + + + + + + + @forelse($items as $item) + + + + + @empty + + + + @endforelse + +
+ ID + + Actions +
+ {{ $item->id }} + + View + Edit +
+ @csrf + @method('DELETE') + +
+
+ No items found. +
+
+ + @if(method_exists($items, 'links')) +
+ {{ $items->links() }} +
+ @endif +
+@endsection + + diff --git a/src/Commands/stubs/crud.views.show.stub b/src/Commands/stubs/crud.views.show.stub new file mode 100644 index 0000000..9991794 --- /dev/null +++ b/src/Commands/stubs/crud.views.show.stub @@ -0,0 +1,44 @@ +@extends('layouts.app') + +@section('title', 'View DummyResource') + +@section('content') +
+
+
+

View DummyResource

+ +
+ +
+
+ DummyDetailFields +
+
+ +
+
+ @csrf + @method('DELETE') + +
+
+
+
+@endsection + + diff --git a/src/Commands/stubs/form.builder.stub b/src/Commands/stubs/form.builder.stub new file mode 100644 index 0000000..6a00fa5 --- /dev/null +++ b/src/Commands/stubs/form.builder.stub @@ -0,0 +1,75 @@ +fields = $fields; + } + + public function schema(array $fields) + { + $this->fields = $fields; + return $this; + } + + public function getFields() + { + return $this->fields; + } + + public static function text($name, $label, $options = []) + { + return array_merge([ + 'type' => 'text', + 'name' => $name, + 'label' => $label, + ], $options); + } + + public static function textarea($name, $label, $options = []) + { + return array_merge([ + 'type' => 'textarea', + 'name' => $name, + 'label' => $label, + ], $options); + } + + public static function email($name, $label, $options = []) + { + return array_merge([ + 'type' => 'email', + 'name' => $name, + 'label' => $label, + ], $options); + } + + public static function select($name, $label, $options = []) + { + return array_merge([ + 'type' => 'select', + 'name' => $name, + 'label' => $label, + ], $options); + } + + public static function checkbox($name, $label, $options = []) + { + return array_merge([ + 'type' => 'checkbox', + 'name' => $name, + 'label' => $label, + ], $options); + } +} + diff --git a/src/Commands/stubs/resource.base.stub b/src/Commands/stubs/resource.base.stub new file mode 100644 index 0000000..c5fae8a --- /dev/null +++ b/src/Commands/stubs/resource.base.stub @@ -0,0 +1,50 @@ +model); + } + + public function getName() + { + return $this->name; + } + + public function getRoutePrefix() + { + return $this->routePrefix; + } + + public function getView() + { + return $this->view; + } + + abstract public function form($form); + abstract public function table($table); + abstract public function actions(); + abstract public function rules(); + + public function getFillable() + { + return []; + } +} + diff --git a/src/Commands/stubs/table.builder.stub b/src/Commands/stubs/table.builder.stub new file mode 100644 index 0000000..b28c696 --- /dev/null +++ b/src/Commands/stubs/table.builder.stub @@ -0,0 +1,85 @@ +columns = $columns; + } + + public function columns(array $columns) + { + $this->columns = $columns; + return $this; + } + + public function getColumns() + { + return $this->columns; + } + + public static function text($name, $label, $options = []) + { + return array_merge([ + 'type' => 'text', + 'name' => $name, + 'label' => $label, + 'orderable' => true, + 'searchable' => true, + ], $options); + } + + public static function number($name, $label, $options = []) + { + return array_merge([ + 'type' => 'number', + 'name' => $name, + 'label' => $label, + 'orderable' => true, + 'searchable' => false, + ], $options); + } + + public static function boolean($name, $label, $options = []) + { + return array_merge([ + 'type' => 'boolean', + 'name' => $name, + 'label' => $label, + 'orderable' => true, + 'searchable' => false, + ], $options); + } + + public static function datetime($name, $label, $options = []) + { + return array_merge([ + 'type' => 'datetime', + 'name' => $name, + 'label' => $label, + 'orderable' => true, + 'searchable' => false, + ], $options); + } + + public static function actions($options = []) + { + return array_merge([ + 'type' => 'actions', + 'name' => 'actions', + 'label' => 'Actions', + 'orderable' => false, + 'searchable' => false, + ], $options); + } +} + diff --git a/src/Commands/stubs/tailwind.config.stub b/src/Commands/stubs/tailwind.config.stub new file mode 100644 index 0000000..7f8b034 --- /dev/null +++ b/src/Commands/stubs/tailwind.config.stub @@ -0,0 +1,25 @@ +/** @type {import('tailwindcss').Config} */ +module.exports = { + content: [ + "./resources/**/*.blade.php", + "./resources/**/*.js", + "./resources/**/*.vue", + "./app/**/*.php", + ], + theme: { + extend: { + colors: { + // Add your custom colors here + }, + fontFamily: { + // Add your custom fonts here + }, + }, + }, + plugins: [ + // Add Tailwind plugins here + // require('@tailwindcss/forms'), + // require('@tailwindcss/typography'), + ], +} +