Skip to content

Commit

Permalink
Merge pull request #70 from mohammadisa2/main
Browse files Browse the repository at this point in the history
refactor: change fields method from instance to static access
  • Loading branch information
rupadana authored Nov 28, 2024
2 parents daed44f + fe9b57e commit 063c90e
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
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 }}
];
}
}

0 comments on commit 063c90e

Please sign in to comment.