Commit 754909b2 authored by catch's avatar catch
Browse files

Revert "fix: #3616328 Key the typed data prototype cache by the definition,...

Revert "fix: #3616328 Key the typed data prototype cache by the definition, not by root type and property path"

This reverts commit 3f2fe1a8.
parent 3f2fe1a8
Loading
Loading
Loading
Loading
Loading
+6
Changes for core/.phpstan-baseline.php: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -14431,6 +14431,12 @@
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/TypedData/TypedDataManager.php',
];
$ignoreErrors[] = [
	'message' => '#^Storing entity storage as a class property is not recommended\\. Call Drupal\\\\Core\\\\Entity\\\\EntityTypeManagerInterface\\:\\:getStorage\\(\\) at the call\\-site instead\\.$#',
	'identifier' => 'drupal.entityStoragePropertyAssignment',
	'count' => 1,
	'path' => __DIR__ . '/lib/Drupal/Core/TypedData/TypedDataManager.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\Core\\\\TypedData\\\\TypedDataManagerInterface\\:\\:setValidationConstraintManager\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+41 −34
Changes for core/lib/Drupal/Core/TypedData/TypedDataManager.php: 41 added lines, 34 removed lines.
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\TypedData\Attribute\DataType;
@@ -19,6 +20,7 @@
 * Manages data type plugins.
 */
class TypedDataManager extends DefaultPluginManager implements TypedDataManagerInterface {
  use DependencySerializationTrait;

  /**
   * The validator used for validating typed data.
@@ -35,17 +37,11 @@ class TypedDataManager extends DefaultPluginManager implements TypedDataManagerI
  protected $constraintManager;

  /**
   * Typed data property prototypes, keyed by their data definition object.
   * An array of typed data property prototypes.
   *
   * The map is created on the first lookup.
   *
   * @var \WeakMap<\Drupal\Core\TypedData\DataDefinitionInterface, \Drupal\Core\TypedData\TypedDataInterface>|null
   *
   * @todo Add the ?\WeakMap type in drupal:13.0.0. Until then the property is
   *   untyped, so that code writing an array to it gets a deprecation instead
   *   of a TypeError.
   * @var array
   */
  protected $prototypes = NULL;
  protected $prototypes = [];

  /**
   * Constructs a new TypedDataManager.
@@ -158,6 +154,39 @@ public function getInstance(array $options) {
   * {@inheritdoc}
   */
  public function getPropertyInstance(TypedDataInterface $object, $property_name, $value = NULL) {
    // For performance, try to reuse existing prototypes instead of
    // constructing new objects when possible. A prototype is reused when
    // creating a data object:
    // - for a similar root object (same data type and settings),
    // - at the same property path under that root object.
    $root_definition = $object->getRoot()->getDataDefinition();
    // If the root object is a list, we want to look at the data type and the
    // settings of its item definition.
    if ($root_definition instanceof ListDataDefinition) {
      $root_definition = $root_definition->getItemDefinition();
    }

    // Root data type and settings.
    $parts[] = $root_definition->getDataType();
    if ($settings = $root_definition->getSettings()) {
      // Include the settings serialized as JSON as part of the key. The JSON is
      // a shorter string than the serialized form, so array access is faster.
      $parts[] = json_encode($settings);
    }
    // Property path for the requested data object.
    $parts[] = $object->getPropertyPath();
    // Only property instances of complex data types should be cached by the
    // property name, as they represent different properties. Properties of list
    // data types are the items of the list and the property name represents
    // only the delta in that list and not an unique property, which is why all
    // items should use the same prototype.
    if ($object instanceof ComplexDataInterface) {
      $parts[] = $property_name;
    }
    $key = implode(':', $parts);

    // Create the prototype if needed.
    if (!isset($this->prototypes[$key])) {
      // Fetch the data definition for the child object from the parent.
      if ($object instanceof ComplexDataInterface) {
        $definition = $object->getDataDefinition()->getPropertyDefinition($property_name);
@@ -171,40 +200,18 @@ public function getPropertyInstance(TypedDataInterface $object, $property_name,
      if (!$definition) {
        throw new \InvalidArgumentException("Property $property_name is unknown.");
      }

    // For performance, try to reuse existing prototypes instead of
    // constructing new objects when possible. A prototype is derived from the
    // definition it was built from: create() takes the class from
    // $definition->getClass(), and the name and parent passed below are wiped
    // again by setContext(NULL, NULL). Prototypes are therefore keyed by the
    // definition object itself, so that two distinct definition objects never
    // share a prototype.
    //
    // The prototypes are held in a \WeakMap so that each one is released
    // together with the data definition it was built from, instead of growing
    // an array that is only ever emptied by clearCachedDefinitions().
    if (!$this->prototypes instanceof \WeakMap) {
      if ($this->prototypes !== NULL) {
        @trigger_error('Setting \\Drupal\\Core\\TypedData\\TypedDataManager::$prototypes to anything other than a \\WeakMap or NULL is deprecated in drupal:11.6.0 and is removed from drupal:13.0.0. See https://www.drupal.org/node/3638699', E_USER_DEPRECATED);
      }
      $this->prototypes = new \WeakMap();
    }
    $prototype = $this->prototypes[$definition] ?? NULL;

    // Create the prototype if needed.
    if ($prototype === NULL) {
      // Create the prototype without any value, but with initial parenting
      // so that constructors can set up the objects correctly.
      $prototype = $this->create($definition, NULL, $property_name, $object);
      // Unset the parent so that it is not statically cached within the
      // prototype. It will be set again later anyway.
      $prototype->setContext(NULL, NULL);
      $this->prototypes[$definition] = $prototype;
      $this->prototypes[$key] = $prototype;
    }

    // Clone the prototype, update its parenting information, and assign the
    // value.
    $property = clone $prototype;
    $property = clone $this->prototypes[$key];
    $property->setContext($property_name, $object);
    if (isset($value)) {
      $property->setValue($value, FALSE);
@@ -281,7 +288,7 @@ public function getDefaultConstraints(DataDefinitionInterface $definition) {
   */
  public function clearCachedDefinitions() {
    parent::clearCachedDefinitions();
    $this->prototypes = new \WeakMap();
    $this->prototypes = [];
  }

  /**
+4 −5
Changes for core/lib/Drupal/Core/TypedData/TypedDataManagerInterface.php: 4 added lines, 5 removed lines.
Original line number Diff line number Diff line
@@ -134,14 +134,13 @@ public function getInstance(array $options);
   * Get a typed data instance for a property of a given typed data object.
   *
   * This method will use prototyping for fast and efficient instantiation of
   * many property objects sharing the same data definition; for example,
   * many property objects with the same property path; for example,
   * when multiple comments are used comment_body.0.value needs to be
   * instantiated very often.
   *
   * Prototyping is done per data definition object, i.e. all property
   * instances created from the same property or item data definition object
   * share a prototype. Two distinct definition objects never share one, even
   * when they declare the same property name under the same root.
   * Prototyping is done by the root object's data type and the given
   * property path, i.e. all property instances having the same property path
   * and inheriting from the same data type are prototyped.
   *
   * @param \Drupal\Core\TypedData\TypedDataInterface $object
   *   The parent typed data object, implementing the TypedDataInterface and
+1 −1
Changes for core/modules/migrate/src/Plugin/migrate/destination/EntityFieldInstance.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -41,7 +41,7 @@
 * @endcode
 *
 * @see \Drupal\field\Entity\FieldConfig
 * @see \Drupal\Core\Field\FieldConfigBase
 * @see \Drupal\field\Entity\FieldConfigBase
 */
#[MigrateDestination('entity:field_config')]
class EntityFieldInstance extends EntityConfigBase {
Loading