Skip to content

Commit

Permalink
- documentation update
Browse files Browse the repository at this point in the history
- compatibility fix: now it is possible to declare several custom HTTP methods for single handler method using PHP attributes just like using annotations
- minor dependency updates
  • Loading branch information
Arthur Mogliev committed Apr 4, 2021
1 parent 467ec6d commit 6836869
Show file tree
Hide file tree
Showing 16 changed files with 693 additions and 344 deletions.
50 changes: 43 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,59 @@ class Handler
}
}
```
...or special PHP attributes:
```PHP
namespace My;

use Articus\PathHandler\PhpAttribute as PHA;
use Articus\PathHandler\Exception;
use Psr\Http\Message\ServerRequestInterface;

#[PHA\Route('/entity')] //This is how you set path for handler operations
class Handler
{
#[PHA\Post()] //This is how you declare HTTP method of the operation
#[PHA\Consumer('application/json', 'Json')] //This is how you consume request body
#[PHA\Attribute('Transfer', ['type'=>'My\DTO','objectAttr'=>'dto','errorAttr'=>'errors'])] //This is how you attribute request
#[PHA\Producer('application/json', 'Json')] //This is how you produce response body from returned value
public function handlePost(ServerRequestInterface $request): \My\DTO
{
$errors = $request->getAttribute('errors');
if (!empty($errors))
{
//This is how you can return non-200 responses
throw new Exception\UnprocessableEntity($errors);
}
/* @var \My\DTO $dto */
$dto = $request->getAttribute('dto');
return $dto;
}
}
```

Finally you need to configure special factory for router service. Here is a sample configuration for [Laminas Service Manager](https://docs.laminas.dev/laminas-servicemanager/) (example is in YAML just for readability):

```YAML
dependencies:
factories:
Mezzio\Router\RouterInterface: Articus\PathHandler\RouteInjection\Factory

Articus\PathHandler\RouteInjection\Factory:
Mezzio\Router\RouterInterface: Articus\PathHandler\RouteInjectionFactory
Articus\PathHandler\MetadataProviderInterface: Articus\PathHandler\MetadataProvider\Factory\Annotation
# Replace previous line with this one if you want use PHP attributes as metadata source
#Articus\PathHandler\MetadataProviderInterface: Articus\PathHandler\MetadataProvider\Factory\PhpAttribute
Articus\PathHandler\Handler\PluginManager: Articus\PathHandler\Handler\Factory\PluginManager
Articus\PathHandler\Consumer\PluginManager: Articus\PathHandler\Consumer\Factory\PluginManager
Articus\PathHandler\Attribute\PluginManager: Articus\PathHandler\Attribute\Factory\PluginManager
Articus\PathHandler\Producer\PluginManager: Articus\PathHandler\Producer\Factory\PluginManager

Articus\PathHandler\RouteInjectionFactory:
paths:
'':
# List of your handlers
- My\Handler
# Configuration for handler plugin manager - sub-container dedicated for handlers
handlers:
factories:
My\Handler: My\HandlerFactory
# Configuration for handler plugin manager - sub-container dedicated for handlers
Articus\PathHandler\Handler\PluginManager:
factories:
My\Handler: My\HandlerFactory
```
For more details check [documentation](http://pathhandler.readthedocs.io/en/latest/).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"psr/simple-cache": "^1.0",
"laminas/laminas-stdlib": "^3.2",
"nikic/fast-route": "^1.3",
"articus/data-transfer": "^0.4",
"articus/data-transfer": "^0.5",
"ext-json": "*",
"ext-uopz": "*",
"phpspec/phpspec": "^5.1|^6.1|^7.0",
Expand Down
Loading

0 comments on commit 6836869

Please sign in to comment.