Skip to content

Commit 436f650

Browse files
committed
[PropertyInfo] Cache Decorator
1 parent 1a7530c commit 436f650

File tree

6 files changed

+227
-97
lines changed

6 files changed

+227
-97
lines changed

src/Symfony/Component/PropertyInfo/PropertyInfoExtractor.php

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

1212
namespace Symfony\Component\PropertyInfo;
1313

14-
use Doctrine\Common\Cache\Cache;
15-
1614
/**
1715
* Default {@see PropertyInfoExtractorInterface} implementation.
1816
*
@@ -40,30 +38,18 @@ class PropertyInfoExtractor implements PropertyInfoExtractorInterface
4038
*/
4139
private $accessExtractors;
4240

43-
/**
44-
* @var Cache
45-
*/
46-
private $cache;
47-
48-
/**
49-
* @var array
50-
*/
51-
private $arrayCache = array();
52-
5341
/**
5442
* @param PropertyListExtractorInterface[] $listExtractors
5543
* @param PropertyTypeExtractorInterface[] $typeExtractors
5644
* @param PropertyDescriptionExtractorInterface[] $descriptionExtractors
5745
* @param PropertyAccessExtractorInterface[] $accessExtractors
58-
* @param Cache|null $cache
5946
*/
60-
public function __construct(array $listExtractors = array(), array $typeExtractors = array(), array $descriptionExtractors = array(), array $accessExtractors = array(), Cache $cache = null)
47+
public function __construct(array $listExtractors = array(), array $typeExtractors = array(), array $descriptionExtractors = array(), array $accessExtractors = array())
6148
{
6249
$this->listExtractors = $listExtractors;
6350
$this->typeExtractors = $typeExtractors;
6451
$this->descriptionExtractors = $descriptionExtractors;
6552
$this->accessExtractors = $accessExtractors;
66-
$this->cache = $cache;
6753
}
6854

