Skip to content

Commit cac2110

Browse files
committed
Initail comit
1 parent 8bf1323 commit cac2110

17 files changed

+521
-76
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
"illuminate/contracts": "^11.0"
2222
},
2323
"require-dev": {
24+
"larastan/larastan": "^v2.9.2",
2425
"laravel/pint": "^v1.15.1",
26+
"mockery/mockery": "^1.6",
2527
"nunomaduro/collision": "^v8.1.1",
26-
"larastan/larastan": "^v2.9.2",
2728
"orchestra/testbench": "^9.0.0",
2829
"pestphp/pest": "^v2.34",
2930
"pestphp/pest-plugin-arch": "^v2.7.0",
31+
"pestphp/pest-plugin-faker": "^2.0",
3032
"pestphp/pest-plugin-laravel": "^v2.3.0",
3133
"phpstan/extension-installer": "^1.3",
3234
"phpstan/phpstan-deprecation-rules": "^1.1",
@@ -76,7 +78,6 @@
7678
"Designbycode\\EloquentDatatable\\EloquentDatatableServiceProvider"
7779
],
7880
"aliases": {
79-
"EloquentDatatable": "Designbycode\\EloquentDatatable\\Facades\\EloquentDatatable"
8081
}
8182
}
8283
},

config/eloquent-datatable.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
<?php
22

3-
// config for Designbycode/EloquentDatatable
3+
/*
4+
* Configuration for DesignByCode Eloquent Datatable
5+
*/
6+
47
return [
58

69
];

database/factories/ModelFactory.php

Lines changed: 0 additions & 19 deletions
This file was deleted.

database/migrations/create_eloquent_datatable_table.php.stub

Lines changed: 0 additions & 19 deletions
This file was deleted.

phpstan.neon.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ parameters:
66
paths:
77
- src
88
- config
9-
- database
109
tmpDir: build/phpstan
1110
checkOctaneCompatibility: true
1211
checkModelProperties: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div>
2+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eaque necessitatibus nostrum quis? Accusamus adipisci aliquam architecto at dolorem doloremque eius explicabo facilis id impedit, in ipsam ipsum iusto laboriosam non numquam, odit perferendis praesentium quam quo quos reprehenderit rerum sit temporibus tenetur? A accusamus adipisci cumque earum enim error excepturi, facere illo ipsa ipsam ipsum maiores molestiae, molestias odio reprehenderit repudiandae unde. Accusantium animi asperiores assumenda aut, cum cupiditate delectus doloribus earum facilis in ipsum iure maxime, minus nesciunt nostrum officia provident quaerat qui quo reprehenderit repudiandae rerum sint ullam vitae voluptate. Animi ea inventore nam nemo obcaecati velit. A ad, aliquam atque commodi, eos ex excepturi harum illo illum, ipsum maxime nisi praesentium quo reprehenderit saepe similique veritatis. Dolore enim et harum ipsum itaque labore laborum magni modi nisi odit, pariatur, quis quisquam ratione rerum saepe, sit temporibus totam ut veritatis voluptatum. Adipisci dolores dolorum, ex illum laborum numquam, placeat quod reiciendis reprehenderit sunt vel, veritatis. Accusantium alias, aspernatur aut blanditiis, cum delectus distinctio dolorem dolores doloribus exercitationem facere ipsam ipsum iure maiores minima modi nisi officia, perferendis repellat suscipit? A alias assumenda beatae consequuntur corporis cum dolore dolorem eaque enim esse exercitationem minima officia perferendis quas ratione, voluptas?
3+
</div>

src/Commands/EloquentDatatableCommand.php

Lines changed: 115 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,127 @@
33
namespace Designbycode\EloquentDatatable\Commands;
44

55
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Str;
68

79
class EloquentDatatableCommand extends Command
810
{
9-
public $signature = 'eloquent-datatable';
11+
protected $signature = 'datatable:create {name : The name of the controller} {--model= : The model associated with the controller} {--middleware= : The middleware to be applied (comma separated)}';
1012

11-
public $description = 'My command';
13+
protected $description = 'Create a DataTable controller';
1214

13-
public function handle(): int
15+
public function __construct()
1416
{
15-
$this->comment('All done');
17+
parent::__construct();
18+
}
19+
20+
public function handle(): void
21+
{
22+
$controllerName = $this->argument('name');
23+
$modelName = $this->option('model');
24+
$middleware = $this->option('middleware');
25+
26+
// Read the stub file
27+
$stub = File::get(__DIR__.'/../../stubs/datatable.stub');
28+
29+
if (! $modelName) {
30+
$modelName = $this->getSingularName($controllerName);
31+
if (! $this->confirm("Do you want to use '$modelName' as the model name?", true)) {
32+
$modelName = $this->ask('Enter the model name:');
33+
}
34+
}
35+
36+
if ($this->confirm("Do you want to use '$modelName' model fillable fields for updating columns?", true)) {
37+
// Get the path to the model file
38+
$modelPath = $this->getModelPath($modelName);
39+
40+
// Read the model file
41+
$modelContent = File::get($modelPath);
42+
43+
// Extract the $fillable fields from the model content
44+
$fillable = $this->extractFillableFields($modelContent);
45+
46+
$stub = str_replace('// :fillable', implode(',', $fillable), $stub);
47+
} else {
48+
$stub = str_replace('// :fillable', "''", $stub);
49+
}
50+
51+
// Replace placeholders
52+
$stub = str_replace('StubController', $controllerName, $stub);
53+
$stub = str_replace('StubModel', $modelName, $stub);
54+
55+
// Define namespace, path, class name, and file name
56+
$namespace = 'App\\Http\\Controllers\\Datatables';
57+
$path = app_path('Http\\Controllers\\Datatables');
58+
$className = $controllerName;
59+
$fileName = $className.'.php';
60+
61+
// Ensure the directory exists
62+
File::ensureDirectoryExists($path);
63+
64+
if ($middleware) {
65+
$middlewareArray = explode(',', $middleware);
66+
$middlewareCode = "\$this->middleware(['".implode("','", $middlewareArray)."']);";
67+
$stub = str_replace('// :middleware', $middlewareCode, $stub);
68+
}
69+
70+
// Check if model exists
71+
if (! $this->modelExists($modelName)) {
72+
$this->error("Model '$modelName' does not exist in App\Models directory.");
73+
74+
return;
75+
}
76+
77+
// Check if the file already exists
78+
if (File::exists($path.'\/'.$fileName)) {
79+
$this->error('Controller file already exists: '.$path.'\/'.$fileName);
80+
81+
return;
82+
}
83+
84+
// Write the content to the file
85+
File::put($path.'/'.$fileName, $stub);
86+
87+
// Inform user about successful creation
88+
$this->info('DataTable controller created successfully: '.$path.'/'.$fileName);
89+
}
90+
91+
private function modelExists($modelName): bool
92+
{
93+
$modelFilePath = app_path('Models/'.$modelName.'.php');
94+
95+
return File::exists($modelFilePath);
96+
}
97+
98+
// Function to get the path to the model file
99+
private function getModelPath($modelName): string
100+
{
101+
return app_path('Models/'.$modelName.'.php');
102+
}
103+
104+
// Function to extract $fillable fields from model content
105+
private function extractFillableFields($modelContent): array
106+
{
107+
$matches = [];
108+
preg_match('/\$fillable\s*=\s*\[(.*?)\];/s', $modelContent, $matches);
109+
if (isset($matches[1])) {
110+
return array_map('trim', explode(',', $matches[1]));
111+
}
112+
113+
return [];
114+
}
115+
116+
private function getSingularName($controller): string
117+
{
118+
return Str::singular($this->getPascalCaseFirstSegment($controller));
119+
}
120+
121+
private function getPascalCaseFirstSegment(string $inputString): string
122+
{
123+
// Split the input string by uppercase letters to get the first segment
124+
preg_match('/^[A-Z][a-z]*/', $inputString, $matches);
16125

17-
return self::SUCCESS;
126+
// Return the matched segment
127+
return $matches[0];
18128
}
19129
}

0 commit comments

Comments
 (0)