[Twig][Component] Replace private with protected constructor parameters (#17486)

| Q               | A
|-----------------|-----
| Branch?         | 2.0
| Bug fix?        | yes
| New feature?    | no
| BC breaks?      | no
| Deprecations?   | no
| Related tickets | n/a
| License         | MIT
This commit is contained in:
Michał Pysiak 2024-11-20 13:19:45 +01:00 committed by GitHub
commit 81502de90d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
36 changed files with 97 additions and 87 deletions

View file

@ -38,8 +38,8 @@ class OrderStatisticsComponent
* @param CustomerRepositoryInterface<CustomerInterface> $customerRepository * @param CustomerRepositoryInterface<CustomerInterface> $customerRepository
*/ */
public function __construct( public function __construct(
private readonly CustomerRepositoryInterface $customerRepository, protected readonly CustomerRepositoryInterface $customerRepository,
private readonly CustomerStatisticsProviderInterface $statisticsProvider, protected readonly CustomerStatisticsProviderInterface $statisticsProvider,
) { ) {
} }

View file

@ -41,7 +41,7 @@ class ChannelSelectorComponent
/** /**
* @param ChannelRepositoryInterface<ChannelInterface> $channelRepository * @param ChannelRepositoryInterface<ChannelInterface> $channelRepository
*/ */
public function __construct(private readonly ChannelRepositoryInterface $channelRepository) public function __construct(protected readonly ChannelRepositoryInterface $channelRepository)
{ {
} }

View file

@ -27,7 +27,7 @@ class NewCustomersComponent
* @param CustomerRepositoryInterface<CustomerInterface> $customerRepository * @param CustomerRepositoryInterface<CustomerInterface> $customerRepository
*/ */
public function __construct( public function __construct(
private readonly CustomerRepositoryInterface $customerRepository, protected readonly CustomerRepositoryInterface $customerRepository,
) { ) {
} }

View file

@ -43,8 +43,8 @@ class NewOrdersComponent
* @param ChannelRepositoryInterface<ChannelInterface> $channelRepository * @param ChannelRepositoryInterface<ChannelInterface> $channelRepository
*/ */
public function __construct( public function __construct(
private readonly OrderRepositoryInterface $orderRepository, protected readonly OrderRepositoryInterface $orderRepository,
private readonly ChannelRepositoryInterface $channelRepository, protected readonly ChannelRepositoryInterface $channelRepository,
) { ) {
} }

View file

@ -55,8 +55,8 @@ class StatisticsComponent
* @param ChannelRepositoryInterface<ChannelInterface> $channelRepository * @param ChannelRepositoryInterface<ChannelInterface> $channelRepository
*/ */
public function __construct( public function __construct(
private readonly ChannelRepositoryInterface $channelRepository, protected readonly ChannelRepositoryInterface $channelRepository,
private readonly StatisticsProviderInterface $statisticsProvider, protected readonly StatisticsProviderInterface $statisticsProvider,
) { ) {
} }

View file

@ -27,7 +27,7 @@ class AddressHistoryComponent
/** @param RepositoryInterface<AddressLogEntry> $addressLogRepository */ /** @param RepositoryInterface<AddressLogEntry> $addressLogRepository */
public function __construct( public function __construct(
private readonly RepositoryInterface $addressLogRepository, protected readonly RepositoryInterface $addressLogRepository,
) { ) {
} }

View file

@ -19,7 +19,7 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
#[AsTwigComponent] #[AsTwigComponent]
class ProductTaxonsComponent class ProductTaxonsComponent
{ {
public function __construct(private readonly AllTaxonsInterface $allTaxons) public function __construct(protected readonly AllTaxonsInterface $allTaxons)
{ {
} }

View file

@ -65,9 +65,9 @@ class FormComponent
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
string $resourceClass, string $resourceClass,
string $formClass, string $formClass,
private readonly SlugGeneratorInterface $slugGenerator, protected readonly SlugGeneratorInterface $slugGenerator,
private readonly RepositoryInterface $productAttributeRepository, protected readonly RepositoryInterface $productAttributeRepository,
private readonly ProductFactoryInterface $productFactory, protected readonly ProductFactoryInterface $productFactory,
) { ) {
$this->initialize($productRepository, $formFactory, $resourceClass, $formClass); $this->initialize($productRepository, $formFactory, $resourceClass, $formClass);
} }

View file

@ -36,7 +36,7 @@ class ProductAttributeAutocompleteComponent
use TemplatePropTrait; use TemplatePropTrait;
public function __construct( public function __construct(
private readonly ChecksumCalculator $checksumCalculator, protected readonly ChecksumCalculator $checksumCalculator,
) { ) {
} }

View file

@ -46,8 +46,8 @@ class FormComponent
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
string $resourceClass, string $resourceClass,
string $formClass, string $formClass,
private readonly ProductVariantFactoryInterface $productVariantFactory, protected readonly ProductVariantFactoryInterface $productVariantFactory,
private readonly ProductRepositoryInterface $productRepository, protected readonly ProductRepositoryInterface $productRepository,
) { ) {
$this->initialize($productVariantRepository, $formFactory, $resourceClass, $formClass); $this->initialize($productVariantRepository, $formFactory, $resourceClass, $formClass);
} }

