Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 74c15ad

Browse files
committed
Deprecate Pool::getPropertyAccessor
PropertyAccesor was added to Pool in order to reuse it for performance reasons in sonata-project#3488 In symfony/symfony#16838 cache was added to the component, so there is no need to reuse it thought the Pool service and it can be injected into the services.
1 parent 88d488f commit 74c15ad

12 files changed

Lines changed: 316 additions & 50 deletions

File tree

UPGRADE-3.x.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,32 @@
11
UPGRADE 3.x
22
===========
33

4+
UPGRADE FROM 3.xx to 3.xx
5+
=========================
6+
7+
### Sonata\AdminBundle\Admin\Pool
8+
9+
- Passing a `Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance as 4 argument instantiating
10+
`Sonata\AdminBundle\Admin\Pool` is deprecated.
11+
- `Sonata\AdminBundle\Admin\Pool::getPropertyAccessor()` method has been deprecated. You SHOULD inject `Symfony\Component\PropertyAccess\PropertyAccessorInterface`
12+
where is needed.
13+
14+
### Sonata\AdminBundle\Action\SetObjectFieldValueAction
15+
16+
Not passing a `Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance as argument 5 instantiating
17+
`Sonata\AdminBundle\Action\SetObjectFieldValueAction` is deprecated.
18+
19+
### Sonata\AdminBundle\Admin\AdminHelper
20+
21+
Not passing a `Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance as argument 1 instantiating
22+
`Sonata\AdminBundle\Admin\AdminHelper` is deprecated.
23+
24+
### Sonata\AdminBundle\Twig\Extension\SonataAdminExtension
25+
26+
Argument 5 of `Sonata\AdminBundle\Admin\SonataAdminExtension` constructor SHOULD be a
27+
`Symfony\Component\PropertyAccess\PropertyAccessorInterface` instance and argument 6 SHOULD be a
28+
`Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface` instance or "null".
29+
430
UPGRADE FROM 3.80 to 3.81
531
=========================
632

src/Action/SetObjectFieldValueAction.php

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Symfony\Component\HttpFoundation\Request;
2424
use Symfony\Component\HttpFoundation\Response;
2525
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2627
use Symfony\Component\PropertyAccess\PropertyPath;
2728
use Symfony\Component\Validator\Validator\ValidatorInterface;
2829
use Twig\Environment;
@@ -50,11 +51,23 @@ final class SetObjectFieldValueAction
5051
private $resolver;
5152

5253
/**
54+
* @var PropertyAccessorInterface
55+
*/
56+
private $propertyAccessor;
57+
58+
/**
59+
* NEXT_MAJOR: Make all arguments mandatory.
60+
*
5361
* @param ValidatorInterface $validator
5462
* @param DataTransformerResolver|null $resolver
5563
*/
56-
public function __construct(Environment $twig, Pool $pool, $validator, $resolver = null)
57-
{
64+
public function __construct(
65+
Environment $twig,
66+
Pool $pool,
67+
$validator,
68+
$resolver = null,
69+
?PropertyAccessorInterface $propertyAccessor = null
70+
) {
5871
// NEXT_MAJOR: Move ValidatorInterface check to method signature
5972
if (!($validator instanceof ValidatorInterface)) {
6073
throw new \InvalidArgumentException(sprintf(
@@ -75,10 +88,23 @@ public function __construct(Environment $twig, Pool $pool, $validator, $resolver
7588
$resolver = new DataTransformerResolver();
7689
}
7790

91+
// NEXT_MAJOR: Remove this check.
92+
if (null === $propertyAccessor) {
93+
@trigger_error(sprintf(
94+
'Omitting the argument 5 for "%s()" or passing "null" is deprecated since sonata-project/admin-bundle'
95+
.' 3.x and will throw a \TypeError error in version 4.0. You must pass an instance of %s instead.',
96+
__METHOD__,
97+
PropertyAccessorInterface::class
98+
), E_USER_DEPRECATED);
99+
100+
$propertyAccessor = $pool->getPropertyAccessor();
101+
}
102+
78103
$this->pool = $pool;
79104
$this->twig = $twig;
80105
$this->validator = $validator;
81106
$this->resolver = $resolver;
107+
$this->propertyAccessor = $propertyAccessor;
82108
}
83109

84110
/**
@@ -137,15 +163,15 @@ public function __invoke(Request $request): JsonResponse
137163

138164
// If property path has more than 1 element, take the last object in order to validate it
139165
if ($propertyPath->getLength() > 1) {
140-
$object = $this->pool->getPropertyAccessor()->getValue($object, $propertyPath->getParent());
166+
$object = $this->propertyAccessor->getValue($object, $propertyPath->getParent());
141167

142168
$elements = $propertyPath->getElements();
143169
$field = end($elements);
144170
$propertyPath = new PropertyPath($field);
145171
}
146172

147173
if ('' === $value) {
148-
$this->pool->getPropertyAccessor()->setValue($object, $propertyPath, null);
174+
$this->propertyAccessor->setValue($object, $propertyPath, null);
149175
} else {
150176
$dataTransformer = $this->resolver->resolve($fieldDescription, $admin->getModelManager());
151177

@@ -161,7 +187,7 @@ public function __invoke(Request $request): JsonResponse
161187
), Response::HTTP_NOT_FOUND);
162188
}
163189

164-
$this->pool->getPropertyAccessor()->setValue($object, $propertyPath, $value);
190+
$this->propertyAccessor->setValue($object, $propertyPath, $value);
165191
}
166192

167193
$violations = $this->validator->validate($object);

src/Admin/AbstractAdmin.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use Symfony\Component\HttpFoundation\InputBag;
5151
use Symfony\Component\HttpFoundation\ParameterBag;
5252
use Symfony\Component\HttpFoundation\Request;
53+
use Symfony\Component\PropertyAccess\PropertyAccess;
5354
use Symfony\Component\PropertyAccess\PropertyPath;
5455
use Symfony\Component\Routing\Generator\UrlGeneratorInterface as RoutingUrlGeneratorInterface;
5556
use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
@@ -3722,7 +3723,7 @@ final protected function appendParentObject(object $object): void
37223723
$parentObject = $parentAdmin->getObject($this->request->get($parentAdmin->getIdParameter()));
37233724

37243725
if (null !== $parentObject) {
3725-
$propertyAccessor = $this->getConfigurationPool()->getPropertyAccessor();
3726+
$propertyAccessor = PropertyAccess::createPropertyAccessor();
37263727
$propertyPath = new PropertyPath($this->getParentAssociationMapping());
37273728

37283729
$value = $propertyAccessor->getValue($object, $propertyPath);

src/Admin/AdminHelper.php

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Form\Form;
2626
use Symfony\Component\Form\FormBuilderInterface;
2727
use Symfony\Component\Form\FormView;
28+
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
2829

2930
/**
3031
* @final since sonata-project/admin-bundle 3.52
@@ -39,13 +40,57 @@ class AdminHelper
3940
private const FORM_FIELD_DELETE = '_delete';
4041

4142
/**
42-
* @var Pool
43+
* NEXT_MAJOR: Remove this property.
44+
*
45+
* @var Pool|null
4346
*/
4447
protected $pool;
4548

46-
public function __construct(Pool $pool)
49+
/**
50+
* @var PropertyAccessorInterface
51+
*/
52+
private $propertyAccessor;
53+
54+
/**
55+
* NEXT_MAJOR: Change signature for (PropertyAccessorInterface $propertyAccessor).
56+
*/
57+
public function __construct($poolOrPropertyAccessor)
4758
{
48-
$this->pool = $pool;
59+
// NEXT_MAJOR: Remove this block.
60+
if (!$poolOrPropertyAccessor instanceof Pool && !$poolOrPropertyAccessor instanceof PropertyAccessorInterface) {
61+
throw new \TypeError(sprintf(
62+
'Argument 1 passed to "%s()" must be either an instance of %s or %s, %s given.',
63+
__METHOD__,
64+
Pool::class,
65+
PropertyAccessorInterface::class,
66+
\is_object($poolOrPropertyAccessor) ? 'instance of "'.\get_class($poolOrPropertyAccessor).'"' : '"'.\gettype($poolOrPropertyAccessor).'"'
67+
));
68+
}
69+
70+
// NEXT_MAJOR: Remove this block.
71+
if ($poolOrPropertyAccessor instanceof Pool) {
72+
@trigger_error(sprintf(
73+
'Passing an instance of "%s" as argument 1 for "%s()" is deprecated since'
74+
.' sonata-project/admin-bundle 3.x and will throw a \TypeError error in version 4.0.'
75+
.' You MUST pass an instance of %s instead.',
76+
Pool::class,
77+
__METHOD__,
78+
PropertyAccessorInterface::class
79+
), E_USER_DEPRECATED);
80+
81+
$this->pool = $poolOrPropertyAccessor;
82+
$this->propertyAccessor = $poolOrPropertyAccessor->getPropertyAccessor();
83+
84+
return;
85+
}
86+
87+
// NEXT_MAJOR: Remove this block.
88+
if ((\func_get_args()[1] ?? null) instanceof Pool) {
89+
$this->pool = \func_get_args()[1];
90+
}
91+
92+
// NEXT_MAJOR: Change $poolOrPropertyAccessor to $propertyAccessor.
93+
$this->propertyAccessor = $poolOrPropertyAccessor;
4994
}
5095

5196
/**
@@ -93,6 +138,15 @@ public function getChildFormView(FormView $formView, $elementId)
93138
*/
94139
public function getAdmin($code)
95140
{
141+
if (null === $this->pool) {
142+
throw new \LogicException(sprintf(
143+
'You MUST pass a "%s" instance as argument 2 when constructing "%s" to be able to call "%s()".',
144+
Pool::class,
145+
self::class,
146+
__METHOD__
147+
));
148+
}
149+
96150
return $this->pool->getInstance($code);
97151
}
98152

@@ -144,11 +198,9 @@ public function appendFormFieldElement(AdminInterface $admin, $subject, $element
144198
//Child form not found (probably nested one)
145199
//if childFormBuilder was not found resulted in fatal error getName() method call on non object
146200
if (!$childFormBuilder) {
147-
$propertyAccessor = $this->pool->getPropertyAccessor();
148-
149201
$path = $this->getElementAccessPath($elementId, $subject);
150202

151-
$collection = $propertyAccessor->getValue($subject, $path);
203+
$collection = $this->propertyAccessor->getValue($subject, $path);
152204

153205
if ($collection instanceof DoctrinePersistentCollection || $collection instanceof PersistentCollection) {
154206
//since doctrine 2.4
@@ -160,7 +212,7 @@ public function appendFormFieldElement(AdminInterface $admin, $subject, $element
160212
}
161213

162214
$collection->add(new $modelClassName());
163-
$propertyAccessor->setValue($subject, $path, $collection);
215+
$this->propertyAccessor->setValue($subject, $path, $collection);
164216

165217
$fieldDescription = null;
166218
} else {
@@ -281,8 +333,6 @@ public function camelize($property)
281333
*/
282334
public function getElementAccessPath($elementId, $model)
283335
{
284-
$propertyAccessor = $this->pool->getPropertyAccessor();
285-
286336
$idWithoutIdentifier = preg_replace('/^[^_]*_/', '', $elementId);
287337
$initialPath = preg_replace('#(_(\d+)_)#', '[$2]_', $idWithoutIdentifier);
288338

@@ -294,7 +344,7 @@ public function getElementAccessPath($elementId, $model)
294344
$currentPath .= empty($currentPath) ? $part : '_'.$part;
295345
$separator = empty($totalPath) ? '' : '.';
296346

297-
if ($propertyAccessor->isReadable($model, $totalPath.$separator.$currentPath)) {
347+
if ($this->propertyAccessor->isReadable($model, $totalPath.$separator.$currentPath)) {
298348
$totalPath .= $separator.$currentPath;
299349
$currentPath = '';
300350
}

src/Admin/Pool.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ class Pool
7575
protected $options = [];
7676

7777
/**
78+
* NEXT_MAJOR: Remove this property.
79+
*
7880
* @var PropertyAccessorInterface
81+
*
82+
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0.
7983
*/
8084
protected $propertyAccessor;
8185

@@ -87,6 +91,8 @@ class Pool
8791
private $templateRegistry;
8892

8993
/**
94+
* NEXT_MAJOR: Remove $propertyAccessor argument.
95+
*
9096
* @param string $title
9197
* @param string $logoTitle
9298
* @param array $options
@@ -102,6 +108,17 @@ public function __construct(
102108
$this->title = $title;
103109
$this->titleLogo = $logoTitle;
104110
$this->options = $options;
111+
112+
// NEXT_MAJOR: Remove this block.
113+
if (null !== $propertyAccessor) {
114+
@trigger_error(sprintf(
115+
'Passing an "%s" instance as argument 4 to "%s()" is deprecated since sonata-project/admin-bundle 3.x.',
116+
PropertyAccessorInterface::class,
117+
__METHOD__
118+
), E_USER_DEPRECATED);
119+
}
120+
121+
// NEXT_MAJOR: Remove next line.
105122
$this->propertyAccessor = $propertyAccessor;
106123
}
107124

@@ -545,8 +562,16 @@ public function getOption($name, $default = null)
545562
return $default;
546563
}
547564

565+
/**
566+
* @deprecated since sonata-project/admin-bundle 3.x, will be dropped in 4.0. Use Symfony "PropertyAccess" instead.
567+
*/
548568
public function getPropertyAccessor()
549569
{
570+
@trigger_error(sprintf(
571+
'The "%s" method is deprecated since version 3.x and will be removed in 4.0.',
572+
__METHOD__
573+
), E_USER_DEPRECATED);
574+
550575
if (null === $this->propertyAccessor) {
551576
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
552577
}

src/Resources/config/actions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
new ReferenceConfigurator('sonata.admin.pool'),
9090
new ReferenceConfigurator('validator'),
9191
new ReferenceConfigurator('sonata.admin.form.data_transformer_resolver'),
92+
new ReferenceConfigurator('property_accessor'),
9293
])
9394

9495
->set('sonata.admin.action.retrieve_autocomplete_items', RetrieveAutocompleteItemsAction::class)

src/Resources/config/core.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
'',
5555
'',
5656
[],
57-
new ReferenceConfigurator('property_accessor'),
5857
])
5958
->call('setTemplateRegistry', [
6059
new ReferenceConfigurator('sonata.admin.global_template_registry'),
@@ -76,6 +75,8 @@
7675
->set('sonata.admin.helper', AdminHelper::class)
7776
->public()
7877
->args([
78+
new ReferenceConfigurator('property_accessor'),
79+
// NEXT_MAJOR: Remove next line.
7980
new ReferenceConfigurator('sonata.admin.pool'),
8081
])
8182

src/Resources/config/twig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
(new ReferenceConfigurator('logger'))->nullOnInvalid(),
5252
new ReferenceConfigurator('translator'),
5353
new ReferenceConfigurator('service_container'),
54+
new ReferenceConfigurator('property_accessor'),
5455
new ReferenceConfigurator('security.authorization_checker'),
5556
])
5657
->call('setXEditableTypeMapping', [

0 commit comments

Comments
 (0)