Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 18 additions & 0 deletions src/Commands/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 0 additions & 2 deletions src/Commands/ClassMakeCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Kernel243\Artisan\Commands;

use Illuminate\Support\Str;
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Kernel243\Artisan\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

Expand Down
2 changes: 0 additions & 2 deletions src/Commands/CrudMakeCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Kernel243\Artisan\Commands;

use Illuminate\Support\Str;
Expand Down
67 changes: 9 additions & 58 deletions src/Commands/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

namespace Kernel243\Artisan\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Str;

class File extends Command
class File extends BaseCommand
{
/**
* The name and signature of the console command.
Expand All @@ -21,37 +20,27 @@ class File extends Command
*/
protected $description = 'Create a new file';

/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}

/**
* Execute the console command.
*
* @return void
* @return int
*/
public function handle()
public function handle(): int
{
if ($this->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;
}

/**
Expand All @@ -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.
*
Expand All @@ -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;
}
}
2 changes: 0 additions & 2 deletions src/Commands/Lang.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Kernel243\Artisan\Commands;

class Lang extends BaseCommand
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Kernel243\Artisan\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ protected function replaceNamespace($namespace, $stub)
/**
* Rewrite actually the content in the file.
*
* @param $filename
* @param null $content
* @param $module
* @param string $filename
* @param string $content
* @param string $module
*/
protected function putInFile($filename, $content, $module = null)
{
Expand Down
11 changes: 9 additions & 2 deletions src/Commands/ResourceMakeCommand.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php

declare(strict_types=1);

namespace Kernel243\Artisan\Commands;

use Illuminate\Support\Str;
Expand Down Expand Up @@ -248,6 +246,15 @@ protected function generateLayout(): void
$this->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'),"; }
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Commands/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
56 changes: 56 additions & 0 deletions src/Commands/stubs/crud.resource.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Resources;

use Illuminate\Database\Eloquent\Model;

/**
* DummyResourceName Resource Class
*
* This class defines the structure and behavior of DummyResourceName CRUD operations
*/
class DummyResourceClass extends Resource
{
protected $model = App\Models\DummyModel::class;
protected $name = 'DummyResourcePlural';
protected $routePrefix = 'DummyResourceKebab';
protected $view = null;

public function form($form)
{
return $form->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();
}
}

47 changes: 47 additions & 0 deletions src/Commands/stubs/crud.service.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Services;

use App\Repositories\DummyRepository;
use Illuminate\Database\Eloquent\Model;

class DummyClass
{
protected $repository;

public function __construct(DummyRepository $repository)
{
$this->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);
}
}

39 changes: 39 additions & 0 deletions src/Commands/stubs/crud.views.default.create.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@extends('layouts.app')

@section('title', 'Create {{ $resourceName }}')

@section('content')
<div class="container mx-auto px-4 py-8">
<div class="max-w-2xl mx-auto">
<h1 class="text-3xl font-bold text-gray-800 mb-6">Create {{ $resourceName }}</h1>

@if($errors->any())
<div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
<strong>Please fix the following errors:</strong>
<ul class="list-disc list-inside mt-2">
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif

<form action="{{ route('{{ $routePrefix }}.store') }}" method="POST" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
@csrf

@include('crud._form')

<div class="flex items-center justify-between">
<button type="submit" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Create
</button>
<a href="{{ route('{{ $routePrefix }}.index') }}" class="text-gray-500 hover:text-gray-700">
Cancel
</a>
</div>
</form>
</div>
</div>
@endsection


Loading