Skip to content

Commit 1c21c2c

Browse files
committed
[Autocomplete] Apply PHP CS Fixer fixes
1 parent db61002 commit 1c21c2c

19 files changed

+196
-190
lines changed

src/Autocomplete/src/Checksum/ChecksumCalculator.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/Autocomplete/src/Controller/EntityAutocompleteController.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
use Symfony\Component\HttpFoundation\JsonResponse;
1515
use Symfony\Component\HttpFoundation\Request;
1616
use Symfony\Component\HttpFoundation\Response;
17+
use Symfony\Component\HttpFoundation\UriSigner;
1718
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1819
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1920
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
2021
use Symfony\UX\Autocomplete\AutocompleteResultsExecutor;
2122
use Symfony\UX\Autocomplete\AutocompleterRegistry;
22-
use Symfony\UX\Autocomplete\Checksum\ChecksumCalculator;
23-
use Symfony\UX\Autocomplete\Form\AutocompleteChoiceTypeExtension;
2423
use Symfony\UX\Autocomplete\OptionsAwareEntityAutocompleterInterface;
2524

2625
/**
@@ -34,7 +33,7 @@ public function __construct(
3433
private AutocompleterRegistry $autocompleteFieldRegistry,
3534
private AutocompleteResultsExecutor $autocompleteResultsExecutor,
3635
private UrlGeneratorInterface $urlGenerator,
37-
private ChecksumCalculator $checksumCalculator,
36+
private UriSigner $uriSigner,
3837
) {
3938
}
4039

@@ -76,15 +75,13 @@ private function getExtraOptions(Request $request): array
7675
return [];
7776
}
7877

79-
$extraOptions = $this->getDecodedExtraOptions($request->query->getString(self::EXTRA_OPTIONS));
78+
ray($request, $this->uriSigner->checkRequest($request));
8079

81-
if (!\array_key_exists(AutocompleteChoiceTypeExtension::CHECKSUM_KEY, $extraOptions)) {
82-
throw new BadRequestHttpException('The extra options are missing the checksum.');
80+
if (!$this->uriSigner->checkRequest($request)) {
81+
throw new BadRequestHttpException('The URI has been tampered with.');
8382
}
8483

85-
$this->validateChecksum($extraOptions[AutocompleteChoiceTypeExtension::CHECKSUM_KEY], $extraOptions);
86-
87-
return $extraOptions;
84+
return $this->getDecodedExtraOptions($request->query->getString(self::EXTRA_OPTIONS));
8885
}
8986

9087
/**
@@ -94,20 +91,4 @@ private function getDecodedExtraOptions(string $extraOptions): array
9491
{
9592
return json_decode(base64_decode($extraOptions), true, flags: \JSON_THROW_ON_ERROR);
9693
}
97-
98-
/**
99-
* @param array<string, scalar> $extraOptions
100-
*/
101-
private function validateChecksum(string $checksum, array $extraOptions): void
102-
{
103-
$extraOptionsWithoutChecksum = array_filter(
104-
$extraOptions,
105-
fn (string $key) => AutocompleteChoiceTypeExtension::CHECKSUM_KEY !== $key,
106-
\ARRAY_FILTER_USE_KEY,
107-
);
108-
109-
if ($checksum !== $this->checksumCalculator->calculateForArray($extraOptionsWithoutChecksum)) {
110-
throw new BadRequestHttpException('The extra options have been tampered with.');
111-
}
112-
}
11394
}

