[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
*/
public function __construct(
private readonly CustomerRepositoryInterface $customerRepository,
private readonly CustomerStatisticsProviderInterface $statisticsProvider,
protected readonly CustomerRepositoryInterface $customerRepository,
protected readonly CustomerStatisticsProviderInterface $statisticsProvider,
) {
}

View file

@ -41,7 +41,7 @@ class ChannelSelectorComponent
/**
* @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
*/
public function __construct(
private readonly CustomerRepositoryInterface $customerRepository,
protected readonly CustomerRepositoryInterface $customerRepository,
) {
}

View file

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

View file

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

View file

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

View file

@ -19,7 +19,7 @@ use Symfony\UX\TwigComponent\Attribute\AsTwigComponent;
#[AsTwigComponent]
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,
string $resourceClass,
string $formClass,
private readonly SlugGeneratorInterface $slugGenerator,
private readonly RepositoryInterface $productAttributeRepository,
private readonly ProductFactoryInterface $productFactory,
protected readonly SlugGeneratorInterface $slugGenerator,
protected readonly RepositoryInterface $productAttributeRepository,
protected readonly ProductFactoryInterface $productFactory,
) {
$this->initialize($productRepository, $formFactory, $resourceClass, $formClass);
}

View file

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

View file

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

View file

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

View file

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

View file

@ -20,7 +20,7 @@ use Symfony\UX\TwigComponent\Attribute\ExposeInTemplate;
class ShopPreviewComponent
{
/** @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
{
public function __construct(
private readonly UrlGeneratorInterface $urlGenerator,
private readonly LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
protected readonly UrlGeneratorInterface $urlGenerator,
protected readonly LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
) {
}

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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