Skip to content

Commit

Permalink
Replace old annotations (#1238)
Browse files Browse the repository at this point in the history
* Replace old annotations

* Add _httpCache

* Fix wrong replaces

* Change routeScope
  • Loading branch information
Isengo1989 authored Dec 18, 2023
1 parent 13cab6d commit 62874dd
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 114 deletions.
8 changes: 2 additions & 6 deletions concepts/framework/http_cache.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down
8 changes: 2 additions & 6 deletions guides/plugins/plugins/checkout/cart/add-cart-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
8 changes: 2 additions & 6 deletions guides/plugins/plugins/content/seo/add-custom-seo-url.md
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
6 changes: 1 addition & 5 deletions guides/plugins/plugins/framework/event/finding-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -150,9 +148,8 @@ If the limit has been exceeded, it throws `Shopware\Core\Framework\RateLimiter\E

```php
// <plugin root>/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
Expand All @@ -169,9 +166,8 @@ We just have to call the `reset` method as you can see below.

```php
// <plugin root>/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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
17 changes: 4 additions & 13 deletions guides/plugins/plugins/framework/store-api/add-store-api-route.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down
30 changes: 8 additions & 22 deletions guides/plugins/plugins/storefront/add-custom-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
Expand All @@ -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', [
Expand Down Expand Up @@ -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
{
...
Expand Down Expand Up @@ -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
{
...
Expand Down
12 changes: 3 additions & 9 deletions guides/plugins/plugins/storefront/add-custom-page.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 1 addition & 3 deletions guides/plugins/plugins/storefront/add-custom-pagelet.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
Expand Down
7 changes: 3 additions & 4 deletions resources/guidelines/code/storefront-controller.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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

Expand Down

0 comments on commit 62874dd

Please sign in to comment.