diff --git a/Controller/Annotations/View.php b/Controller/Annotations/View.php index 6888cc46a..95583c13c 100644 --- a/Controller/Annotations/View.php +++ b/Controller/Annotations/View.php @@ -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; @@ -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 */ @@ -45,7 +66,7 @@ public function setStatusCode($statusCode) } /** - * @return int + * @return int|null */ public function getStatusCode() { diff --git a/Resources/doc/annotations-reference.rst b/Resources/doc/annotations-reference.rst index 09d82409a..d26589f64 100644 --- a/Resources/doc/annotations-reference.rst +++ b/Resources/doc/annotations-reference.rst @@ -68,17 +68,32 @@ FileParam View ---- -.. code-block:: php +.. tabs:: - use FOS\RestBundle\Controller\Annotations\View; + .. tab:: Annotations - /** - * @View( - * statusCode=null, - * serializerGroups={}, - * serializerEnableMaxDepthChecks=false - * ) - */ + .. code-block:: php + + use FOS\RestBundle\Controller\Annotations\View; + + /** + * @View( + * statusCode=null, + * serializerGroups={}, + * serializerEnableMaxDepthChecks=false + * ) + */ + .. tab:: Attributes + + .. code-block:: php + + use FOS\RestBundle\Controller\Annotations\View; + + #[View( + statusCode: null, + serializerGroups: [], + serializerEnableMaxDepthChecks: false + )] Routing ------- diff --git a/Tests/Functional/Bundle/TestBundle/Controller/RouteAttributesController.php b/Tests/Functional/Bundle/TestBundle/Controller/AttributesController.php similarity index 67% rename from Tests/Functional/Bundle/TestBundle/Controller/RouteAttributesController.php rename to Tests/Functional/Bundle/TestBundle/Controller/AttributesController.php index bb1350be2..b3c022547 100644 --- a/Tests/Functional/Bundle/TestBundle/Controller/RouteAttributesController.php +++ b/Tests/Functional/Bundle/TestBundle/Controller/AttributesController.php @@ -13,43 +13,30 @@ 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'])] + #[Rest\View] 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; + ]; } } diff --git a/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml b/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml index c60ce44c7..7cdf3c8c5 100644 --- a/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml +++ b/Tests/Functional/Bundle/TestBundle/Resources/config/routing.yml @@ -75,5 +75,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 diff --git a/composer.json b/composer.json index 595c2380f..4c914d47d 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,7 @@ "jms/serializer-bundle": "^2.4.3|^3.0.1|^4.0", "psr/http-message": "^1.0", "psr/log": "^1.0|^2.0|^3.0", - "sensio/framework-extra-bundle": "^5.2.3|^6.0", + "sensio/framework-extra-bundle": "^6.1", "symfony/phpunit-bridge": "^5.3|^6.0", "symfony/asset": "^4.4|^5.3|^6.0", "symfony/browser-kit": "^4.4|^5.3|^6.0", @@ -71,7 +71,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"