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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class User extends Model implements HasAllowedFields, HasAllowedSorts, HasAllowe

### Create a Handler

To create a handler you can use this command. By default, i'm using CreateHandler
To create a handler you can use this command. We have 5 Handler, CreateHandler, UpdateHandler, DeleteHandler, DetailHandler, PaginationHandler, If you want a custom handler then write what handler you want.

```bash
php artisan make:filament-api-handler BlogResource
Expand Down
72 changes: 70 additions & 2 deletions src/Commands/MakeApiHandlerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use function Laravel\Prompts\select;
use function Laravel\Prompts\text;

use Illuminate\Support\Facades\File;

class MakeApiHandlerCommand extends Command
{
use CanManipulateFiles;
Expand Down Expand Up @@ -110,16 +112,82 @@ public function handle(): int

$handlerDirectory = "{$baseResourcePath}/Api/Handlers/$handlerClass.php";

$this->copyStubToApp('CustomHandler', $handlerDirectory, [
$stubName = $this->getStubForHandler($handlerClass);
$this->copyStubToApp($stubName, $handlerDirectory, [
'resource' => "{$namespace}\\{$resourceClass}",
'resourceClass' => $resourceClass,
'handlersNamespace' => $handlersNamespace,
'handlerClass' => $handlerClass,
]);

$this->createOrUpdateApiServiceFile($baseResourcePath, $namespace, $resourceClass, $modelClass, $handlerClass);

$this->components->info("Successfully created API Handler for {$resource}!");
$this->components->info("You can register \"Handlers\\$handlerClass::class\" to handlers method on APIService");
$this->components->info("Handler {$handlerClass} has been registered in the APIService.");

return static::SUCCESS;
}

private function getStubForHandler(string $handlerClass): string
{
$handlerMap = [
'CreateHandler' => 'CreateHandler',
'UpdateHandler' => 'UpdateHandler',
'DeleteHandler' => 'DeleteHandler',
'DetailHandler' => 'DetailHandler',
'PaginationHandler' => 'PaginationHandler',
];

return $handlerMap[$handlerClass] ?? 'CustomHandler';
}

private function createOrUpdateApiServiceFile(string $baseResourcePath, string $namespace, string $resourceClass, string $modelClass, string $handlerClass): void
{
$apiServicePath = "{$baseResourcePath}/Api/{$modelClass}ApiService.php";
$apiServiceNamespace = "{$namespace}\\{$resourceClass}\\Api";

if (!File::exists($apiServicePath)) {
$this->copyStubToApp('CustomApiService', $apiServicePath, [
'namespace' => $apiServiceNamespace,
'resource' => "{$namespace}\\{$resourceClass}",
'resourceClass' => $resourceClass,
'apiServiceClass' => "{$modelClass}ApiService",
'handlers' => "Handlers\\{$handlerClass}::class",
]);
} else {
$content = File::get($apiServicePath);
$updatedContent = $this->updateHandlersInContent($content, $handlerClass);
File::put($apiServicePath, $updatedContent);
}
}

private function updateHandlersInContent(string $content, string $newHandler): string
{
$pattern = '/public\s+static\s+function\s+handlers\(\)\s*:\s*array\s*\{[^}]*\}/s';

if (preg_match($pattern, $content, $matches)) {
$handlersBlock = $matches[0];
$handlersList = $this->extractHandlersList($handlersBlock);

if (!in_array("Handlers\\{$newHandler}::class", $handlersList)) {
$handlersList[] = "Handlers\\{$newHandler}::class";
}

$newHandlersBlock = "public static function handlers() : array\n {\n return [\n " .
implode(",\n ", $handlersList) .
"\n ];\n }";

return str_replace($handlersBlock, $newHandlersBlock, $content);
}

return $content;
}

private function extractHandlersList(string $handlersBlock): array
{
preg_match('/return\s*\[(.*?)\]/s', $handlersBlock, $matches);
$handlersListString = $matches[1] ?? '';
$handlersList = array_map('trim', explode(',', $handlersListString));
return array_filter($handlersList);
}
}
19 changes: 19 additions & 0 deletions stubs/CustomApiService.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace {{ namespace }};

use Rupadana\ApiService\ApiService;
use {{ resource }};
use Illuminate\Routing\Router;

class {{ apiServiceClass }} extends ApiService
{
protected static string | null $resource = {{ resourceClass }}::class;

public static function handlers() : array
{
return [
{{ handlers }}
];
}
}