From 62fdc8d0771e719fab896e56dee5be19abb291a0 Mon Sep 17 00:00:00 2001 From: John Morrison Date: Wed, 22 Nov 2023 08:00:42 +0000 Subject: [PATCH] Add note on api platform state management --- content-org/api-platform-state-management.org | 122 ++++++++++++++++ .../posts/api-platform-state-management.md | 131 ++++++++++++++++++ 2 files changed, 253 insertions(+) create mode 100644 content-org/api-platform-state-management.org create mode 100644 content/posts/api-platform-state-management.md diff --git a/content-org/api-platform-state-management.org b/content-org/api-platform-state-management.org new file mode 100644 index 0000000..71e2ee4 --- /dev/null +++ b/content-org/api-platform-state-management.org @@ -0,0 +1,122 @@ +#+hugo_base_dir: ~/development/web/jslmorrison.github.io +#+hugo_section: posts +#+options: author:nil + +* State management in Api Platform :symfony:api_platform: +:PROPERTIES: +:EXPORT_FILE_NAME: api-platform-state-management +:EXPORT_DATE: 2023-11-21 +:END: +Fetch data and mutate state for custom api resources when using the [[https://api-platform.com/][Api Platform framework]]. + +#+hugo: more +** Define an api resource +The following defines an api resource for a product. Note the provider classes set for the operations annotation. +#+begin_src php :noeval +id = (string) $entity->id(); + $productResource->name = $entity->name(); + $productResource->price = $entity->price(); + + return $productResource; + } +} +#+end_src + +** State Providers +The following defines a state provider for the above resource. +#+begin_src php :noeval +productRepository->findAll() as $product) { + $products[] = ProductResource::fromEntity($product); + } + + return $products; + } + + return ProductResource::fromEntity($this->productRepository->find(Ulid::fromString($uriVariables['id']))); + } +} +#+end_src + +** State Processors +Continuing with the above resource, in order to mutate its state a new operation should be configured. E.g to enable post request to create new product: +#+begin_src php :noeval +#[ApiResource( + shortName: 'Product', + operations: [ + new Get(provider: ProductProvider::class), + new GetCollection(provider: ProductProvider::class), + new Post(processor: ProductProcessor::class), // new operation defining processor + ] +)] +final class ProductResource +#+end_src + +And the corresponding processor: +#+begin_src php :noeval +// App\State\ProductProcessor.php + +final class ProductProcessor implements ProcessorInterface +{ + public function process($data, Operation $operation, array $uriVariables = [], array $context = []) + { + // mutate and persist data as you like here, e.g: + $this->productRepository->add(ProductResource::toEntity($data)); + + return $data; + } +} +#+end_src + +Read the documentation for more info on [[https://api-platform.com/docs/v3.2/core/state-providers/][state providers]] and [[https://api-platform.com/docs/v3.2/core/state-processors/][state processors]]. diff --git a/content/posts/api-platform-state-management.md b/content/posts/api-platform-state-management.md new file mode 100644 index 0000000..1b55cdb --- /dev/null +++ b/content/posts/api-platform-state-management.md @@ -0,0 +1,131 @@ ++++ +title = "State management in Api Platform" +date = 2023-11-21 +tags = ["symfony", "api-platform"] +draft = false ++++ + +Fetch data and mutate state for custom api resources when using the [Api Platform framework](https://api-platform.com/). + + + + +## Define an api resource {#define-an-api-resource} + +The following defines an api resource for a product. Note the provider classes set for the operations annotation. + +```php +id = (string) $entity->id(); + $productResource->name = $entity->name(); + $productResource->price = $entity->price(); + + return $productResource; + } +} +``` + + +## State Providers {#state-providers} + +The following defines a state provider for the above resource. + +```php +productRepository->findAll() as $product) { + $products[] = ProductResource::fromEntity($product); + } + + return $products; + } + + return ProductResource::fromEntity($this->productRepository->find(Ulid::fromString($uriVariables['id']))); + } +} +``` + + +## State Processors {#state-processors} + +Continuing with the above resource, in order to mutate its state a new operation should be configured. E.g to enable post request to create new product: + +```php +#[ApiResource( + shortName: 'Product', + operations: [ + new Get(provider: ProductProvider::class), + new GetCollection(provider: ProductProvider::class), + new Post(processor: ProductProcessor::class), // new operation defining processor + ] +)] +final class ProductResource +``` + +And the corresponding processor: + +```php +// App\State\ProductProcessor.php + +final class ProductProcessor implements ProcessorInterface +{ + public function process($data, Operation $operation, array $uriVariables = [], array $context = []) + { + // mutate and persist data as you like here, e.g: + $this->productRepository->add(ProductResource::toEntity($data)); + + return $data; + } +} +``` + +Read the documentation for more info on [state providers](https://api-platform.com/docs/v3.2/core/state-providers/) and [state processors](https://api-platform.com/docs/v3.2/core/state-processors/).