Skip to content

Commit 57104ae

Browse files
committed
add support for filament plugins
1 parent 505df8c commit 57104ae

File tree

105 files changed

+3028
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+3028
-26
lines changed

.DS_Store

8 KB
Binary file not shown.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
}
3333
],
3434
"require": {
35-
"php": "^8.0.2",
36-
"tomatophp/console-helpers": "^1.0"
35+
"php": "^8.1|^8.2",
36+
"tomatophp/console-helpers": "^1.1"
3737
}
3838
}

publish/.DS_Store

6 KB
Binary file not shown.

publish/resources/lang/ar.json

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

publish/resources/lang/en.json

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

src/Console/FilamentPluginGenerator.php

Lines changed: 454 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
namespace TomatoPHP\LaravelPackageGenerator\Console;
4+
5+
use Carbon\Carbon;
6+
use Illuminate\Console\Command;
7+
use Illuminate\Support\Facades\File;
8+
use Illuminate\Support\Str;
9+
use TomatoPHP\ConsoleHelpers\Traits\HandleFiles;
10+
use TomatoPHP\ConsoleHelpers\Traits\HandleStub;
11+
use TomatoPHP\ConsoleHelpers\Traits\RunCommand;
12+
use TomatoPHP\LaravelPackageGenerator\Services\Resource\FilamentResourceGenerator;
13+
use function Laravel\Prompts\error;
14+
use function Laravel\Prompts\select;
15+
use function Laravel\Prompts\suggest;
16+
use function Laravel\Prompts\text;
17+
18+
class FilamentResourceClassesGenerator extends Command
19+
{
20+
use RunCommand;
21+
use HandleFiles;
22+
use HandleStub;
23+
24+
private string $stubPath;
25+
26+
/**
27+
* The name and signature of the console command.
28+
*
29+
* @var string
30+
*/
31+
protected $signature = 'package:filament-resource {vendor} {package} {resource} {namespace}';
32+
33+
/**
34+
* The console command description.
35+
*
36+
* @var string
37+
*/
38+
protected $description = 'Generate Filament Resource Classes';
39+
40+
public function __construct()
41+
{
42+
parent::__construct();
43+
$this->publish = __DIR__ .'/../../tomato/';
44+
$this->stubPath = config('laravel-package-generator.stub-path');
45+
}
46+
47+
48+
/**
49+
* Execute the console command.
50+
*
51+
* @return mixed
52+
*/
53+
public function handle()
54+
{
55+
$getPackageVendors = [];
56+
if(File::exists(base_path('packages'))){
57+
$getPackageVendors = File::directories(base_path('packages'));
58+
}
59+
60+
$packageVendor = $this->argument('vendor') ?? suggest(
61+
label:"Enter your package vendor name?",
62+
options: collect($getPackageVendors)->map(function($vendor){
63+
return Str::afterLast($vendor, '/');
64+
})->toArray(),
65+
required: true,
66+
hint: "Like: tomatophp",
67+
);
68+
69+
$getPackages = [];
70+
if(File::exists(base_path('packages/' . $packageVendor))){
71+
$getPackages = File::directories(base_path('packages/' . $packageVendor));
72+
}
73+
74+
75+
$packageName = $this->argument('package') ?? suggest(
76+
label: "Enter your package name",
77+
options: collect($getPackages)->map(function($vendor){
78+
return Str::afterLast($vendor, '/');
79+
})->toArray(),
80+
required: true,
81+
hint: "Like: filament-users",
82+
);
83+
84+
$packagePathExists = File::exists(base_path("packages/$packageVendor/$packageName/src/Filament/Resources"));
85+
if(!$packagePathExists){
86+
error("Package path does not exist. Please create the package first.");
87+
return;
88+
}
89+
90+
$scanPackageResources = File::directories(base_path("packages/$packageVendor/$packageName/src/Filament/Resources"));
91+
92+
$resourceName = $this->argument('resource') ?? suggest(
93+
label: "Select the resource name",
94+
options: collect($scanPackageResources)->map(function($vendor){
95+
return Str::afterLast($vendor, '/');
96+
})->toArray(),
97+
required: true,
98+
);
99+
100+
$namespace = $this->argument('namespace') ?? text(
101+
label: "Enter your vendor package namespace",
102+
required: true,
103+
hint: "Like: TomatoPHP\\FilamentUsers",
104+
);
105+
106+
new FilamentResourceGenerator(
107+
$packageVendor,
108+
$packageName,
109+
$resourceName,
110+
$namespace,
111+
);
112+
113+
}
114+
}

src/Console/FilamentTestGenerator.php

Lines changed: 259 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
<?php
2+
3+
namespace TomatoPHP\LaravelPackageGenerator\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\File;
7+
use Illuminate\Support\Str;
8+
use TomatoPHP\ConsoleHelpers\Traits\HandleFiles;
9+
use TomatoPHP\ConsoleHelpers\Traits\HandleStub;
10+
use TomatoPHP\ConsoleHelpers\Traits\RunCommand;
11+
12+
class FilamentTestGenerator extends Command
13+
{
14+
use RunCommand;
15+
use HandleFiles;
16+
use HandleStub;
17+
18+
private string $stubPath;
19+
20+
/**
21+
* The name and signature of the console command.
22+
*
23+
* @var string
24+
*/
25+
protected $name = 'package:tests';
26+
27+
/**
28+
* The console command description.
29+
*
30+
* @var string
31+
*/
32+
protected $description = 'use this command to generate package boilerplate';
33+
34+
public function __construct()
35+
{
36+
parent::__construct();
37+
$this->publish = __DIR__ .'/../../publish/';
38+
$this->stubPath = config('laravel-package-generator.stub-path');
39+
}
40+
41+
42+
/**
43+
* Execute the console command.
44+
*
45+
* @return mixed
46+
*/
47+
public function handle()
48+
{
49+
$packageName = null;
50+
while(empty($packageName)){
51+
$packageName = $this->ask("Enter your package name");
52+
if(is_numeric($packageName[0])){
53+
$this->error("Package name can't start with a number");
54+
$packageName = null;
55+
}
56+
}
57+
$packageString = Str::of($packageName);
58+
59+
$packageVendor = null;
60+
while(empty($packageVendor)){
61+
$packageVendor = $this->ask("Enter your package vendor name");
62+
if(is_numeric($packageVendor[0])){
63+
$this->error("Vendor name can't start with a number");
64+
$packageVendor = null;
65+
}
66+
}
67+
68+
$packageVendorString = Str::of($packageVendor);
69+
70+
//Package Meta
71+
$packageDescription = $this->ask("Enter your package description", "your package description will go here");
72+
$packageAuthor = $this->ask("Enter your package author", "Queen Tech");
73+
$packageAuthorEmail = $this->ask("Enter your package author email", "git@queentechsoltions.net");
74+
75+
//Check Options
76+
$packageConfig = $this->ask("Has Config file? (yes/no)", "yes")?: "yes";
77+
$packageRoute = $this->ask("Has Routes? (yes/no)", "yes")?: "yes";
78+
$packageView = $this->ask("Has Views? (yes/no)", "yes")?: "yes";
79+
$packageMigration = $this->ask("Has Migrations? (yes/no)", "yes")?: "yes";
80+
81+
$this->info('Generating package boilerplate...');
82+
83+
//create package directory
84+
if(!File::exists(base_path(config('laravel-package-generator.packages-folder')))){
85+
File::makeDirectory(base_path(config('laravel-package-generator.packages-folder')));
86+
}
87+
88+
$packageVendorPath = $packageVendorString
89+
->replace(' ', '-')
90+
->replace('_', '-')
91+
->lower()
92+
->toString();
93+
94+
//Create vendor directory
95+
if(!File::exists(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendorPath)){
96+
File::makeDirectory(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendorPath);
97+
}
98+
99+
$packageNamePath = $packageString
100+
->replace(' ', '-')
101+
->replace('_', '-')
102+
->lower()
103+
->toString();
104+
105+
//Create package directory
106+
if(!File::exists(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendor . "/" . $packageNamePath)){
107+
File::makeDirectory(base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendor . "/" . $packageNamePath);
108+
}
109+
110+
$packagePath = base_path(config('laravel-package-generator.packages-folder')) . "/" .$packageVendorPath . "/" . $packageNamePath;
111+
112+
//Build a package inside vendor directory
113+
$packageConfig !== 'yes' ? null : $this->handelFile('config', $packagePath. "/config", 'folder');
114+
$packageMigration !== 'yes' ? null : $this->handelFile('database', $packagePath. "/database", 'folder');
115+
$packageView !== 'yes' ? null : $this->handelFile('resources', $packagePath. "/resources", 'folder');
116+
$packageRoute !== 'yes' ? null : $this->handelFile('routes', $packagePath. "/routes", 'folder');
117+
$this->handelFile('src', $packagePath. "/src", 'folder');
118+
$this->handelFile('tests', $packagePath. "/tests", 'folder');
119+
$this->handelFile('LICENSE.md', $packagePath. "/LICENSE.md");
120+
$this->handelFile('CHANGELOG.md', $packagePath. "/CHANGELOG.md");
121+
$this->handelFile('SECURITY.md', $packagePath. "/SECURITY.md");
122+
123+
124+
$vendorNamespace = Str::of($packageVendorPath)->camel()->ucfirst()->toString();
125+
$packageNamespace = Str::of($packageNamePath)->camel()->ucfirst()->toString();
126+
$packageProviderName = Str::of($packageNamePath)->camel()->ucfirst()->toString() . "ServiceProvider";
127+
$fullNameSpace = $vendorNamespace . "\\". $packageNamespace;
128+
129+
//Build Stubs Files
130+
$commandClassName = $packageString->studly()->append('Install')->toString();
131+
$this->generateStubs(
132+
$this->stubPath . 'command.stub',
133+
$packagePath . '/src/Console/'. $commandClassName . ".php",
134+
[
135+
"namespace" => $fullNameSpace,
136+
"name" => $commandClassName,
137+
"command" => $packageNamePath,
138+
"packageName" => Str::of($packageNamePath)->camel()->toString(),
139+
"provider" => $packageProviderName
140+
],
141+
[
142+
$packagePath . '/src/Console/'
143+
]
144+
);
145+
146+
//Build Provider Register Methods
147+
$register = collect([]);
148+
$register->push('//Register generate command');
149+
$register->push(' $this->commands([');
150+
$register->push(' '."\\".$fullNameSpace."\\Console\\".$commandClassName.'::class,');
151+
$register->push(' ]);');
152+
$register->push(" ");
153+
if($packageConfig === 'yes'){
154+
$register->push(' //Register Config file');
155+
$register->push(' $this->mergeConfigFrom(__DIR__.\'/../config/'.$packageNamePath.'.php\', \''.$packageNamePath.'\');');
156+
$register->push(" ");
157+
$register->push(' //Publish Config');
158+
$register->push(' $this->publishes([');
159+
$register->push(' __DIR__.\'/../config/'.$packageNamePath.'.php\' => config_path(\''.$packageNamePath.'.php\'),');
160+
$register->push(' ], \''.$packageNamePath.'-config\');');
161+
$register->push(" ");
162+
163+
//Generate Config file
164+
$this->generateStubs(
165+
$this->stubPath . 'config.stub',
166+
$packagePath . '/config/'.$packageNamePath.'.php',
167+
[
168+
],
169+
);
170+
}
171+
if($packageMigration === 'yes'){
172+
$register->push(' //Register Migrations');
173+
$register->push(' $this->loadMigrationsFrom(__DIR__.\'/../database/migrations\');');
174+
$register->push(" ");
175+
$register->push(' //Publish Migrations');
176+
$register->push(' $this->publishes([');
177+
$register->push(' __DIR__.\'/../database/migrations\' => database_path(\'migrations\'),');
178+
$register->push(' ], \''.$packageNamePath.'-migrations\');');
179+
}
180+
if($packageView === 'yes'){
181+
$register->push(' //Register views');
182+
$register->push(' $this->loadViewsFrom(__DIR__.\'/../resources/views\', \''.$packageNamePath.'\');');
183+
$register->push(" ");
184+
$register->push(' //Publish Views');
185+
$register->push(' $this->publishes([');
186+
$register->push(' __DIR__.\'/../resources/views\' => resource_path(\'views/vendor/'.$packageNamePath.'\'),');
187+
$register->push(' ], \''.$packageNamePath.'-views\');');
188+
$register->push(" ");
189+
$register->push(' //Register Langs');
190+
$register->push(' $this->loadTranslationsFrom(__DIR__.\'/../resources/lang\', \''.$packageNamePath.'\');');
191+
$register->push(" ");
192+
$register->push(' //Publish Lang');
193+
$register->push(' $this->publishes([');
194+
$register->push(' __DIR__.\'/../resources/lang\' => base_path(\'lang/vendor/'.$packageNamePath.'\'),');
195+
$register->push(' ], \''.$packageNamePath.'-lang\');');
196+
$register->push(" ");
197+
}
198+
if($packageRoute === 'yes') {
199+
$register->push(' //Register Routes');
200+
$register->push(' $this->loadRoutesFrom(__DIR__.\'/../routes/web.php\');');
201+
$register->push(" ");
202+
}
203+
204+
//Generate Provider File
205+
$this->generateStubs(
206+
$this->stubPath . 'provider.stub',
207+
$packagePath . '/src/'. $packageProviderName . ".php",
208+
[
209+
"namespace" => $fullNameSpace,
210+
"name" => $packageProviderName,
211+
"register" => $register->implode("\n")
212+
],
213+
[
214+
$packagePath . '/src'
215+
]
216+
);
217+
218+
//Generate Composer.json file
219+
$this->generateStubs(
220+
$this->stubPath . 'composer.stub',
221+
$packagePath . '/composer.json',
222+
[
223+
"vendor" => $packageVendorPath,
224+
"package" => $packageNamePath,
225+
"description" => $packageDescription,
226+
"namespace" => $vendorNamespace ."\\\\". $packageNamespace,
227+
"provider" => $packageProviderName,
228+
"author" => $packageAuthor,
229+
"email" => $packageAuthorEmail,
230+
],
231+
[
232+
$packagePath
233+
]
234+
);
235+
236+
237+
238+
//Generate Readme.md file
239+
$this->generateStubs(
240+
$this->stubPath . 'readme.stub',
241+
$packagePath . '/README.md',
242+
[
243+
"name" => Str::of($packageNamePath)->replace('-', ' ')->ucfirst()->toString(),
244+
"vendor" => $packageVendorPath,
245+
"command" => $packageNamePath,
246+
"package" => $packageNamePath,
247+
"description" => $packageDescription,
248+
"author" => $packageAuthor,
249+
"email" => $packageAuthorEmail,
250+
"vendorName" => $vendorNamespace,
251+
],
252+
[
253+
$packagePath
254+
]
255+
);
256+
257+
$this->info('Package boilerplate generated successfully');
258+
}
259+
}

0 commit comments

Comments
 (0)