src/Autocomplete/src/DependencyInjection/AutocompleteExtension.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2222
use Symfony\UX\Autocomplete\AutocompleteResultsExecutor;
2323
use Symfony\UX\Autocomplete\AutocompleterRegistry;
24-
use Symfony\UX\Autocomplete\Checksum\ChecksumCalculator;
2524
use Symfony\UX\Autocomplete\Controller\EntityAutocompleteController;
2625
use Symfony\UX\Autocomplete\Doctrine\DoctrineRegistryWrapper;
2726
use Symfony\UX\Autocomplete\Doctrine\EntityMetadataFactory;
@@ -117,7 +116,7 @@ private function registerBasicServices(ContainerBuilder $container): void
117116
new Reference('ux.autocomplete.autocompleter_registry'),
118117
new Reference('ux.autocomplete.results_executor'),
119118
new Reference('router'),
120-
new Reference('ux.autocomplete.checksum_calculator'),
119+
new Reference('uri_signer'),
121120
])
122121
->addTag('controller.service_arguments')
123122
;
@@ -129,13 +128,6 @@ private function registerBasicServices(ContainerBuilder $container): void
129128
])
130129
->addTag('maker.command')
131130
;
132-
133-
$container
134-
->register('ux.autocomplete.checksum_calculator', ChecksumCalculator::class)
135-
->setArguments([
136-
'%kernel.secret%',
137-
])
138-
;
139131
}
140132

