Skip to content

Commit

Permalink
Add support for native PHP8 View attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
W0rma committed Aug 19, 2021
1 parent 3c9a2cb commit 2a4a71e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
25 changes: 23 additions & 2 deletions Controller/Annotations/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@
* @Annotation
* @Target({"METHOD","CLASS"})
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_METHOD)]
class View extends Template
{
/**
* @var int
* @var int|null
*/
protected $statusCode;

Expand All @@ -36,6 +37,26 @@ class View extends Template
*/
protected $serializerEnableMaxDepthChecks;

/**
* @param array|string $data
*/
public function __construct(
$data = [],
array $vars = [],
bool $isStreamable = false,
array $owner = [],
?int $statusCode = null,
array $serializerGroups = [],
bool $serializerEnableMaxDepthChecks = false
) {
parent::__construct($data, $vars, $isStreamable, $owner);

$values = is_array($data) ? $data : [];
$this->statusCode = $values['statusCode'] ?? $statusCode;
$this->serializerGroups = $values['serializerGroups'] ?? $serializerGroups;
$this->serializerEnableMaxDepthChecks = $values['serializerEnableMaxDepthChecks'] ?? $serializerEnableMaxDepthChecks;
}

/**
* @param int $statusCode
*/
Expand All @@ -45,7 +66,7 @@ public function setStatusCode($statusCode)
}

/**
* @return int
* @return int|null
*/
public function getStatusCode()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,29 @@

use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\View\View;
use Symfony\Component\HttpFoundation\Request;

/**
* Controller to test native PHP8 Route attributes.
* Controller to test native PHP8 attributes.
*/
#[Rest\Route('/products')]
class RouteAttributesController extends AbstractFOSRestController
class AttributesController extends AbstractFOSRestController
{
/**
* @return View view instance
*
* @Rest\View()
*/
#[Rest\Get(path: '/{page}', name: 'product_list', requirements: ['page' => '\d+'], defaults: ['_format' => 'json'])]
public function listAction(int $page)
{
$view = $this->view([
return [
['name' => 'product1'],
['name' => 'product2'],
]);

return $view;
];
}

/**
* @return View view instance
*
* @Rest\View()
*/
#[Rest\Post(path: '', name: 'product_create')]
#[Rest\View(statusCode: 201)]
public function createAction(Request $request)
{
$view = $this->view([
return [
'name' => 'product1',
], 201);

return $view;
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ test_allowed_methods2:
methods: ['POST', 'PUT']
defaults: { _controller: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\AllowedMethodsController::indexAction }

test_route_attributes:
resource: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\RouteAttributesController
test_php8_attributes:
resource: FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller\AttributesController
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"require-dev": {
"doctrine/annotations": "^1.13.2",
"psr/log": "^1.0|^2.0|^3.0",
"sensio/framework-extra-bundle": "^5.2.3",
"sensio/framework-extra-bundle": "^6.1",
"symfony/phpunit-bridge": "^5.2",
"symfony/asset": "^4.4|^5.0",
"symfony/form": "^4.4|^5.0",
Expand All @@ -70,7 +70,7 @@
},
"conflict": {
"doctrine/annotations": "<1.12",
"sensio/framework-extra-bundle": "<5.2.3",
"sensio/framework-extra-bundle": "<6.1",
"symfony/error-handler": "<4.4.1",
"jms/serializer-bundle": "<2.4.3|3.0.0",
"jms/serializer": "<1.13.0"
Expand Down

0 comments on commit 2a4a71e

Please sign in to comment.