From 62874dd65132633916e3bbb7eb235ea95259cfd2 Mon Sep 17 00:00:00 2001 From: Micha Hobert Date: Mon, 18 Dec 2023 17:06:17 +0100 Subject: [PATCH] Replace old annotations (#1238) * Replace old annotations * Add _httpCache * Fix wrong replaces * Change routeScope --- concepts/framework/http_cache.md | 8 ++--- .../plugins/checkout/cart/add-cart-items.md | 8 ++--- .../plugins/content/seo/add-custom-seo-url.md | 8 ++--- .../plugins/framework/event/finding-events.md | 6 +--- .../add-rate-limiter-to-api-route.md | 14 ++++----- .../add-caching-for-store-api-route.md | 9 ++---- .../store-api/add-store-api-route.md | 17 +++-------- .../store-api/override-existing-route.md | 11 ++----- .../add-caching-to-custom-controller.md | 10 ++----- .../storefront/add-custom-controller.md | 30 +++++-------------- .../plugins/storefront/add-custom-page.md | 12 ++------ .../plugins/storefront/add-custom-pagelet.md | 4 +-- .../storefront/add-data-to-storefront-page.md | 4 +-- .../add-dynamic-content-via-ajax-calls.md | 8 ++--- .../guidelines/code/storefront-controller.md | 7 ++--- 15 files changed, 42 insertions(+), 114 deletions(-) diff --git a/concepts/framework/http_cache.md b/concepts/framework/http_cache.md index 5e346f088..304a31a90 100644 --- a/concepts/framework/http_cache.md +++ b/concepts/framework/http_cache.md @@ -40,14 +40,10 @@ The Shopware HTTP cache has a variety of mechanisms to answer these questions. ## When will the page be cached? -The called route needs an `@HttpCache` annotation. Examples for this can be found in the [ProductController](https://github.com/shopware/shopware/blob/v6.3.4.1/src/Storefront/Controller/ProductController.php#L86). +Set the defaults value of the `_httpCache` key to `true`. Examples for this can be found in the [ProductController](https://github.com/shopware/shopware/blob/trunk/src/Storefront/Controller/ProductController.php#L62). ```php -/** - * @Since("6.3.3.0") - * @HttpCache() - * @Route("/detail/{productId}", name="frontend.detail.page", methods={"GET"}) - */ +#[Route(path: '/detail/{productId}', name: 'frontend.detail.page', methods: ['GET'], defaults: ['_httpCache' => true])] public function index(SalesChannelContext $context, Request $request): Response ``` diff --git a/guides/plugins/plugins/checkout/cart/add-cart-items.md b/guides/plugins/plugins/checkout/cart/add-cart-items.md index d2bc46a67..c8a10bb20 100644 --- a/guides/plugins/plugins/checkout/cart/add-cart-items.md +++ b/guides/plugins/plugins/checkout/cart/add-cart-items.md @@ -42,9 +42,7 @@ use Shopware\Storefront\Framework\Routing\StorefrontResponse; use Symfony\Component\Routing\Annotation\Route; use Shopware\Core\Checkout\Cart\Cart; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { private LineItemFactoryRegistry $factory; @@ -57,9 +55,7 @@ class ExampleController extends StorefrontController $this->cartService = $cartService; } - /** - * @Route("/cartAdd", name="frontend.example", methods={"GET"}) - */ + #[Route(path: '/cartAdd', name: 'frontend.example', methods: ['GET'])] public function add(Cart $cart, SalesChannelContext $context): StorefrontResponse { // Create product line item diff --git a/guides/plugins/plugins/content/seo/add-custom-seo-url.md b/guides/plugins/plugins/content/seo/add-custom-seo-url.md index 2a1ec3243..6f94b2e7c 100644 --- a/guides/plugins/plugins/content/seo/add-custom-seo-url.md +++ b/guides/plugins/plugins/content/seo/add-custom-seo-url.md @@ -52,14 +52,10 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { - /** - * @Route("/example", name="frontend.example.example", methods={"GET"}) - */ + #[Route(path: '/example', name: 'frontend.example.example', methods: ['GET'])] public function showExample(): Response { return $this->renderStorefront('@SwagBasicExample/storefront/page/example/index.html.twig', [ diff --git a/guides/plugins/plugins/framework/event/finding-events.md b/guides/plugins/plugins/framework/event/finding-events.md index d8fc61dda..4ab20870e 100644 --- a/guides/plugins/plugins/framework/event/finding-events.md +++ b/guides/plugins/plugins/framework/event/finding-events.md @@ -178,11 +178,7 @@ a `Criteria` instance. Let's have a look at an [example code](https://github.com/shopware/shopware/blob/v6.4.0.0/src/Core/Content/Product/SalesChannel/Listing/ResolveCriteriaProductListingRoute.php#L55-L59): ```php -/** - * @Since("6.2.0.0") - * @Entity("product") - * @Route("/store-api/product-listing/{categoryId}", name="store-api.product.listing", methods={"POST"}) - */ +#[Route(path: '/store-api/product-listing/{categoryId}', name: 'store-api.product.listing', methods: ['POST'], defaults: ['_entity' => 'product'])] public function load(string $categoryId, Request $request, SalesChannelContext $context, Criteria $criteria): ProductListingRouteResponse { $this->eventDispatcher->dispatch( diff --git a/guides/plugins/plugins/framework/rate-limiter/add-rate-limiter-to-api-route.md b/guides/plugins/plugins/framework/rate-limiter/add-rate-limiter-to-api-route.md index d32d0666b..1693226b4 100644 --- a/guides/plugins/plugins/framework/rate-limiter/add-rate-limiter-to-api-route.md +++ b/guides/plugins/plugins/framework/rate-limiter/add-rate-limiter-to-api-route.md @@ -120,9 +120,7 @@ namespace Swag\BasicExample\Core\Content\Example\SalesChannel; use Shopware\Core\Framework\RateLimiter\RateLimiter; ... -/** - * @Route(defaults={"_routeScope"={"store-api"}}) - */ +#[Route(defaults: ['_routeScope' => ['store-api']])] class ExampleRoute extends AbstractExampleRoute { private RateLimiter $rateLimiter; @@ -150,9 +148,8 @@ If the limit has been exceeded, it throws `Shopware\Core\Framework\RateLimiter\E ```php // /src/Core/Content/Example/SalesChannel/ExampleRoute.php -/** - * @Route("/store-api/example", name="store-api.example.search", methods={"GET", "POST"}) -*/ + +#[Route(path: '/store-api/example', name: 'store-api.example.search', methods: ['GET','POST'])] public function load(Request $request, SalesChannelContext $context): ExampleRouteResponse { // Limit ip address @@ -169,9 +166,8 @@ We just have to call the `reset` method as you can see below. ```php // /src/Core/Content/Example/SalesChannel/ExampleRoute.php -/** - * @Route("/store-api/example", name="store-api.example.search", methods={"GET", "POST"}) -*/ + +#[Route(path: '/store-api/example', name: 'store-api.example.search', methods: ['GET','POST'])] public function load(Request $request, SalesChannelContext $context): ExampleRouteResponse { // Limit ip address for example diff --git a/guides/plugins/plugins/framework/store-api/add-caching-for-store-api-route.md b/guides/plugins/plugins/framework/store-api/add-caching-for-store-api-route.md index 7371a2580..5af665b57 100644 --- a/guides/plugins/plugins/framework/store-api/add-caching-for-store-api-route.md +++ b/guides/plugins/plugins/framework/store-api/add-caching-for-store-api-route.md @@ -51,9 +51,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; -/** - * @Route(defaults={"_routeScope"={"store-api"}}) - */ +#[Route(defaults: ['_routeScope' => ['store-api']])] class CachedExampleRoute extends AbstractExampleRoute { private AbstractExampleRoute $decorated; @@ -90,10 +88,7 @@ class CachedExampleRoute extends AbstractExampleRoute return $this->decorated; } - /** - * @Entity("swag_example") - * @Route("/store-api/example", name="store-api.example.search", methods={"GET", "POST"}) - */ + #[Route(path: '/store-api/example', name: 'store-api.example.search', methods: ['GET','POST'], defaults: ['_entity' => 'swag_example'])] public function load(Criteria $criteria, SalesChannelContext $context): ExampleRouteResponse { // The context is provided with a state where the route cannot be cached diff --git a/guides/plugins/plugins/framework/store-api/add-store-api-route.md b/guides/plugins/plugins/framework/store-api/add-store-api-route.md index e43fb3ca2..d7e84c0a7 100644 --- a/guides/plugins/plugins/framework/store-api/add-store-api-route.md +++ b/guides/plugins/plugins/framework/store-api/add-store-api-route.md @@ -63,9 +63,7 @@ use Shopware\Core\Framework\Routing\Annotation\Entity; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"store-api"}}) - */ +#[Route(defaults: ['_routeScope' => ['store-api']])] class ExampleRoute extends AbstractExampleRoute { protected EntityRepository $exampleRepository; @@ -80,10 +78,7 @@ class ExampleRoute extends AbstractExampleRoute throw new DecorationPatternException(self::class); } - /** - * @Entity("swag_example") - * @Route("/store-api/example", name="store-api.example.search", methods={"GET", "POST"}) - */ + #[Route(path: '/store-api/example', name: 'store-api.example.search', methods: ['GET','POST'], defaults: ['_entity' => 'swag_example'])] public function load(Criteria $criteria, SalesChannelContext $context): ExampleRouteResponse { return new ExampleRouteResponse($this->exampleRepository->search($criteria, $context->getContext())); @@ -318,9 +313,7 @@ use Swag\BasicExample\Core\Content\Example\SalesChannel\AbstractExampleRoute; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { private AbstractExampleRoute $route; @@ -330,9 +323,7 @@ class ExampleController extends StorefrontController $this->route = $route; } - /** - * @Route("/example", name="frontend.example.search", methods={"GET", "POST"}, defaults={"XmlHttpRequest"=true}) - */ + #[Route(path: '/example', name: 'frontend.example.search', methods: ['GET', 'POST'], defaults: ['XmlHttpRequest' => 'true']) public function load(Criteria $criteria, SalesChannelContext $context): Response { return $this->route->load($criteria, $context); diff --git a/guides/plugins/plugins/framework/store-api/override-existing-route.md b/guides/plugins/plugins/framework/store-api/override-existing-route.md index 43729771a..751aba565 100644 --- a/guides/plugins/plugins/framework/store-api/override-existing-route.md +++ b/guides/plugins/plugins/framework/store-api/override-existing-route.md @@ -33,9 +33,7 @@ use Shopware\Core\Framework\Routing\Annotation\Entity; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"store-api"}}) - */ +#[Route(defaults: ['_routeScope' => ['store-api']])] class ExampleRouteDecorator extends AbstractExampleRoute { protected EntityRepository $exampleRepository; @@ -52,11 +50,8 @@ class ExampleRouteDecorator extends AbstractExampleRoute { return $this->decorated; } - - /** - * @Entity("swag_example") - * @Route("/store-api/example", name="store-api.example.search", methods={"GET", "POST"}) - */ + + #[Route(path: '/store-api/example', name: 'store-api.example.search', methods: ['GET', 'POST'], defaults: ['_entity' => 'category'])] public function load(Criteria $criteria, SalesChannelContext $context): ExampleRouteResponse { // We must call this function when using the decorator approach diff --git a/guides/plugins/plugins/storefront/add-caching-to-custom-controller.md b/guides/plugins/plugins/storefront/add-caching-to-custom-controller.md index c51b6650b..eba3c2343 100644 --- a/guides/plugins/plugins/storefront/add-caching-to-custom-controller.md +++ b/guides/plugins/plugins/storefront/add-caching-to-custom-controller.md @@ -38,15 +38,11 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { - /** - * @HttpCache() - * @Route("/example", name="frontend.example.example", methods={"GET"}) - */ + + #[Route(path: '/example', name: 'frontend.example.example', methods: ['GET'], defaults: ['_httpCache' => true])] public function showExample(): Response { return $this->renderStorefront('@SwagBasicExample/storefront/page/example/index.html.twig', [ diff --git a/guides/plugins/plugins/storefront/add-custom-controller.md b/guides/plugins/plugins/storefront/add-custom-controller.md index 15b513190..bb768b5d4 100644 --- a/guides/plugins/plugins/storefront/add-custom-controller.md +++ b/guides/plugins/plugins/storefront/add-custom-controller.md @@ -39,9 +39,7 @@ namespace Swag\BasicExample\Storefront\Controller; use Shopware\Storefront\Controller\StorefrontController; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { } @@ -63,14 +61,10 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { - /** - * @Route("/example", name="frontend.example.example", methods={"GET"}) - */ + #[Route(path: '/example', name: 'frontend.example.example', methods: ['GET'])] public function showExample(): Response { return $this->renderStorefront('@SwagBasicExample/storefront/page/example.html.twig', [ @@ -98,14 +92,10 @@ use Shopware\Storefront\Controller\StorefrontController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { - /** - * @Route("/example", name="frontend.example.example", methods={"GET"}, defaults={"_routeScope"={"storefront"}}) - */ + #[Route(path: '/example', name: 'frontend.example.example', methods: ['GET'], defaults: ['_routeScope' => 'storefront'])] public function showExample(): Response { ... @@ -189,14 +179,10 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController -{ - /** - * @Route("/example", name="frontend.example.example", methods={"GET"}) - */ +{ + #[Route(path: '/example', name: 'frontend.example.example', methods: ['GET'])] public function showExample(Request $request, SalesChannelContext $context): Response { ... diff --git a/guides/plugins/plugins/storefront/add-custom-page.md b/guides/plugins/plugins/storefront/add-custom-page.md index 71e7b7882..af2c1cbb7 100644 --- a/guides/plugins/plugins/storefront/add-custom-page.md +++ b/guides/plugins/plugins/storefront/add-custom-page.md @@ -33,14 +33,10 @@ use Shopware\Storefront\Controller\StorefrontController; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { - /** - * @Route("/example-page", name="frontend.example.page", methods={"GET"}) - */ + #[Route(path: '/example-page', name: 'frontend.example.page', methods: ['GET'])] public function examplePage(): Response { } @@ -151,9 +147,7 @@ class ExampleController extends StorefrontController $this->examplePageLoader = $examplePageLoader; } - /** - * @Route("/example-page", name="frontend.example.page", methods={"GET"}) - */ + #[Route(path: '/example-page', name: 'frontend.example.page', methods: ['GET'])] public function examplePage(Request $request, SalesChannelContext $context): Response { $page = $this->examplePageLoader->load($request, $context); diff --git a/guides/plugins/plugins/storefront/add-custom-pagelet.md b/guides/plugins/plugins/storefront/add-custom-pagelet.md index c059b7031..042c1a99a 100644 --- a/guides/plugins/plugins/storefront/add-custom-pagelet.md +++ b/guides/plugins/plugins/storefront/add-custom-pagelet.md @@ -175,9 +175,7 @@ Of course, in this example your `ExamplePage` struct needs a method `setExampleP As already mentioned, a pagelet can be loaded via a route if you want it to. For that case, you can simply add a new route to your controller and load the pagelet via the `ExamplePageletLoader`: ```php -/** - * @Route("/example-pagelet", name="frontend.example.pagelet", methods={"POST"}, defaults={"XmlHttpRequest"=true}) - */ +#[Route(path: '/example-pagelet', name: 'frontend.example.pagelet', methods: ['POST'], defaults: ['XmlHttpRequest' => 'true'])] public function examplePagelet(Request $request, SalesChannelContext $context): Response { $pagelet = $this->examplePageletLoader->load($request, $context); diff --git a/guides/plugins/plugins/storefront/add-data-to-storefront-page.md b/guides/plugins/plugins/storefront/add-data-to-storefront-page.md index 482e66d37..d2211bce9 100644 --- a/guides/plugins/plugins/storefront/add-data-to-storefront-page.md +++ b/guides/plugins/plugins/storefront/add-data-to-storefront-page.md @@ -99,9 +99,7 @@ use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria; use Shopware\Core\System\SalesChannel\SalesChannelContext; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"store-api"}}) - */ +#[Route(defaults: ['_routeScope' => ['store-api']])] abstract class AbstractProductCountRoute { abstract public function getDecorated(): AbstractProductCountRoute; diff --git a/guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md b/guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md index 7a40ab0cd..d4824cfc1 100644 --- a/guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md +++ b/guides/plugins/plugins/storefront/add-dynamic-content-via-ajax-calls.md @@ -31,14 +31,10 @@ use Shopware\Storefront\Controller\StorefrontController; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\Routing\Annotation\Route; -/** - * @Route(defaults={"_routeScope"={"storefront"}}) - */ +#[Route(defaults: ['_routeScope' => ['storefront']])] class ExampleController extends StorefrontController { - /** - * @Route("/example", name="frontend.example.example", defaults={"XmlHttpRequest"=true}, methods={"GET"}) - */ + #[Route(path: '/example', name: 'frontend.example.example', methods: ['GET'], defaults: ['XmlHttpRequest' => 'true'])] public function showExample(): JsonResponse { return new JsonResponse(['timestamp' => (new \DateTime())->format(\DateTimeInterface::W3C)]); diff --git a/resources/guidelines/code/storefront-controller.md b/resources/guidelines/code/storefront-controller.md index 161c769f0..7ca90801b 100644 --- a/resources/guidelines/code/storefront-controller.md +++ b/resources/guidelines/code/storefront-controller.md @@ -9,8 +9,7 @@ nav: ## Controller -* Each controller action has to be declared with a `@Since` tag. -* Each controller action requires a `@Route` annotation. +* Each controller action requires a `#Route` attribute. * The name of the route should start with "frontend". * Each route should define the corresponding HTTP Method \(GET, POST, DELETE, PATCH\). * The function name should be concise. @@ -19,7 +18,7 @@ nav: * Use Symfony flash bags for error reporting. * Each storefront functionality has to be available inside the Store API too. * A Storefront controller should never contain business logic. -* The class requires the annotation: `@Route(defaults={"_routeScope"={"storefront"}})`. +* The class requires the attribute: `#[Route(defaults: ['_routeScope' => ['storefront']])]`. * Depending services have to be injected over the class constructor. * Depending services have to be defined in the DI-Container service definition. * Depending services have to be assigned to a private class property. @@ -29,7 +28,7 @@ nav: * A Storefront controller should never use a repository directly. The data should be fetched over a route or page loader. * Routes that load a full Storefront page should use a page loader class to load all corresponding data. -* Pages that contain data that are the same for all customers should have the `@HttpCache` annotation. +* Pages that contain data that are the same for all customers should have the `_httpCache` annotation. ## Write operations inside Storefront controllers