141133
private function registerFormServices(ContainerBuilder $container): void
@@ -158,7 +150,7 @@ private function registerFormServices(ContainerBuilder $container): void
158150
$container
159151
->register('ux.autocomplete.choice_type_extension', AutocompleteChoiceTypeExtension::class)
160152
->setArguments([
161-
new Reference('ux.autocomplete.checksum_calculator'),
153+
new Reference('uri_signer'),
162154
new Reference('translator', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
163155
])
164156
->addTag('form.type_extension');

src/Autocomplete/src/Form/AutocompleteChoiceTypeExtension.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use Symfony\Component\Form\Extension\Core\Type\TextType;
1717
use Symfony\Component\Form\FormInterface;
1818
use Symfony\Component\Form\FormView;
19+
use Symfony\Component\HttpFoundation\UriSigner;
1920
use Symfony\Component\OptionsResolver\Options;
2021
use Symfony\Component\OptionsResolver\OptionsResolver;
2122
use Symfony\Contracts\Translation\TranslatorInterface;
22-
use Symfony\UX\Autocomplete\Checksum\ChecksumCalculator;
2323

2424
/**
2525
* Initializes the autocomplete Stimulus controller for any fields with the "autocomplete" option.
@@ -28,10 +28,8 @@
2828
*/
2929
final class AutocompleteChoiceTypeExtension extends AbstractTypeExtension
3030
{
31-
public const CHECKSUM_KEY = '@checksum';
32-
3331
public function __construct(
34-
private readonly ChecksumCalculator $checksumCalculator,
32+
private readonly UriSigner $uriSigner,
3533
private readonly ?TranslatorInterface $translator = null,
3634
) {
3735
}
@@ -86,6 +84,7 @@ public function finishView(FormView $view, FormInterface $form, array $options):
8684

8785
if ($options['extra_options']) {
8886
$values['url'] = $this->getUrlWithExtraOptions($values['url'], $options['extra_options']);
87+
$values['url'] = $this->uriSigner->sign($values['url']);
8988
}
9089

9190
$values['loading-more-text'] = $this->trans($options['loading_more_text']);
@@ -105,7 +104,6 @@ private function getUrlWithExtraOptions(string $url, array $extraOptions): strin
105104
{
106105
$this->validateExtraOptions($extraOptions);
107106

108-
$extraOptions[self::CHECKSUM_KEY] = $this->checksumCalculator->calculateForArray($extraOptions);
109107
$extraOptions = base64_encode(json_encode($extraOptions));
110108

111109
return sprintf(

src/Autocomplete/tests/Fixtures/Autocompleter/CustomGroupByProductAutocompleter.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22

3-
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter;
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
411

5-
use Doctrine\ORM\EntityRepository;
6-
use Doctrine\ORM\QueryBuilder;
7-
use Symfony\Component\HttpFoundation\RequestStack;
8-
use Symfony\Bundle\SecurityBundle\Security;
9-
use Symfony\UX\Autocomplete\Doctrine\EntitySearchUtil;
10-
use Symfony\UX\Autocomplete\EntityAutocompleterInterface;
11-
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Product;
12+
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter;
1213

1314
class CustomGroupByProductAutocompleter extends CustomProductAutocompleter
1415
{

src/Autocomplete/tests/Fixtures/Autocompleter/CustomProductAutocompleter.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Autocompleter;
413

514
use Doctrine\ORM\EntityRepository;
@@ -14,9 +23,8 @@ class CustomProductAutocompleter implements EntityAutocompleterInterface
1423
{
1524
public function __construct(
1625
private RequestStack $requestStack,
17-
private EntitySearchUtil $entitySearchUtil
18-
)
19-
{
26+
private EntitySearchUtil $entitySearchUtil,
27+
) {
2028
}
2129

2230
public function getEntityClass(): string

src/Autocomplete/tests/Fixtures/Entity/Category.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Entity;
413

514
use Doctrine\Common\Collections\ArrayCollection;
615
use Doctrine\Common\Collections\Collection;
7-
use Doctrine\DBAL\Types\Types;
816
use Doctrine\ORM\Mapping as ORM;
917

1018
#[ORM\Entity()]

src/Autocomplete/tests/Fixtures/Entity/Ingredient.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Entity;
413

514
use Doctrine\ORM\Mapping as ORM;

src/Autocomplete/tests/Fixtures/Entity/Product.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Entity;
413

514
use Doctrine\Common\Collections\ArrayCollection;

src/Autocomplete/tests/Fixtures/Factory/CategoryFactory.php

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Symfony\UX\Autocomplete\Tests\Fixtures\Factory;
413

514
use Doctrine\ORM\EntityRepository;
615
use Symfony\UX\Autocomplete\Tests\Fixtures\Entity\Category;
7-
use Zenstruck\Foundry\RepositoryProxy;
816
use Zenstruck\Foundry\ModelFactory;
917
use Zenstruck\Foundry\Proxy;
18+
use Zenstruck\Foundry\RepositoryProxy;
1019

1120
/**
1221
* @extends ModelFactory<Category>
1322
*
14-
* @method static Category|Proxy createOne(array $attributes = [])
15-
* @method static Category[]|Proxy[] createMany(int $number, array|callable $attributes = [])
16-
* @method static Category|Proxy find(object|array|mixed $criteria)
17-
* @method static Category|Proxy findOrCreate(array $attributes)
18-
* @method static Category|Proxy first(string $sortedField = 'id')
19-
* @method static Category|Proxy last(string $sortedField = 'id')
20-
* @method static Category|Proxy random(array $attributes = [])
21-
* @method static Category|Proxy randomOrCreate(array $attributes = []))
22-
* @method static Category[]|Proxy[] all()
23-
* @method static Category[]|Proxy[] findBy(array $attributes)
24-
* @method static Category[]|Proxy[] randomSet(int $number, array $attributes = []))
25-
* @method static Category[]|Proxy[] randomRange(int $min, int $max, array $attributes = []))
23+
* @method static Category|Proxy createOne(array $attributes = [])
24+
* @method static Category[]|Proxy[] createMany(int $number, array|callable $attributes = [])
25+
* @method static Category|Proxy find(object|array|mixed $criteria)
26+
* @method static Category|Proxy findOrCreate(array $attributes)
27+
* @method static Category|Proxy first(string $sortedField = 'id')
28+
* @method static Category|Proxy last(string $sortedField = 'id')
29+
* @method static Category|Proxy random(array $attributes = [])
30+
* @method static Category|Proxy randomOrCreate(array $attributes = []))
31+
* @method static Category[]|Proxy[] all()
32+
* @method static Category[]|Proxy[] findBy(array $attributes)
33+
* @method static Category[]|Proxy[] randomSet(int $number, array $attributes = []))
34+
* @method static Category[]|Proxy[] randomRange(int $min, int $max, array $attributes = []))
2635
* @method static EntityRepository|RepositoryProxy repository()
27-
* @method Category|Proxy create(array|callable $attributes = [])
36+
* @method Category|Proxy create(array|callable $attributes = [])
2837
*/
2938
final class CategoryFactory extends ModelFactory
3039
{

0 commit comments

Comments
 (0)