6955
/**
@@ -125,27 +111,11 @@ public function isWritable($class, $property, array $context = array())
125111
*/
126112
private function extract(array $extractors, $method, array $arguments)
127113
{
128-
$key = $method.serialize($arguments);
129-
130-
if (isset($this->arrayCache[$key])) {
131-
return $this->arrayCache[$key];
132-
}
133-
134-
if ($this->cache && $value = $this->cache->fetch($key)) {
135-
return $this->arrayCache[$key] = $value;
136-
}
137-
138114
foreach ($extractors as $extractor) {
139115
$value = call_user_func_array(array($extractor, $method), $arguments);
140116
if (null !== $value) {
141-
break;
117+
return $value;
142118
}
143119
}
144-
145-
if ($this->cache) {
146-
$this->cache->save($key, $value);
147-
}
148-
149-
return $this->arrayCache[$key] = $value;
150120
}
151121
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyInfo;
13+
14+
use Doctrine\Common\Cache\Cache;
15+
16+
/**
17+
* Adds a cache layer on top of an extractor.
18+
*
19+
* @author Kévin Dunglas <dunglas@gmail.com>
20+
*/
21+
class PropertyInfoExtractorCacheDecorator implements PropertyInfoExtractorInterface
22+
{
23+
/**
24+
* @var PropertyInfoExtractorInterface
25+
*/
26+
private $propertyInfoExtractor;
27+
28+
/**
29+
* @var Cache
30+
*/
31+
private $cache;
32+
33+
/**
34+
* @var array
35+
*/
36+
private $arrayCache = array();
37+
38+
public function __construct(PropertyInfoExtractorInterface $propertyInfoExtractor, Cache $cache)
39+
{
40+
$this->propertyInfoExtractor = $propertyInfoExtractor;
41+
$this->cache = $cache;
42+
}
43+
44+
/**
45+
* {@inheritdoc}
46+
*/
47+
public function isReadable($class, $property, array $context = array())
48+
{
49+
return $this->extract('isReadable', array($class, $property, $context));
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function isWritable($class, $property, array $context = array())
56+
{
57+
return $this->extract('isWritable', array($class, $property, $context));
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function getShortDescription($class, $property, array $context = array())
64+
{
65+
return $this->extract('getShortDescription', array($class, $property, $context));
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function getLongDescription($class, $property, array $context = array())
72+
{
73+
return $this->extract('getLongDescription', array($class, $property, $context));
74+
}
75+
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function getProperties($class, array $context = array())
80+
{
81+
return $this->extract('getProperties', array($class, $context));
82+
}
83+
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function getTypes($class, $property, array $context = array())
88+
{
89+
return $this->extract('getTypes', array($class, $context));
90+
}
91+
92+
/**
93+
* Retrieves the cached data if applicable or delegates to the decorated extractor.
94+
*
95+
* @param string $method
96+
* @param array $arguments
97+
*
98+
* @return mixed
99+
*/
100+
private function extract($method, array $arguments)
101+
{
102+
$key = $method.serialize($arguments);
103+
104+
if (isset($this->arrayCache[$key])) {
105+
return $this->arrayCache[$key];
106+
}
107+
108+
if ($value = $this->cache->fetch($key)) {
109+
return $this->arrayCache[$key] = $value;
110+
}
111+
112+
$value = call_user_func_array(array($this->propertyInfoExtractor, $method), $arguments);
113+
$this->cache->save($key, $value);
114+
115+
return $this->arrayCache[$key] = $value;
116+
}
117+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyInfo\Tests;
13+
14+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
15+
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyExtractor;
16+
use Symfony\Component\PropertyInfo\Tests\Fixtures\NullExtractor;
17+
use Symfony\Component\PropertyInfo\Type;
18+
19+
/**
20+
* @author Kévin Dunglas <dunglas@gmail.com>
21+
*/
22+
class AbstractPropertyInfoExtractorTest extends \PHPUnit_Framework_TestCase
23+
{
24+
/**
25+
* @var PropertyInfoExtractor
26+
*/
27+
protected $propertyInfo;
28+
29+
public function setUp()
30+
{
31+
$extractors = array(new NullExtractor(), new DummyExtractor());
32+
$this->propertyInfo = new PropertyInfoExtractor($extractors, $extractors, $extractors, $extractors);
33+
}
34+
35+
public function testInstanceOf()
36+
{
37+
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface', $this->propertyInfo);
38+
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface', $this->propertyInfo);
39+
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface', $this->propertyInfo);
40+
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface', $this->propertyInfo);
41+
}
42+
43+
public function testGetShortDescription()
44+
{
45+
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
46+
}
47+
48+
public function testGetLongDescription()
49+
{
50+
$this->assertSame('long', $this->propertyInfo->getLongDescription('Foo', 'bar', array()));
51+
}
52+
53+
public function testGetTypes()
54+
{
55+
$this->assertEquals(array(new Type(Type::BUILTIN_TYPE_INT)), $this->propertyInfo->getTypes('Foo', 'bar', array()));
56+
}
57+
58+
public function testIsReadable()
59+
{
60+
$this->assertTrue($this->propertyInfo->isReadable('Foo', 'bar', array()));
61+
}
62+
63+
public function testIsWritable()
64+
{
65+
$this->assertTrue($this->propertyInfo->isWritable('Foo', 'bar', array()));
66+
}
67+
68+
public function testGetProperties()
69+
{
70+
$this->assertEquals(array('a', 'b'), $this->propertyInfo->getProperties('Foo'));
71+
}
72+
}

src/Symfony/Component/PropertyInfo/Tests/Extractors/SerializerExtractorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\PropertyInfo\PropertyInfo\Tests\Extractors;
12+
namespace Symfony\Component\PropertyInfo\Tests\Extractors;
1313

1414
use Doctrine\Common\Annotations\AnnotationReader;
1515
use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\PropertyInfo\Tests;
13+
14+
use Doctrine\Common\Cache\ArrayCache;
15+
use Symfony\Component\PropertyInfo\PropertyInfoExtractorCacheDecorator;
16+
17+
/**
18+
* @author Kévin Dunglas <dunglas@gmail.com>
19+
*/
20+
class PropertyInfoExtractorCacheDecoratorTest extends AbstractPropertyInfoExtractorTest
21+
{
22+
public function setUp()
23+
{
24+
parent::setUp();
25+
26+
$this->propertyInfo = new PropertyInfoExtractorCacheDecorator($this->propertyInfo, new ArrayCache());
27+
}
28+
29+
public function testCache()
30+
{
31+
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
32+
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
33+
}
34+
}

src/Symfony/Component/PropertyInfo/Tests/PropertyInfoExtractorTest.php

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,72 +11,9 @@
1111

1212
namespace Symfony\Component\PropertyInfo\Tests;
1313

14-
use Doctrine\Common\Cache\ArrayCache;
15-
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
16-
use Symfony\Component\PropertyInfo\Tests\Fixtures\DummyExtractor;
17-
use Symfony\Component\PropertyInfo\Tests\Fixtures\NullExtractor;
18-
use Symfony\Component\PropertyInfo\Type;
19-
2014
/**
2115
* @author Kévin Dunglas <dunglas@gmail.com>
2216
*/
23-
class PropertyInfoExtractorTest extends \PHPUnit_Framework_TestCase
17+
class PropertyInfoExtractorTest extends AbstractPropertyInfoExtractorTest
2418
{
25-
/**
26-
* @var PropertyInfoExtractor
27-
*/
28-
private $propertyInfo;
29-
30-
public function setUp()
31-
{
32-
$extractors = array(new NullExtractor(), new DummyExtractor());
33-
$this->propertyInfo = new PropertyInfoExtractor($extractors, $extractors, $extractors, $extractors);
34-
}
35-
36-
public function testInstanceOf()
37-
{
38-
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface', $this->propertyInfo);
39-
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface', $this->propertyInfo);
40-
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface', $this->propertyInfo);
41-
$this->assertInstanceOf('Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface', $this->propertyInfo);
42-
}
43-
44-
public function testGetShortDescription()
45-
{
46-
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
47-
}
48-
49-
public function testGetLongDescription()
50-
{
51-
$this->assertSame('long', $this->propertyInfo->getLongDescription('Foo', 'bar', array()));
52-
}
53-
54-
public function testGetTypes()
55-
{
56-
$this->assertEquals(array(new Type(Type::BUILTIN_TYPE_INT)), $this->propertyInfo->getTypes('Foo', 'bar', array()));
57-
}
58-
59-
public function testIsReadable()
60-
{
61-
$this->assertTrue($this->propertyInfo->isReadable('Foo', 'bar', array()));
62-
}
63-
64-
public function testIsWritable()
65-
{
66-
$this->assertTrue($this->propertyInfo->isWritable('Foo', 'bar', array()));
67-
}
68-
69-
public function testGetProperties()
70-
{
71-
$this->assertEquals(array('a', 'b'), $this->propertyInfo->getProperties('Foo'));
72-
}
73-
74-
public function testCache()
75-
{
76-
$extractors = array(new NullExtractor(), new DummyExtractor());
77-
$this->propertyInfo = new PropertyInfoExtractor($extractors, $extractors, $extractors, $extractors, new ArrayCache());
78-
79-
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
80-
$this->assertSame('short', $this->propertyInfo->getShortDescription('Foo', 'bar', array()));
81-
}
8219
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy