Skip to content

Commit

Permalink
feature Sylius#13339 Add Support for Symfony 5.4 (AdamKasp)
Browse files Browse the repository at this point in the history
This PR was merged into the 1.11-dev branch.

Discussion
----------

| Q               | A
| --------------- | -----
| Branch?         |  master
| Bug fix?        | no
| New feature?    |yes
| BC breaks?      | no
| Deprecations?   | no
| License         | MIT

<!--
 - Bug fixes must be submitted against the 1.10 branch
 - Features and deprecations must be submitted against the master branch
 - Make sure that the correct base branch is set

 To be sure you are not breaking any Backward Compatibilities, check the documentation:
 https://docs.sylius.com/en/latest/book/organization/backward-compatibility-promise.html
-->

Commits
-------

28a02a6 Prepare Sylius for support Symfony 5.4
d2c39e4 remove usage of symfony internal method
fef6243 bump symofny verion from 5.4.0-RC1 to 5.4
6339528 update argument in UsernamePasswordToken
2964e0e make workorounds over deprecated method in symofny 5.4
  • Loading branch information
GSadee authored and lchrusciel committed Dec 6, 2021
1 parent 860c3e8 commit 71e4fd6
Show file tree
Hide file tree
Showing 18 changed files with 196 additions and 23 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
fail-fast: false
matrix:
php: ["7.4", "8.0"]
symfony: ["^4.4", "5.2.*", "5.3.*"]
symfony: ["^4.4", "5.2.*", "5.3.*", "5.4.*"]

steps:
-
Expand Down Expand Up @@ -120,7 +120,7 @@ jobs:
fail-fast: false
matrix:
php: ["7.4", "8.0"]
symfony: ["^4.4", "5.2.*", "5.3.*"]
symfony: ["^4.4", "5.2.*", "5.3.*", "5.4.*"]
node: ["14.x"]
mysql: ["5.7", "8.0"]

Expand Down Expand Up @@ -249,7 +249,7 @@ jobs:
fail-fast: false
matrix:
php: ["7.4", "8.0"]
symfony: ["^4.4", "5.2.*", "5.3.*"]
symfony: ["^4.4", "5.2.*", "5.3.*", "5.4.*"]
node: ["14.x"]
mysql: ["5.7", "8.0"]

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
fail-fast: false
matrix:
php: ["7.4", "8.0"]
symfony: ["^4.4", "5.2.*"]
symfony: ["^4.4", "5.2.*", "5.3.*", "5.4.*"]
package: "${{ fromJson(needs.list.outputs.packages) }}"

steps:
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<referencedMethod name="Symfony\Component\Security\Core\User\UserInterface::getUsername" /> <!-- deprecated in Symfony 5.3 -->
<referencedMethod name="Symfony\Component\Security\Core\User\UserProviderInterface::loadUserByUsername" /> <!-- deprecated in Symfony 5.3 -->
<referencedMethod name="Faker\Generator::__get"/>
<referencedMethod name="Symfony\Component\Security\Core\Authentication\Token\AbstractToken::isAuthenticated" /> <!-- deprecated in Symfony 5.4 -->
</errorLevel>
</DeprecatedMethod>

Expand Down Expand Up @@ -202,6 +203,7 @@
<referencedFunction name="Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch" />
<referencedFunction name="Symfony\Component\HttpKernel\Config\FileLocator::__construct" />
<referencedFunction name="Symfony\Contracts\EventDispatcher\EventDispatcherInterface::dispatch" />
<referencedFunction name="Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct" /> <!-- removed parameter in Symfony 5.4 -->
</errorLevel>
<errorLevel type="suppress">
<referencedFunction name="Doctrine\ORM\Query\Expr::andX" />
Expand All @@ -225,6 +227,7 @@
<errorLevel type="info">
<referencedFunction name="Symfony\Component\EventDispatcher\EventDispatcherInterface::dispatch" />
<referencedFunction name="SyliusLabs\AssociationHydrator\AssociationHydrator::__construct" />
<referencedFunction name="Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken::__construct" />
</errorLevel>
</InvalidArgument>

Expand Down
8 changes: 7 additions & 1 deletion src/Sylius/Behat/Service/SecurityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ public function __construct(SessionInterface $session, CookieSetterInterface $co

public function logIn(UserInterface $user): void
{
$token = new UsernamePasswordToken($user, $user->getPassword(), $this->firewallContextName, $user->getRoles());
/** @deprecated parameter credential was deprecated in Symfony 5.4, so in Sylius 1.11 too, in Sylius 2.0 providing 4 arguments will be prohibited. */
if (3 === (new \ReflectionClass(UsernamePasswordToken::class))->getConstructor()->getNumberOfParameters()) {
$token = new UsernamePasswordToken($user, $this->firewallContextName, $user->getRoles());
} else {
$token = new UsernamePasswordToken($user, $user->getPassword(), $this->firewallContextName, $user->getRoles());
}

$this->setToken($token);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public function __construct(
public function __invoke(Request $request): JsonResponse
{
/** @var PaymentInterface|null $payment */
$payment = $this->paymentRepository->findOneByOrderToken($request->get('paymentId'), $request->get('id'));
$payment = $this->paymentRepository->findOneByOrderToken(
$request->attributes->get('paymentId'),
$request->attributes->get('id')
);

Assert::notNull($payment);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __invoke(Request $request): ImageInterface
$image->setFile($file);

/** @var string $ownerIri */
$ownerIri = $request->get('owner');
$ownerIri = $request->request->get('owner');
Assert::notEmpty($ownerIri);

/** @var ResourceInterface|AdminUserInterface $owner */
Expand Down
4 changes: 2 additions & 2 deletions src/Sylius/Bundle/CoreBundle/Command/SetupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function ($value): string {
/** @var ConstraintViolationListInterface $errors */
$errors = $this->getContainer()->get('validator')->validate((string) $value, [new Email(), new NotBlank()]);
foreach ($errors as $error) {
throw new \DomainException($error->getMessage());
throw new \DomainException((string) $error->getMessage());
}

return $value;
Expand Down Expand Up @@ -191,7 +191,7 @@ function ($value): string {
/** @var ConstraintViolationListInterface $errors */
$errors = $this->getContainer()->get('validator')->validate($value, [new NotBlank()]);
foreach ($errors as $error) {
throw new \DomainException($error->getMessage());
throw new \DomainException((string) $error->getMessage());
}

return $value;
Expand Down
33 changes: 30 additions & 3 deletions src/Sylius/Bundle/CoreBundle/Controller/ProductTaxonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ class ProductTaxonController extends ResourceController
* @throws HttpException
*
* @deprecated This ajax action is deprecated and will be removed in Sylius 2.0 - use ProductTaxonController::updateProductTaxonsPositionsAction instead.
*
* @psalm-suppress DeprecatedMethod
*/
public function updatePositionsAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
$productTaxons = $request->get('productTaxons');

$productTaxons = $this->getParameterFromRequest($request,'productTaxons');
$this->validateCsrfProtection($request, $configuration);

if ($this->shouldProductsPositionsBeUpdated($request, $productTaxons)) {
Expand All @@ -55,11 +56,14 @@ public function updatePositionsAction(Request $request): Response
return new JsonResponse();
}

/**
* @psalm-suppress DeprecatedMethod
*/
public function updateProductTaxonsPositionsAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
$productTaxons = $request->get('productTaxons');
$productTaxons = $this->getParameterFromRequest($request,'productTaxons');

$this->validateCsrfProtection($request, $configuration);

Expand Down Expand Up @@ -103,4 +107,27 @@ private function updatePositions(string $position, int $id): void
$productTaxonFromBase = $this->repository->findOneBy(['id' => $id]);
$productTaxonFromBase->setPosition((int) $position);
}

/**
* @return mixed
*
* @deprecated This function will be removed in Sylius 2.0, since Symfony 5.4, use explicit input sources instead
* based on Symfony\Component\HttpFoundation\Request::get
*/
private function getParameterFromRequest(Request $request, string $key)
{
if ($request !== $result = $request->attributes->get($key, $request)) {
return $result;
}

if ($request->query->has($key)) {
return $request->query->all()[$key];
}

if ($request->request->has($key)) {
return $request->request->all()[$key];
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ class ProductVariantController extends ResourceController
{
/**
* @throws HttpException
* @psalm-suppress DeprecatedMethod
*/
public function updatePositionsAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);
$this->isGrantedOr403($configuration, ResourceActions::UPDATE);
$productVariantsToUpdate = $request->get('productVariants');
$productVariantsToUpdate = $this->getParameterFromRequest($request, 'productVariants');

if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid('update-product-variant-position', (string) $request->request->get('_csrf_token'))) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
Expand All @@ -54,4 +55,27 @@ public function updatePositionsAction(Request $request): Response

return new JsonResponse();
}

/**
* @return mixed
*
* @deprecated This function will be removed in Sylius 2.0, since Symfony 5.4, use explicit input sources instead
* based on Symfony\Component\HttpFoundation\Request::get
*/
private function getParameterFromRequest(Request $request, string $key)
{
if ($request !== $result = $request->attributes->get($key, $request)) {
return $result;
}

if ($request->query->has($key)) {
return $request->query->all()[$key];
}

if ($request->request->has($key)) {
return $request->request->all()[$key];
}

return null;
}
}
2 changes: 2 additions & 0 deletions src/Sylius/Bundle/CoreBundle/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,7 @@
<service class="Sylius\Bundle\CoreBundle\Mailer\OrderEmailManager" id="sylius.mailer.order_email_manager">
<argument type="service" id="sylius.email_sender" />
</service>

<service id="security.authentication_manager" alias="security.authentication.manager" />
</services>
</container>
22 changes: 16 additions & 6 deletions src/Sylius/Bundle/CoreBundle/Security/UserImpersonator.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,22 @@ public function __construct(SessionInterface $session, string $firewallContextNa

public function impersonate(UserInterface $user): void
{
$token = new UsernamePasswordToken(
$user,
$user->getPassword(),
$this->firewallContextName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
/** @deprecated parameter credential was deprecated in Symfony 5.4, so in Sylius 1.11 too, in Sylius 2.0 providing 4 arguments will be prohibited. */
if (3 === (new \ReflectionClass(UsernamePasswordToken::class))->getConstructor()->getNumberOfParameters()) {
$token = new UsernamePasswordToken(
$user,
$this->firewallContextName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
} else {
$token = new UsernamePasswordToken(
$user,
$user->getPassword(),
$this->firewallContextName,
array_map(/** @param object|string $role */ static function ($role): string { return (string) $role; }, $user->getRoles())
);
}

$this->session->set($this->sessionTokenParameter, serialize($token));
$this->session->save();

Expand Down
28 changes: 27 additions & 1 deletion src/Sylius/Bundle/OrderBundle/Controller/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,17 @@ public function saveAction(Request $request): Response
);
}

/**
* @psalm-suppress DeprecatedMethod
*/
public function clearAction(Request $request): Response
{
$configuration = $this->requestConfigurationFactory->create($this->metadata, $request);

$this->isGrantedOr403($configuration, ResourceActions::DELETE);
$resource = $this->getCurrentCart();

if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid((string) $resource->getId(), $request->get('_csrf_token'))) {
if ($configuration->isCsrfProtectionEnabled() && !$this->isCsrfTokenValid((string) $resource->getId(), $this->getParameterFromRequest($request, '_csrf_token'))) {
throw new HttpException(Response::HTTP_FORBIDDEN, 'Invalid csrf token.');
}

Expand Down Expand Up @@ -194,4 +197,27 @@ protected function getEventDispatcher(): EventDispatcherInterface
{
return $this->container->get('event_dispatcher');
}

/**
* @return mixed
*
* @deprecated This function will be removed in Sylius 2.0, since Symfony 5.4, use explicit input sources instead
* based on Symfony\Component\HttpFoundation\Request::get
*/
private function getParameterFromRequest(Request $request, string $key)
{
if ($request !== $result = $request->attributes->get($key, $request)) {
return $result;
}

if ($request->query->has($key)) {
return $request->query->all()[$key];
}

if ($request->request->has($key)) {
return $request->request->all()[$key];
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,38 @@

namespace Sylius\Bundle\ProductBundle\Controller;

use Sylius\Component\Product\Generator\SlugGeneratorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ProductSlugController extends AbstractController
{
private ?SlugGeneratorInterface $slugGenerator;

public function __construct(?SlugGeneratorInterface $slugGenerator = null)
{
$this->slugGenerator = $slugGenerator;

if ($this->slugGenerator === null) {
@trigger_error(sprintf('Not passing a $slugGenerator to %s constructor is deprecated since Sylius 1.11 and will be prohibited in Sylius 2.0.', self::class), \E_USER_DEPRECATED);
}
}

/**
* @psalm-suppress DeprecatedMethod
*/
public function generateAction(Request $request): Response
{
$name = $request->query->get('name');

if ($this->slugGenerator !== null) {
return new JsonResponse([
'slug' => $this->slugGenerator->generate((string) $name),
]);
}

return new JsonResponse([
'slug' => $this->get('sylius.generator.slug')->generate($name),
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<defaults public="true" />

<service id="sylius.controller.product_slug" class="Sylius\Bundle\ProductBundle\Controller\ProductSlugController">
<argument type="service" id="sylius.generator.slug" />
<call method="setContainer">
<argument type="service" id="service_container" />
</call>
Expand Down
Loading

0 comments on commit 71e4fd6

Please sign in to comment.