This commit is contained in:
Saeed Vaziry 2025-02-20 18:00:13 +01:00
parent 8c7c3d2192
commit a1cf09e35d
20 changed files with 550 additions and 9 deletions

View File

@ -0,0 +1,32 @@
<?php
namespace App\Cli\Commands;
use App\Models\User;
use Illuminate\Console\Command;
use function Laravel\Prompts\error;
abstract class AbstractCommand extends Command
{
private User|null $user = null;
public function user()
{
if ($this->user) {
return $this->user->refresh();
}
/** @var User $user */
$user = User::query()->first();
if (!$user) {
error('The application is not setup');
exit(1);
}
$this->user = $user;
return $user;
}
}

View File

@ -0,0 +1,18 @@
<?php
namespace App\Cli\Commands;
use Illuminate\Console\Command;
class InfoCommand extends Command
{
protected $signature = 'info';
protected $description = 'Show the application information';
public function handle(): void
{
$this->info('Version: '.config('app.version'));
$this->info('Timezone: '.config('app.timezone'));
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace App\Cli\Commands\Projects;
use App\Cli\Commands\AbstractCommand;
use App\Models\Project;
use function Laravel\Prompts\table;
class ProjectsListCommand extends AbstractCommand
{
protected $signature = 'projects:list';
protected $description = 'Show projects list';
public function handle(): void
{
$projects = Project::all();
table(
headers: ['ID', 'Name', 'Selected'],
rows: $projects->map(fn (Project $project) => [
$project->id,
$project->name,
$project->id === $this->user()->current_project_id ? 'Yes' : 'No',
])->toArray(),
);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Cli\Commands\Projects;
use App\Cli\Commands\AbstractCommand;
use App\Models\Project;
use function Laravel\Prompts\error;
use function Laravel\Prompts\info;
class ProjectsSelectCommand extends AbstractCommand
{
protected $signature = 'projects:select {project}';
protected $description = 'Select a project';
public function handle(): void
{
$project = Project::query()->find($this->argument('project'));
if (! $project) {
error('The project does not exist');
return;
}
$this->user()->update([
'current_project_id' => $project->id,
]);
info(__('The project [:project] has been selected' , [
'project' => $project->name,
]));
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Cli\Commands\ServerProviders;
use App\Cli\Commands\AbstractCommand;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
class ServerProvidersCreateCommand extends AbstractCommand
{
protected $signature = 'server-providers:create';
protected $description = 'Create a new server provider';
public function handle(): void
{
$provider = select(
label: 'What is the server provider?',
options: collect(config('core.server_providers'))
->filter(fn($provider) => $provider != \App\Enums\ServerProvider::CUSTOM)
->mapWithKeys(fn($provider) => [$provider => $provider]),
);
$profile = text(
label: 'What should we call this provider profile?',
required: true,
);
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace App\Cli\Commands\ServerProviders;
use App\Cli\Commands\AbstractCommand;
use App\Models\Project;
use App\Models\Server;
use App\Models\ServerProvider;
use function Laravel\Prompts\table;
class ServerProvidersListCommand extends AbstractCommand
{
protected $signature = 'server-providers:list';
protected $description = 'Show server providers list';
public function handle(): void
{
$providers = $this->user()->serverProviders;
table(
headers: ['ID', 'Provider', 'Name', 'Created At'],
rows: $providers->map(fn (ServerProvider $provider) => [
$provider->id,
$provider->provider,
$provider->profile,
$provider->created_at_by_timezone,
])->toArray(),
);
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace App\Cli\Commands\Servers;
use App\Cli\Commands\AbstractCommand;
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;
class ServersCreateCommand extends AbstractCommand
{
protected $signature = 'servers:create';
protected $description = 'Create a new server';
public function handle(): void
{
$name = text(
label: 'What is the server name?',
required: true,
);
$os = select(
label: 'What is the server OS?',
options: collect(config('core.operating_systems'))
->mapWithKeys(fn($value) => [$value => $value]),
);
}
}

View File

@ -0,0 +1,34 @@
<?php
namespace App\Cli\Commands\Servers;
use App\Cli\Commands\AbstractCommand;
use App\Models\Project;
use App\Models\Server;
use function Laravel\Prompts\table;
class ServersListCommand extends AbstractCommand
{
protected $signature = 'servers:list';
protected $description = 'Show servers list';
public function handle(): void
{
$servers = $this->user()->currentProject->servers;
table(
headers: ['ID', 'Name', 'IP', 'Provider', 'OS', 'Status', 'Created At'],
rows: $servers->map(fn (Server $server) => [
$server->id,
$server->name,
$server->ip,
$server->provider,
$server->os,
$server->status,
$server->created_at_by_timezone,
])->toArray(),
);
}
}

View File

@ -0,0 +1,81 @@
<?php
namespace App\Cli\Commands;
use App\Models\Project;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
use function Laravel\Prompts\text;
use function Laravel\Prompts\info;
class SetupCommand extends Command
{
protected $signature = 'setup';
protected $description = 'Setup the application';
public function handle(): void
{
$this->prepareStorage();
$this->migrate();
$this->makeUser();
$this->makeProject();
info('The application has been setup');
}
private function prepareStorage(): void
{
File::ensureDirectoryExists(storage_path());
}
private function migrate(): void
{
$this->call('migrate', ['--force' => true]);
}
private function makeUser(): void
{
$user = User::query()->first();
if ($user) {
return;
}
$name = text(
label: 'What is your name?',
required: true,
);
$email = text(
label: 'What is your email?',
required: true,
);
User::query()->create([
'name' => $name,
'email' => $email,
'password' => bcrypt(str()->random(16)),
]);
}
private function makeProject(): void
{
$project = Project::query()->first();
if ($project) {
return;
}
$project = Project::query()->create([
'name' => 'default',
]);
$user = User::query()->first();
$user->update([
'current_project_id' => $project->id,
]);
}
}

43
app/Cli/Kernel.php Normal file
View File

@ -0,0 +1,43 @@
<?php
namespace App\Cli;
use Illuminate\Console\Application as Artisan;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Console\Migrations\InstallCommand;
use Illuminate\Database\Console\Migrations\MigrateCommand;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
protected $commands = [
'command.migrate',
'command.migrate.install'
];
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__ . '/Commands');
$this->app->singleton('command.migrate', function ($app) {
return new MigrateCommand($app['migrator'], $app[Dispatcher::class]);
});
$this->app->singleton('command.migrate.install', function ($app) {
return new InstallCommand($app['migration.repository']);
});
}
protected function shouldDiscoverCommands(): false
{
return false;
}
protected function getArtisan(): ?Artisan
{
return $this->artisan = (new Artisan($this->app, $this->events, $this->app->version()))
->resolveCommands($this->commands);
}
}

View File

@ -18,6 +18,9 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register(): void public function register(): void
{ {
if ($this->app->runningInConsole()) {
return;
}
Fortify::ignoreRoutes(); Fortify::ignoreRoutes();
} }
@ -36,6 +39,8 @@ public function boot(): void
return new FTP; return new FTP;
}); });
if (! $this->app->runningInConsole()) {
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class); Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
} }
} }
}

