diff vendor/symfony/serializer/Normalizer/PropertyNormalizer.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
line wrap: on
line diff
--- a/vendor/symfony/serializer/Normalizer/PropertyNormalizer.php	Mon Apr 23 09:33:26 2018 +0100
+++ b/vendor/symfony/serializer/Normalizer/PropertyNormalizer.php	Mon Apr 23 09:46:53 2018 +0100
@@ -30,12 +30,14 @@
  */
 class PropertyNormalizer extends AbstractObjectNormalizer
 {
+    private $cache = array();
+
     /**
      * {@inheritdoc}
      */
     public function supportsNormalization($data, $format = null)
     {
-        return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
+        return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
     }
 
     /**
@@ -43,7 +45,7 @@
      */
     public function supportsDenormalization($data, $type, $format = null)
     {
-        return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
+        return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
     }
 
     /**
@@ -58,11 +60,13 @@
         $class = new \ReflectionClass($class);
 
         // We look for at least one non-static property
-        foreach ($class->getProperties() as $property) {
-            if (!$property->isStatic()) {
-                return true;
+        do {
+            foreach ($class->getProperties() as $property) {
+                if (!$property->isStatic()) {
+                    return true;
+                }
             }
-        }
+        } while ($class = $class->getParentClass());
 
         return false;
     }
@@ -77,7 +81,7 @@
         }
 
         try {
-            $reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute);
+            $reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
             if ($reflectionProperty->isStatic()) {
                 return false;
             }
@@ -96,13 +100,15 @@
         $reflectionObject = new \ReflectionObject($object);
         $attributes = array();
 
-        foreach ($reflectionObject->getProperties() as $property) {
-            if (!$this->isAllowedAttribute($object, $property->name)) {
-                continue;
+        do {
+            foreach ($reflectionObject->getProperties() as $property) {
+                if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name)) {
+                    continue;
+                }
+
+                $attributes[] = $property->name;
             }
-
-            $attributes[] = $property->name;
-        }
+        } while ($reflectionObject = $reflectionObject->getParentClass());
 
         return $attributes;
     }
@@ -113,7 +119,7 @@
     protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
     {
         try {
-            $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
+            $reflectionProperty = $this->getReflectionProperty($object, $attribute);
         } catch (\ReflectionException $reflectionException) {
             return;
         }
@@ -132,7 +138,7 @@
     protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
     {
         try {
-            $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
+            $reflectionProperty = $this->getReflectionProperty($object, $attribute);
         } catch (\ReflectionException $reflectionException) {
             return;
         }
@@ -148,4 +154,26 @@
 
         $reflectionProperty->setValue($object, $value);
     }
+
+    /**
+     * @param string|object $classOrObject
+     * @param string        $attribute
+     *
+     * @return \ReflectionProperty
+     *
+     * @throws \ReflectionException
+     */
+    private function getReflectionProperty($classOrObject, $attribute)
+    {
+        $reflectionClass = new \ReflectionClass($classOrObject);
+        while (true) {
+            try {
+                return $reflectionClass->getProperty($attribute);
+            } catch (\ReflectionException $e) {
+                if (!$reflectionClass = $reflectionClass->getParentClass()) {
+                    throw $e;
+                }
+            }
+        }
+    }
 }