-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' of github.com:xtend-packages/rest-presenter in…
…to develop
- Loading branch information
Showing
43 changed files
with
1,345 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/Commands/Generator/stubs/extend/resource.controller.auth.php.stub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace {{ namespace }}; | ||
|
||
use {{ resourceNamespace }} as {{ aliasResource }}; | ||
use XtendPackages\RESTPresenter\StarterKits\Auth\Sanctum\Actions; | ||
|
||
class {{ class }} extends {{ aliasResource }} | ||
{ | ||
public static bool $onlyRegisterActionRoutes = true; | ||
|
||
/** | ||
* @return array<string, string> | ||
*/ | ||
public function routeActions(): array | ||
{ | ||
return [ | ||
'register' => Actions\Register::class, | ||
'login' => Actions\Login::class, | ||
'logout' => Actions\Logout::class, | ||
'reset-password' => Actions\ResetPassword::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace XtendPackages\RESTPresenter\Commands; | ||
|
||
use Illuminate\Console\Command; | ||
use Illuminate\Filesystem\Filesystem; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use XtendPackages\RESTPresenter\Concerns\InteractsWithGit; | ||
|
||
use function Laravel\Prompts\confirm; | ||
|
||
#[AsCommand(name: 'rest-presenter:filament')] | ||
final class RESTPresenterFilamentCommand extends Command | ||
{ | ||
use InteractsWithGit; | ||
|
||
protected $signature = 'rest-presenter:filament | ||
{--install : Install REST Presenter for Filament} | ||
{--uninstall : Uninstall REST Presenter for Filament}'; | ||
|
||
protected $description = 'REST Presenter for Filament'; | ||
|
||
public function __construct(protected Filesystem $filesystem) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
public function handle(): int | ||
{ | ||
if ($this->option('install')) { | ||
return $this->install(); | ||
} | ||
|
||
if (! $this->filesystem->exists(config('rest-presenter.generator.path').'/StarterKits')) { | ||
$this->components->warn('REST Presenter for Filament is not currently installed.'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
if ($this->option('uninstall')) { | ||
return $this->uninstall(); | ||
} | ||
|
||
$this->components->warn('Please specify an option: --install, --uninstall'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
private function install(): int | ||
{ | ||
$this->components->info('Installing REST Presenter for Filament'); | ||
|
||
if (confirm(__('Would you like to auto-commit all changes made by the installer?'))) { | ||
$this->gitAutoCommit = $this->isCleanWorkingDirectory(); | ||
} | ||
|
||
$this->call('rest-presenter:xtend-starter-kit', ['name' => 'Sanctum']); | ||
if ($this->gitAutoCommit) { | ||
$this->commitChanges('feat: REST Presenter Sanctum Starter Kit'); | ||
} | ||
|
||
$this->call('rest-presenter:xtend-starter-kit', ['name' => 'Filament']); | ||
if ($this->gitAutoCommit) { | ||
$this->commitChanges('feat: REST Presenter Filament Starter Kit'); | ||
} | ||
|
||
$this->components->info('REST Presenter Filament installed successfully 🚀'); | ||
|
||
$this->components->info('Next step when your ready run "php artisan rest-presenter:generate-api-collection" to auto-generate your API collection for Insomnia or Postman.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
|
||
private function uninstall(): int | ||
{ | ||
if (! confirm('Are you sure you want to uninstall REST Presenter for Filament?')) { | ||
return self::FAILURE; | ||
} | ||
|
||
if (confirm(__('Would you like to auto-commit revert changes made by the installer?'))) { | ||
$this->gitAutoCommit = $this->isCleanWorkingDirectory(); | ||
} | ||
|
||
$this->filesystem->delete(config_path('rest-presenter.php')); | ||
$this->filesystem->deleteDirectory(config('rest-presenter.generator.path').'/StarterKits'); | ||
$this->filesystem->deleteDirectory(app()->basePath('tests/StarterKits')); | ||
$this->filesystem->deleteDirectory(resource_path('rest-presenter')); | ||
|
||
if ($this->gitAutoCommit) { | ||
$this->commitChanges('revert: Remove REST Presenter Sanctum & Filament Starter Kits'); | ||
} | ||
|
||
$this->components->info('REST Presenter Filament uninstalled successfully.'); | ||
|
||
return self::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace XtendPackages\RESTPresenter\Concerns; | ||
|
||
use Illuminate\Support\Facades\Process; | ||
|
||
trait InteractsWithGit | ||
{ | ||
protected bool $gitAutoCommit = false; | ||
|
||
protected function commitChanges(string $message): void | ||
{ | ||
$this->components->info('Committing changes...'); | ||
|
||
Process::run('git add .'); | ||
Process::run('git commit -m "'.$message.'"'); | ||
|
||
$this->components->info('Changes committed successfully'); | ||
} | ||
|
||
protected function isCleanWorkingDirectory(): bool | ||
{ | ||
$cleanDir = (Process::run('git diff --quiet'))->exitCode() === 0; | ||
|
||
if (! $cleanDir) { | ||
$this->components->warn('Please commit or stash your changes before proceeding with auto-commit'); | ||
} | ||
|
||
return $cleanDir; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace XtendPackages\RESTPresenter\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Str; | ||
use InvalidArgumentException; | ||
use Sushi\Sushi; | ||
|
||
/** | ||
* @property int $id | ||
* @property string $group | ||
* @property string $route | ||
* @property string $type | ||
* @property string $uri | ||
* @property bool $is_authenticated | ||
*/ | ||
class Endpoint extends Model | ||
{ | ||
use Sushi; | ||
|
||
/** | ||
* @return array<mixed> | ||
*/ | ||
public function getRows(): array | ||
{ | ||
return Http::withOptions(type(['verify' => false])->asArray()) | ||
->withHeader('X-REST-PRESENTER-API-KEY', type(config('rest-presenter.auth.key'))->asString()) | ||
->get(route('api.v1.resources')) | ||
->collect()->transform(function ($v, $k) { | ||
if (! is_array($v)) { | ||
throw new InvalidArgumentException('v must be an array'); | ||
} | ||
|
||
$group = Str::of($v['name']) | ||
->beforeLast('.') | ||
->afterLast('.') | ||
->title() | ||
->value(); | ||
|
||
if ($group === 'V1') { | ||
$group = 'API Resources'; | ||
} | ||
|
||
$authenticatedRoute = false; | ||
if ($v['middleware'] ?? false) { | ||
$authenticatedRoute = collect(type($v['middleware'])->asArray())->contains('auth:sanctum'); | ||
} | ||
|
||
return [ | ||
'id' => $k + 1, | ||
'group' => $group, | ||
'route' => $v['name'], | ||
'type' => $v['methods'][0], | ||
'uri' => $v['uri'], | ||
'is_authenticated' => $authenticatedRoute, | ||
]; | ||
})->toArray(); | ||
} | ||
} |
Oops, something went wrong.