View File

@ -23,6 +23,9 @@ class RouteServiceProvider extends ServiceProvider
*/ */
public function boot(): void public function boot(): void
{ {
if ($this->app->runningInConsole()) {
return;
}
$this->configureRateLimiting(); $this->configureRateLimiting();
} }

View File

@ -38,11 +38,18 @@ class WebServiceProvider extends ServiceProvider
*/ */
public function register(): void public function register(): void
{ {
if ($this->app->runningInConsole()) {
return;
}
Filament::registerPanel($this->panel(Panel::make())); Filament::registerPanel($this->panel(Panel::make()));
} }
public function boot(): void public function boot(): void
{ {
if ($this->app->runningInConsole()) {
return;
}
FilamentView::registerRenderHook( FilamentView::registerRenderHook(
PanelsRenderHook::SIDEBAR_NAV_START, PanelsRenderHook::SIDEBAR_NAV_START,
fn () => Livewire::mount(SelectProject::class) fn () => Livewire::mount(SelectProject::class)

2
bootstrap/cli-cache/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore

69
bootstrap/cli.php Normal file
View File

@ -0,0 +1,69 @@
<?php
use Illuminate\Foundation\Application;
putenv('APP_SERVICES_CACHE='.__DIR__.'/cli-cache/services.php');
putenv('APP_PACKAGES_CACHE='.__DIR__.'/cli-cache/packages.php');
putenv('APP_CONFIG_CACHE='.__DIR__.'/cli-cache/config.php');
putenv('APP_ROUTES_CACHE='.__DIR__.'/cli-cache/routes.php');
putenv('APP_EVENTS_CACHE='.__DIR__.'/cli-cache/events.php');
putenv('LOG_CHANNEL=syslog');
putenv('QUEUE_CONNECTION=sync');
putenv('CACHE_DRIVER=null');
putenv('DB_DATABASE=database.sqlite');
/*
|--------------------------------------------------------------------------
| Create The Application
|--------------------------------------------------------------------------
|
| The first thing we will do is create a new Laravel application instance
| which serves as the "glue" for all the components of Laravel, and is
| the IoC container for the system binding all of the various parts.
|
*/
$app = new Application(
$_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);
$app->useStoragePath(getenv('HOME') . '/.vito/storage');
/*
|--------------------------------------------------------------------------
| Bind Important Interfaces
|--------------------------------------------------------------------------
|
| Next, we need to bind some important interfaces into the container so
| we will be able to resolve them when needed. The kernels serve the
| incoming requests to this application from both the web and CLI.
|
*/
// $app->singleton(
// Illuminate\Contracts\Http\Kernel::class,
// App\Http\Kernel::class
// );
$app->singleton(
Illuminate\Contracts\Console\Kernel::class,
App\Cli\Kernel::class
);
$app->singleton(
Illuminate\Contracts\Debug\ExceptionHandler::class,
App\Exceptions\Handler::class
);
/*
|--------------------------------------------------------------------------
| Return The Application
|--------------------------------------------------------------------------
|
| This script returns the application instance. The instance is given to
| the calling script so we can separate the building of the instances
| from the actual running of the application and sending responses.
|
*/
return $app;

20
box.json Normal file
View File

@ -0,0 +1,20 @@
{
"directories": [
"app",
"bootstrap",
"config",
"database",
"public",
"resources",
"routes",
"storage",
"vendor"
],
"files": [
"artisan"
],
"main": "cli",
"output": "vito-cli.phar",
"chmod": "0755",
"stub": true
}

53
cli Normal file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env php
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader
| for our application. We just need to utilize it! We'll require it
| into the script here so that we do not have to worry about the
| loading of any our classes "manually". Feels great to relax.
|
*/
$autoloader = require file_exists(__DIR__.'/vendor/autoload.php') ? __DIR__.'/vendor/autoload.php' : __DIR__.'/../../autoload.php';
$app = require_once __DIR__.'/bootstrap/cli.php';
/*
|--------------------------------------------------------------------------
| Run The Artisan Application
|--------------------------------------------------------------------------
|
| When we run the console application, the current CLI command will be
| executed in this console and the response sent back to a terminal
| or another output device for the developers. Here goes nothing!
|
*/
$kernel = $app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArgvInput,
new Symfony\Component\Console\Output\ConsoleOutput
);
/*
|--------------------------------------------------------------------------
| Shutdown The Application
|--------------------------------------------------------------------------
|
| Once Artisan has finished running, we will fire off the shutdown events
| so that any final work may be done by the application before we shut
| down the process. This is the last thing to happen to the request.
|
*/
$kernel->terminate($input, $status);
exit($status);

View File

@ -15,6 +15,7 @@
"filament/filament": "^3.2", "filament/filament": "^3.2",
"laravel/fortify": "^1.17", "laravel/fortify": "^1.17",
"laravel/framework": "^11.0", "laravel/framework": "^11.0",
"laravel/prompts": "^0.3.5",
"laravel/sanctum": "^4.0", "laravel/sanctum": "^4.0",
"laravel/tinker": "^2.8", "laravel/tinker": "^2.8",
"mobiledetect/mobiledetectlib": "^4.8", "mobiledetect/mobiledetectlib": "^4.8",

16
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "be3e63b7efd71f649cbffb0d469ba7c1", "content-hash": "e211da7974e07c3b74ad59ce245a9446",
"packages": [ "packages": [
{ {
"name": "anourvalar/eloquent-serialize", "name": "anourvalar/eloquent-serialize",
@ -2559,16 +2559,16 @@
}, },
{ {
"name": "laravel/prompts", "name": "laravel/prompts",
"version": "v0.3.3", "version": "v0.3.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel/prompts.git", "url": "https://github.com/laravel/prompts.git",
"reference": "749395fcd5f8f7530fe1f00dfa84eb22c83d94ea" "reference": "57b8f7efe40333cdb925700891c7d7465325d3b1"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel/prompts/zipball/749395fcd5f8f7530fe1f00dfa84eb22c83d94ea", "url": "https://api.github.com/repos/laravel/prompts/zipball/57b8f7efe40333cdb925700891c7d7465325d3b1",
"reference": "749395fcd5f8f7530fe1f00dfa84eb22c83d94ea", "reference": "57b8f7efe40333cdb925700891c7d7465325d3b1",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -2582,7 +2582,7 @@
"laravel/framework": ">=10.17.0 <10.25.0" "laravel/framework": ">=10.17.0 <10.25.0"
}, },
"require-dev": { "require-dev": {
"illuminate/collections": "^10.0|^11.0", "illuminate/collections": "^10.0|^11.0|^12.0",
"mockery/mockery": "^1.5", "mockery/mockery": "^1.5",
"pestphp/pest": "^2.3|^3.4", "pestphp/pest": "^2.3|^3.4",
"phpstan/phpstan": "^1.11", "phpstan/phpstan": "^1.11",
@ -2612,9 +2612,9 @@
"description": "Add beautiful and user-friendly forms to your command-line applications.", "description": "Add beautiful and user-friendly forms to your command-line applications.",
"support": { "support": {
"issues": "https://github.com/laravel/prompts/issues", "issues": "https://github.com/laravel/prompts/issues",
"source": "https://github.com/laravel/prompts/tree/v0.3.3" "source": "https://github.com/laravel/prompts/tree/v0.3.5"
}, },
"time": "2024-12-30T15:53:31+00:00" "time": "2025-02-11T13:34:40+00:00"
}, },
{ {
"name": "laravel/sanctum", "name": "laravel/sanctum",

22
config/cli.php Normal file
View File

@ -0,0 +1,22 @@
<?php
return [
'providers' => [
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
Illuminate\Filesystem\FilesystemServiceProvider::class,
Illuminate\Foundation\Providers\FoundationServiceProvider::class,
Illuminate\Hashing\HashServiceProvider::class,
Illuminate\Mail\MailServiceProvider::class,
Illuminate\Notifications\NotificationServiceProvider::class,
Illuminate\Pagination\PaginationServiceProvider::class,
Illuminate\Pipeline\PipelineServiceProvider::class,
Illuminate\Queue\QueueServiceProvider::class,
Illuminate\Redis\RedisServiceProvider::class,
Illuminate\Translation\TranslationServiceProvider::class,
App\Providers\AppServiceProvider::class,
],
];