View file

@ -30,8 +30,8 @@ class GeneratorInstructionFormComponent
use TemplatePropTrait; use TemplatePropTrait;
public function __construct( public function __construct(
private readonly FormFactoryInterface $formFactory, protected readonly FormFactoryInterface $formFactory,
private readonly string $formClass, protected readonly string $formClass,
) { ) {
} }

View file

@ -19,8 +19,8 @@ use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
class NotificationsComponent class NotificationsComponent
{ {
public function __construct( public function __construct(
private readonly NotificationProviderInterface $notificationProvider, protected readonly NotificationProviderInterface $notificationProvider,
private readonly bool $areNotificationsEnabled, protected readonly bool $areNotificationsEnabled,
) { ) {
} }

View file

@ -20,7 +20,7 @@ use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
class ShopPreviewComponent class ShopPreviewComponent
{ {
/** @param ChannelRepositoryInterface<ChannelInterface> $channelRepository */ /** @param ChannelRepositoryInterface<ChannelInterface> $channelRepository */
public function __construct(private readonly ChannelRepositoryInterface $channelRepository) public function __construct(protected readonly ChannelRepositoryInterface $channelRepository)
{ {
} }

View file

@ -21,8 +21,8 @@ use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
class UserDropdownComponent class UserDropdownComponent
{ {
public function __construct( public function __construct(
private readonly UrlGeneratorInterface $urlGenerator, protected readonly UrlGeneratorInterface $urlGenerator,
private readonly LoggedInAdminUserProviderInterface $loggedInAdminUserProvider, protected readonly LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
) { ) {
} }

View file

@ -19,7 +19,7 @@ use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
class RenderEntityWithTemplateComponent class RenderEntityWithTemplateComponent
{ {
public function __construct( public function __construct(
private EntityManagerInterface $entityManager, protected EntityManagerInterface $entityManager,
) { ) {
} }

View file

@ -37,7 +37,7 @@ final class DeleteComponent
public string $taxonId = ''; public string $taxonId = '';
public function __construct( public function __construct(
private readonly CsrfTokenManagerInterface $csrfTokenManager, protected readonly CsrfTokenManagerInterface $csrfTokenManager,
) { ) {
} }

View file

@ -44,7 +44,7 @@ class FormComponent
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
string $resourceClass, string $resourceClass,
string $formClass, string $formClass,
private readonly TaxonSlugGeneratorInterface $slugGenerator, protected readonly TaxonSlugGeneratorInterface $slugGenerator,
) { ) {
$this->initialize($repository, $formFactory, $resourceClass, $formClass); $this->initialize($repository, $formFactory, $resourceClass, $formClass);
} }

View file

@ -33,9 +33,9 @@ class TreeComponent
/** @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository */ /** @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository */
public function __construct( public function __construct(
private readonly AllTaxonsInterface $allTaxons, protected readonly AllTaxonsInterface $allTaxons,
private readonly TaxonRepositoryInterface $taxonRepository, protected readonly TaxonRepositoryInterface $taxonRepository,
private readonly ObjectManager $taxonManager, protected readonly ObjectManager $taxonManager,
) { ) {
} }

View file

@ -36,8 +36,8 @@ class ChangePasswordFormComponent
/** @param class-string $formClass */ /** @param class-string $formClass */
public function __construct( public function __construct(
private readonly FormFactoryInterface $formFactory, protected readonly FormFactoryInterface $formFactory,
private readonly string $formClass, protected readonly string $formClass,
) { ) {
} }

View file

@ -50,8 +50,8 @@ class FormComponent
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
string $resourceClass, string $resourceClass,
string $formClass, string $formClass,
private readonly ObjectManager $manager, protected readonly ObjectManager $manager,
private readonly EventDispatcherInterface $eventDispatcher, protected readonly EventDispatcherInterface $eventDispatcher,
) { ) {
$this->initialize($orderRepository, $formFactory, $resourceClass, $formClass); $this->initialize($orderRepository, $formFactory, $resourceClass, $formClass);
} }

View file

@ -43,7 +43,7 @@ class WidgetComponent
/** @param OrderRepositoryInterface<OrderInterface> $orderRepository */ /** @param OrderRepositoryInterface<OrderInterface> $orderRepository */
public function __construct( public function __construct(
private readonly CartContextInterface $cartContext, protected readonly CartContextInterface $cartContext,
OrderRepositoryInterface $orderRepository, OrderRepositoryInterface $orderRepository,
) { ) {
$this->initialize($orderRepository); $this->initialize($orderRepository);

View file

@ -41,7 +41,7 @@ class AddressBookComponent
public string $field; public string $field;
public function __construct( public function __construct(
private readonly CustomerContextInterface $customerContext, protected readonly CustomerContextInterface $customerContext,
) { ) {
} }

View file

@ -49,9 +49,9 @@ class FormComponent
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
string $resourceClass, string $resourceClass,
string $formClass, string $formClass,
private readonly CustomerContextInterface $customerContext, protected readonly CustomerContextInterface $customerContext,
private readonly UserRepositoryInterface $shopUserRepository, protected readonly UserRepositoryInterface $shopUserRepository,
private readonly AddressRepositoryInterface $addressRepository, protected readonly AddressRepositoryInterface $addressRepository,
) { ) {
$this->initialize($repository, $formFactory, $resourceClass, $formClass); $this->initialize($repository, $formFactory, $resourceClass, $formClass);
} }

View file

@ -24,8 +24,8 @@ use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
readonly class CurrencySwitcherComponent readonly class CurrencySwitcherComponent
{ {
public function __construct( public function __construct(
private ChannelContextInterface $channelContext, protected ChannelContextInterface $channelContext,
private CurrencyContextInterface $currencyContext, protected CurrencyContextInterface $currencyContext,
) { ) {
} }

View file

@ -22,8 +22,8 @@ use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
readonly class LocaleSwitcherComponent readonly class LocaleSwitcherComponent
{ {
public function __construct( public function __construct(
private LocaleContextInterface $localeContext, protected LocaleContextInterface $localeContext,
private LocaleProviderInterface $localeProvider, protected LocaleProviderInterface $localeProvider,
) { ) {
} }

View file

@ -28,9 +28,9 @@ readonly class TaxonMenuComponent
* @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository * @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository
*/ */
public function __construct( public function __construct(
private TaxonRepositoryInterface $taxonRepository, protected TaxonRepositoryInterface $taxonRepository,
private ChannelContextInterface $channelContext, protected ChannelContextInterface $channelContext,
private LocaleContextInterface $localeContext, protected LocaleContextInterface $localeContext,
) { ) {
} }

View file

@ -15,7 +15,7 @@ namespace Sylius\Bundle\ShopBundle\Twig\Component\Product;
use Doctrine\Persistence\ObjectManager; use Doctrine\Persistence\ObjectManager;
use Sylius\Bundle\CoreBundle\Provider\FlashBagProvider; use Sylius\Bundle\CoreBundle\Provider\FlashBagProvider;
use Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactory; use Sylius\Bundle\OrderBundle\Factory\AddToCartCommandFactoryInterface;
use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductLivePropTrait; use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductLivePropTrait;
use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductVariantLivePropTrait; use Sylius\Bundle\ShopBundle\Twig\Component\Product\Trait\ProductVariantLivePropTrait;
use Sylius\Bundle\UiBundle\Twig\Component\TemplatePropTrait; use Sylius\Bundle\UiBundle\Twig\Component\TemplatePropTrait;
@ -37,6 +37,7 @@ use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Routing\RouterInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent; use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction; use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveArg;
use Symfony\UX\LiveComponent\Attribute\LiveProp; use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\Attribute\PreReRender; use Symfony\UX\LiveComponent\Attribute\PreReRender;
use Symfony\UX\LiveComponent\ComponentToolsTrait; use Symfony\UX\LiveComponent\ComponentToolsTrait;
@ -71,15 +72,15 @@ class AddToCartFormComponent
* @param ProductVariantRepositoryInterface<ProductVariantInterface> $productVariantRepository * @param ProductVariantRepositoryInterface<ProductVariantInterface> $productVariantRepository
*/ */
public function __construct( public function __construct(
private readonly FormFactoryInterface $formFactory, protected readonly FormFactoryInterface $formFactory,
private readonly ObjectManager $manager, protected readonly ObjectManager $manager,
private readonly RouterInterface $router, protected readonly RouterInterface $router,
private readonly RequestStack $requestStack, protected readonly RequestStack $requestStack,
private readonly EventDispatcherInterface $eventDispatcher, protected readonly EventDispatcherInterface $eventDispatcher,
private readonly CartContextInterface $cartContext, protected readonly CartContextInterface $cartContext,
private readonly AddToCartCommandFactory $addToCartCommandFactory, protected readonly AddToCartCommandFactoryInterface $addToCartCommandFactory,
private readonly CartItemFactoryInterface $cartItemFactory, protected readonly CartItemFactoryInterface $cartItemFactory,
private readonly string $formClass, protected readonly string $formClass,
ProductRepositoryInterface $productRepository, ProductRepositoryInterface $productRepository,
ProductVariantRepositoryInterface $productVariantRepository, ProductVariantRepositoryInterface $productVariantRepository,
) { ) {
@ -106,9 +107,14 @@ class AddToCartFormComponent
$this->emitUp(self::SYLIUS_SHOP_VARIANT_CHANGED, ['variantId' => $this->variant?->getId()]); $this->emitUp(self::SYLIUS_SHOP_VARIANT_CHANGED, ['variantId' => $this->variant?->getId()]);
} }
/** @param array<string, mixed> $routeParameters */
#[LiveAction] #[LiveAction]
public function addToCart(): RedirectResponse public function addToCart(
{ #[LiveArg] ?string $routeName = null,
#[LiveArg] array $routeParameters = [],
#[LiveArg] ?string $idRouteParameter = null,
#[LiveArg] bool $addFlashMessage = true,
): RedirectResponse {
$this->submitForm(); $this->submitForm();
$addToCartCommand = $this->getForm()->getData(); $addToCartCommand = $this->getForm()->getData();
@ -116,13 +122,17 @@ class AddToCartFormComponent
$this->manager->persist($addToCartCommand->getCart()); $this->manager->persist($addToCartCommand->getCart());
$this->manager->flush(); $this->manager->flush();
FlashBagProvider if ($addFlashMessage) {
::getFlashBag($this->requestStack) FlashBagProvider::getFlashBag($this->requestStack)->add('success', 'sylius.cart.add_item');
->add('success', 'sylius.cart.add_item'); }
if ($idRouteParameter !== null) {
$routeParameters[$idRouteParameter] = $addToCartCommand->getCart()->getId();
}
return new RedirectResponse($this->router->generate( return new RedirectResponse($this->router->generate(
$this->routeName, $routeName ?? $this->routeName,
$this->routeParameters, array_merge($this->routeParameters, $routeParameters),
)); ));
} }

View file

@ -31,8 +31,8 @@ class AssociationComponent
* @param ProductAssociationRepositoryInterface<ProductAssociationInterface> $productAssociationRepository * @param ProductAssociationRepositoryInterface<ProductAssociationInterface> $productAssociationRepository
*/ */
public function __construct( public function __construct(
private readonly ProductAssociationRepositoryInterface $productAssociationRepository, protected readonly ProductAssociationRepositoryInterface $productAssociationRepository,
private readonly ChannelContextInterface $channelContext, protected readonly ChannelContextInterface $channelContext,
) { ) {
} }

View file

@ -27,9 +27,9 @@ readonly class BreadcrumbComponent
* @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository * @param TaxonRepositoryInterface<TaxonInterface> $taxonRepository
*/ */
public function __construct( public function __construct(
private RequestStack $requestStack, protected RequestStack $requestStack,
private TaxonRepositoryInterface $taxonRepository, protected TaxonRepositoryInterface $taxonRepository,
private LocaleContextInterface $localeContext, protected LocaleContextInterface $localeContext,
) { ) {
} }

View file

@ -30,9 +30,9 @@ class BySlugComponent
* @param ProductRepositoryInterface<ProductInterface> $productRepository * @param ProductRepositoryInterface<ProductInterface> $productRepository
*/ */
public function __construct( public function __construct(
private readonly ProductRepositoryInterface $productRepository, protected readonly ProductRepositoryInterface $productRepository,
private readonly ChannelContextInterface $channelContext, protected readonly ChannelContextInterface $channelContext,
private readonly LocaleContextInterface $localeContext, protected readonly LocaleContextInterface $localeContext,
) { ) {
} }

View file

@ -41,11 +41,11 @@ class CardComponent
* @param ProductRepositoryInterface<ProductInterface> $productRepository * @param ProductRepositoryInterface<ProductInterface> $productRepository
*/ */
public function __construct( public function __construct(
private readonly ProductRepositoryInterface $productRepository, protected readonly ProductRepositoryInterface $productRepository,
private readonly ProductVariantResolverInterface $productVariantResolver, protected readonly ProductVariantResolverInterface $productVariantResolver,
private readonly ChannelContextInterface $channelContext, protected readonly ChannelContextInterface $channelContext,
private readonly LocaleContextInterface $localeContext, protected readonly LocaleContextInterface $localeContext,
private readonly ProductVariantPricesCalculatorInterface $productVariantPricesCalculator, protected readonly ProductVariantPricesCalculatorInterface $productVariantPricesCalculator,
) { ) {
} }

View file

@ -33,9 +33,9 @@ class ListComponent
/** @param ProductRepositoryInterface<ProductInterface> $productRepository */ /** @param ProductRepositoryInterface<ProductInterface> $productRepository */
public function __construct( public function __construct(
private readonly ProductRepositoryInterface $productRepository, protected readonly ProductRepositoryInterface $productRepository,
private readonly LocaleContextInterface $localeContext, protected readonly LocaleContextInterface $localeContext,
private readonly ChannelContextInterface $channelContext, protected readonly ChannelContextInterface $channelContext,
) { ) {
} }

View file

@ -43,12 +43,12 @@ class PriceComponent
public bool $hasDiscount = false; public bool $hasDiscount = false;
public function __construct( public function __construct(
private readonly ProductVariantPricesCalculatorInterface $productVariantPricesCalculator, protected readonly ProductVariantPricesCalculatorInterface $productVariantPricesCalculator,
private readonly MoneyFormatterInterface $moneyFormatter, protected readonly MoneyFormatterInterface $moneyFormatter,
private readonly ChannelContextInterface $channelContext, protected readonly ChannelContextInterface $channelContext,
private readonly LocaleContextInterface $localeContext, protected readonly LocaleContextInterface $localeContext,
private readonly CurrencyContextInterface $currencyContext, protected readonly CurrencyContextInterface $currencyContext,
private readonly CurrencyConverterInterface $currencyConverter, protected readonly CurrencyConverterInterface $currencyConverter,
) { ) {
} }

View file

@ -42,7 +42,7 @@ class SummaryComponent
* @param ProductVariantRepositoryInterface<ProductVariantInterface> $productVariantRepository * @param ProductVariantRepositoryInterface<ProductVariantInterface> $productVariantRepository
*/ */
public function __construct( public function __construct(
private readonly ProductVariantResolverInterface $productVariantResolver, protected readonly ProductVariantResolverInterface $productVariantResolver,
ProductRepositoryInterface $productRepository, ProductRepositoryInterface $productRepository,
ProductVariantRepositoryInterface $productVariantRepository, ProductVariantRepositoryInterface $productVariantRepository,
) { ) {

View file

@ -27,7 +27,7 @@ class CountComponent
/** /**
* @param ProductReviewRepositoryInterface<ProductReview> $productReviewRepository * @param ProductReviewRepositoryInterface<ProductReview> $productReviewRepository
*/ */
public function __construct(private readonly ProductReviewRepositoryInterface $productReviewRepository) public function __construct(protected readonly ProductReviewRepositoryInterface $productReviewRepository)
{ {
} }

View file

@ -32,7 +32,7 @@ class ListComponent
/** /**
* @param ProductReviewRepositoryInterface<ProductReview> $productReviewRepository * @param ProductReviewRepositoryInterface<ProductReview> $productReviewRepository
*/ */
public function __construct(private readonly ProductReviewRepositoryInterface $productReviewRepository) public function __construct(protected readonly ProductReviewRepositoryInterface $productReviewRepository)
{ {
} }