1111
1212namespace Symfony \Component \PropertyAccess ;
1313
14+ use Doctrine \Common \Cache \Cache ;
1415use Symfony \Component \PropertyAccess \Exception \AccessException ;
1516use Symfony \Component \PropertyAccess \Exception \NoSuchPropertyException ;
1617use Symfony \Component \PropertyAccess \Exception \NoSuchIndexException ;
@@ -38,6 +39,8 @@ class PropertyAccessor implements PropertyAccessorInterface
3839 const ACCESS_TYPE_MAGIC = 2 ;
3940 const ACCESS_TYPE_ADDER_AND_REMOVER = 3 ;
4041 const ACCESS_TYPE_NOT_FOUND = 4 ;
42+ const CACHE_PREFIX_READ = 'r ' ;
43+ const CACHE_PREFIX_WRITE = 'w ' ;
4144
4245 /**
4346 * @var bool
@@ -49,6 +52,11 @@ class PropertyAccessor implements PropertyAccessorInterface
4952 */
5053 private $ ignoreInvalidIndices ;
5154
55+ /**
56+ * @var Cache
57+ */
58+ private $ cache ;
59+
5260 /**
5361 * @var array
5462 */
@@ -63,10 +71,11 @@ class PropertyAccessor implements PropertyAccessorInterface
6371 * Should not be used by application code. Use
6472 * {@link PropertyAccess::createPropertyAccessor()} instead.
6573 */
66- public function __construct ($ magicCall = false , $ throwExceptionOnInvalidIndex = false )
74+ public function __construct ($ magicCall = false , $ throwExceptionOnInvalidIndex = false , Cache $ cache = null )
6775 {
6876 $ this ->magicCall = $ magicCall ;
6977 $ this ->ignoreInvalidIndices = !$ throwExceptionOnInvalidIndex ;
78+ $ this ->cache = $ cache ;
7079 }
7180
7281 /**
@@ -401,7 +410,7 @@ private function getReadAccessInfo($object, $property)
401410
402411 if (isset ($ this ->readPropertyCache [$ key ])) {
403412 $ access = $ this ->readPropertyCache [$ key ];
404- } else {
413+ } elseif (! $ this -> cache || ! $ access = $ this -> cache -> fetch ( self :: CACHE_PREFIX_READ . $ key )) {
405414 $ access = array ();
406415
407416 $ reflClass = new \ReflectionClass ($ object );
@@ -456,6 +465,10 @@ private function getReadAccessInfo($object, $property)
456465 );
457466 }
458467
468+ if ($ this ->cache ) {
469+ $ this ->cache ->save (self ::CACHE_PREFIX_READ .$ key , $ access );
470+ }
471+
459472 $ this ->readPropertyCache [$ key ] = $ access ;
460473 }
461474
@@ -583,7 +596,7 @@ private function getWriteAccessInfo($object, $property, $value)
583596
584597 if (isset ($ this ->writePropertyCache [$ key ])) {
585598 $ access = $ this ->writePropertyCache [$ key ];
586- } else {
599+ } elseif (! $ this -> cache || ! $ access = $ this -> cache -> fetch ( self :: CACHE_PREFIX_WRITE . $ key )) {
587600 $ access = array ();
588601
589602 $ reflClass = new \ReflectionClass ($ object );
@@ -645,6 +658,10 @@ private function getWriteAccessInfo($object, $property, $value)
645658 }
646659 }
647660
661+ if ($ this ->cache ) {
662+ $ this ->cache ->save (self ::CACHE_PREFIX_WRITE .$ key , $ access );
663+ }
664+
648665 $ this ->writePropertyCache [$ key ] = $ access ;
649666 }
650667
0 commit comments