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

Skip to content

Commit 9e38156

Browse files
committed
[PropertyAccess] Add Doctrine Cache
1 parent 582f475 commit 9e38156

5 files changed

Lines changed: 80 additions & 4 deletions

File tree

src/Symfony/Component/PropertyAccess/PropertyAccessor.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\PropertyAccess;
1313

14+
use Doctrine\Common\Cache\Cache;
1415
use Symfony\Component\PropertyAccess\Exception\AccessException;
1516
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
1617
use 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

src/Symfony/Component/PropertyAccess/PropertyAccessorBuilder.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\PropertyAccess;
1313

14+
use Doctrine\Common\Cache\Cache;
15+
1416
/**
1517
* A configurable builder to create a PropertyAccessor.
1618
*
@@ -28,6 +30,11 @@ class PropertyAccessorBuilder
2830
*/
2931
private $throwExceptionOnInvalidIndex = false;
3032

33+
/**
34+
* @var Cache|null
35+
*/
36+
private $cache;
37+
3138
/**
3239
* Enables the use of "__call" by the PropertyAccessor.
3340
*
@@ -97,13 +104,37 @@ public function isExceptionOnInvalidIndexEnabled()
97104
return $this->throwExceptionOnInvalidIndex;
98105
}
99106

107+
/**
108+
* Uses a cache system.
109+
*
110+
* @param Cache|null $cache
111+
*
112+
* @return PropertyAccessorBuilder The builder object
113+
*/
114+
public function useCache(Cache $cache = null)
115+
{
116+
$this->cache = $cache;
117+
118+
return $this;
119+
}
120+
121+
/**
122+
* Gets the cache system.
123+
*
124+
* @return Cache|null
125+
*/
126+
public function getCache()
127+
{
128+
return $this->cache;
129+
}
130+
100131
/**
101132
* Builds and returns a new PropertyAccessor object.
102133
*
103134
* @return PropertyAccessorInterface The built PropertyAccessor
104135
*/
105136
public function getPropertyAccessor()
106137
{
107-
return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex);
138+
return new PropertyAccessor($this->magicCall, $this->throwExceptionOnInvalidIndex, $this->cache);
108139
}
109140
}

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorBuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\PropertyAccess\Tests;
1313

14+
use Doctrine\Common\Cache\ArrayCache;
1415
use Symfony\Component\PropertyAccess\PropertyAccessorBuilder;
1516

1617
class PropertyAccessorBuilderTest extends \PHPUnit_Framework_TestCase
@@ -52,4 +53,12 @@ public function testGetPropertyAccessor()
5253
$this->assertInstanceOf('Symfony\Component\PropertyAccess\PropertyAccessor', $this->builder->getPropertyAccessor());
5354
$this->assertInstanceOf('Symfony\Component\PropertyAccess\PropertyAccessor', $this->builder->enableMagicCall()->getPropertyAccessor());
5455
}
56+
57+
public function testUseCache()
58+
{
59+
$cache = new ArrayCache();
60+
$this->builder->useCache($cache);
61+
$this->assertEquals($cache, $this->builder->getCache());
62+
$this->assertInstanceOf('Symfony\Component\PropertyAccess\PropertyAccessor', $this->builder->getPropertyAccessor());
63+
}
5564
}

src/Symfony/Component/PropertyAccess/Tests/PropertyAccessorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\PropertyAccess\Tests;
1313

14+
use Doctrine\Common\Cache\ArrayCache;
15+
use Doctrine\Common\Cache\Cache;
1416
use Symfony\Component\PropertyAccess\Exception\NoSuchIndexException;
1517
use Symfony\Component\PropertyAccess\PropertyAccessor;
1618
use Symfony\Component\PropertyAccess\Tests\Fixtures\TestClass;
@@ -510,4 +512,15 @@ public function testIsWritableForReferenceChainIssue($object, $path, $value)
510512
{
511513
$this->assertEquals($value, $this->propertyAccessor->isWritable($object, $path));
512514
}
515+
516+
public function testCacheReadAccess()
517+
{
518+
$obj = new TestClass('foo');
519+
520+
$propertyAccessor = new PropertyAccessor(false, false, new ArrayCache());
521+
$this->assertEquals('foo', $propertyAccessor->getValue($obj, 'publicAccessor'));
522+
$propertyAccessor->setValue($obj, 'publicAccessor', 'bar');
523+
$propertyAccessor->setValue($obj, 'publicAccessor', 'baz');
524+
$this->assertEquals('baz', $propertyAccessor->getValue($obj, 'publicAccessor'));
525+
}
513526
}

src/Symfony/Component/PropertyAccess/composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
"require": {
1919
"php": ">=5.5.9"
2020
},
21+
"require-dev": {
22+
"doctrine/cache": "~1.0"
23+
},
24+
"suggest": {
25+
"doctrine/cache": "To cache access methods"
26+
},
2127
"autoload": {
2228
"psr-4": { "Symfony\\Component\\PropertyAccess\\": "" },
2329
"exclude-from-classmap": [

0 commit comments

Comments
 (0)