From 36925b06c0cdd286f82e127a7fa8c5b33cdce9c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Zapletal?= Date: Fri, 15 Mar 2024 20:02:53 -0600 Subject: [PATCH] Rename API end-points, Add Recipe resources --- install.sh | 2 +- src/Collection/RecipeEntityMetadata.php | 25 ++++++++++----- src/Collection/RecipeFinder.php | 2 ++ src/Database/Manager/AuthResourceManager.php | 31 ++++++++++--------- src/Http/Request/Collection/CreateRequest.php | 6 ++-- src/Http/Request/Collection/DeleteRequest.php | 6 ++-- .../Request/Collection/ShowOneRequest.php | 6 ++-- src/Http/Request/Collection/ShowRequest.php | 6 ++-- src/Http/Request/Collection/UpdateRequest.php | 6 ++-- src/Subscriber/AuthCollectionRequest.php | 4 +-- 10 files changed, 54 insertions(+), 40 deletions(-) diff --git a/install.sh b/install.sh index 545ad7e..cec72b7 100755 --- a/install.sh +++ b/install.sh @@ -1,7 +1,7 @@ #!/bin/bash # Repository info -VERSION='0.7.9' #can be '0.0.1', 'master', 'develop', 'whatever' +VERSION='0.8.0' #can be '0.0.1', 'master', 'develop', 'whatever' TAGS_OR_HEADS='tags' # 'heads' for branches, 'tags' for tag-releases REPOSITORY_OWNER='strategio-digital' REPOSITORY_NAME='megio-starter' diff --git a/src/Collection/RecipeEntityMetadata.php b/src/Collection/RecipeEntityMetadata.php index 3473307..1f2230b 100644 --- a/src/Collection/RecipeEntityMetadata.php +++ b/src/Collection/RecipeEntityMetadata.php @@ -22,12 +22,9 @@ public function __construct( { } - /** - * @return string - */ - public function getTableName(): string + public function getRecipe(): ICollectionRecipe { - return $this->tableName; + return $this->recipe; } /** @@ -38,6 +35,20 @@ public function getReflection(): \ReflectionClass return $this->entityRef; } + public function getType(): CollectionPropType + { + return $this->type; + } + + /** + * @return string + */ + public function getTableName(): string + { + return $this->tableName; + } + + public function getQbSelect(string $qbAlias): string { $schema = $this->getFullSchemaReflectedByDoctrine(); @@ -47,7 +58,7 @@ public function getQbSelect(string $qbAlias): string /** * @return array{ - * meta: array{table: string, invisible: string[]}, + * meta: array{recipe: string, invisible: string[]}, * props: array{maxLength: int|null, name: string, nullable: bool, type: string}[] * } */ @@ -58,7 +69,7 @@ public function getSchema(): array return [ 'meta' => [ - 'table' => $this->tableName, + 'recipe' => $this->recipe->name(), 'invisible' => $this->recipe->invisibleColumns() ], 'props' => $this->sortColumns($props) diff --git a/src/Collection/RecipeFinder.php b/src/Collection/RecipeFinder.php index 35c9f36..b3e814e 100644 --- a/src/Collection/RecipeFinder.php +++ b/src/Collection/RecipeFinder.php @@ -13,6 +13,8 @@ class RecipeFinder public function load(): self { + $this->recipes = []; + $appFiles = Finder::findFiles()->from(Path::appDir() . '/Recipe'); foreach ($appFiles as $file) { $class = 'App\\Recipe\\' . $file->getBasename('.php'); diff --git a/src/Database/Manager/AuthResourceManager.php b/src/Database/Manager/AuthResourceManager.php index 7167927..16a1cec 100644 --- a/src/Database/Manager/AuthResourceManager.php +++ b/src/Database/Manager/AuthResourceManager.php @@ -7,8 +7,8 @@ namespace Megio\Database\Manager; +use Megio\Collection\RecipeFinder; use Nette\Utils\Strings; -use Megio\Database\EntityFinder; use Megio\Database\Entity\Admin; use Megio\Database\Entity\Auth\Resource; use Megio\Database\EntityManager; @@ -22,7 +22,7 @@ public function __construct( private EntityManager $em, private RouteCollection $routes, - private EntityFinder $entityFinder, + private RecipeFinder $recipeFinder, ) { } @@ -128,19 +128,19 @@ public function routerViewResources(array $viewResources): array public function collectionDataResources(): array { $excluded = [Admin::class]; - $entities = $this->entityFinder->findAll(); - $entities = array_filter($entities, fn($entity) => !in_array($entity['className'], $excluded)); + $recipes = $this->recipeFinder->load()->getAll(); - $tables = array_map(fn($entity) => $entity['table'], $entities); - $resourceNames = array_keys($this->routes->all()); + /** @var \Megio\Collection\ICollectionRecipe[] $recipes */ + $recipes = array_filter($recipes, fn($recipe) => !in_array($recipe->source(), $excluded)); + $recipeNames = array_map(fn($recipe) => $recipe->name(), $recipes); + $resourceNames = array_keys($this->routes->all()); $collectionRouteNames = array_filter($resourceNames, fn($name) => Strings::startsWith($name, Router::ROUTE_COLLECTION_PREFIX)); $names = []; - - foreach ($tables as $tableName) { + foreach ($recipeNames as $recipeName) { foreach ($collectionRouteNames as $routeName) { - $names[] = $routeName . '.' . $tableName; + $names[] = $routeName . '.' . $recipeName; } } @@ -153,14 +153,15 @@ public function collectionDataResources(): array public function collectionNavResources(): array { $excluded = [Admin::class]; - $entities = $this->entityFinder->findAll(); - $entities = array_filter($entities, fn($entity) => !in_array($entity['className'], $excluded)); + $recipes = $this->recipeFinder->load()->getAll(); - $tables = array_map(fn($entity) => $entity['table'], $entities); - $names = []; + /** @var \Megio\Collection\ICollectionRecipe[] $recipes */ + $recipes = array_filter($recipes, fn($recipe) => !in_array($recipe->source(), $excluded)); + $recipeNames = array_map(fn($recipe) => $recipe->name(), $recipes); - foreach ($tables as $tableName) { - $names[] = Router::ROUTE_META_NAVBAR . '.' . $tableName; + $names = []; + foreach ($recipeNames as $recipeName) { + $names[] = Router::ROUTE_META_NAVBAR . '.' . $recipeName; } return $names; diff --git a/src/Http/Request/Collection/CreateRequest.php b/src/Http/Request/Collection/CreateRequest.php index 5f3104f..e403440 100644 --- a/src/Http/Request/Collection/CreateRequest.php +++ b/src/Http/Request/Collection/CreateRequest.php @@ -38,17 +38,17 @@ public function schema(): array // TODO: get rows types trough reflection and validate them return [ - 'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName + 'recipe' => Expect::anyOf(...$names)->required(), 'rows' => Expect::array()->min(1)->max(1000)->required() ]; } public function process(array $data): Response { - $recipe = $this->recipeFinder->findByName($data['table']); + $recipe = $this->recipeFinder->findByName($data['recipe']); if ($recipe === null) { - return $this->error(["Collection {$data['table']} not found"]); + return $this->error(["Collection {$data['recipe']} not found"]); } try { diff --git a/src/Http/Request/Collection/DeleteRequest.php b/src/Http/Request/Collection/DeleteRequest.php index 49bd34f..b95ddd6 100644 --- a/src/Http/Request/Collection/DeleteRequest.php +++ b/src/Http/Request/Collection/DeleteRequest.php @@ -34,17 +34,17 @@ public function schema(): array $names = array_map(fn($r) => $r->name(), $this->recipeFinder->load()->getAll()); return [ - 'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName + 'recipe' => Expect::anyOf(...$names)->required(), 'ids' => Expect::arrayOf('string')->min(1)->required(), ]; } public function process(array $data): Response { - $recipe = $this->recipeFinder->findByName($data['table']); + $recipe = $this->recipeFinder->findByName($data['recipe']); if ($recipe === null) { - return $this->error(["Collection {$data['table']} not found"]); + return $this->error(["Collection {$data['recipe']} not found"]); } try { diff --git a/src/Http/Request/Collection/ShowOneRequest.php b/src/Http/Request/Collection/ShowOneRequest.php index a84d0d9..30b85bd 100644 --- a/src/Http/Request/Collection/ShowOneRequest.php +++ b/src/Http/Request/Collection/ShowOneRequest.php @@ -35,7 +35,7 @@ public function schema(): array $names = array_map(fn($r) => $r->name(), $this->recipeFinder->load()->getAll()); return [ - 'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName + 'recipe' => Expect::anyOf(...$names)->required(), 'schema' => Expect::bool(false), 'id' => Expect::string()->required(), ]; @@ -43,10 +43,10 @@ public function schema(): array public function process(array $data): Response { - $recipe = $this->recipeFinder->findByName($data['table']); + $recipe = $this->recipeFinder->findByName($data['recipe']); if ($recipe === null) { - return $this->error(["Collection {$data['table']} not found"]); + return $this->error(["Collection {$data['recipe']} not found"]); } try { diff --git a/src/Http/Request/Collection/ShowRequest.php b/src/Http/Request/Collection/ShowRequest.php index 4315e66..4e0237e 100644 --- a/src/Http/Request/Collection/ShowRequest.php +++ b/src/Http/Request/Collection/ShowRequest.php @@ -32,7 +32,7 @@ public function schema(): array $names = array_map(fn($r) => $r->name(), $this->recipeFinder->load()->getAll()); return [ - 'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName + 'recipe' => Expect::anyOf(...$names)->required(), 'schema' => Expect::bool(false), 'currentPage' => Expect::int(1)->min(1)->required(), 'itemsPerPage' => Expect::int(10)->max(1000)->required(), @@ -45,10 +45,10 @@ public function schema(): array public function process(array $data): Response { - $recipe = $this->recipeFinder->findByName($data['table']); + $recipe = $this->recipeFinder->findByName($data['recipe']); if ($recipe === null) { - return $this->error(["Collection {$data['table']} not found"]); + return $this->error(["Collection {$data['recipe']} not found"]); } try { diff --git a/src/Http/Request/Collection/UpdateRequest.php b/src/Http/Request/Collection/UpdateRequest.php index 8892e00..beaee42 100644 --- a/src/Http/Request/Collection/UpdateRequest.php +++ b/src/Http/Request/Collection/UpdateRequest.php @@ -38,7 +38,7 @@ public function schema(): array // TODO: get rows types trough reflection and validate them return [ - 'table' => Expect::anyOf(...$names)->required(), // TODO: rename to recipeName + 'recipe' => Expect::anyOf(...$names)->required(), 'rows' => Expect::arrayOf( Expect::structure([ 'id' => Expect::string()->required(), @@ -50,10 +50,10 @@ public function schema(): array public function process(array $data): Response { - $recipe = $this->recipeFinder->findByName($data['table']); + $recipe = $this->recipeFinder->findByName($data['recipe']); if ($recipe === null) { - return $this->error(["Collection {$data['table']} not found"]); + return $this->error(["Collection {$data['recipe']} not found"]); } try { diff --git a/src/Subscriber/AuthCollectionRequest.php b/src/Subscriber/AuthCollectionRequest.php index 993c24d..24817e7 100644 --- a/src/Subscriber/AuthCollectionRequest.php +++ b/src/Subscriber/AuthCollectionRequest.php @@ -54,8 +54,8 @@ public function onProcess(OnProcessingStartEvent $event): void return; } - $tableName = $event->getMetadata()->getTableName(); - $resourceName = $routeName . '.' . $tableName; + $recipeName = $event->getMetadata()->getRecipe()->name(); + $resourceName = $routeName . '.' . $recipeName; if (!in_array($resourceName, $this->authUser->getResources())) { $message = "This collection-resource '{$resourceName}' is not allowed for current user";