8000 [Config] added ClassExistenceResource · symfony/symfony@d98eb7b · GitHub
[go: up one dir, main page]

Skip to content

Commit d98eb7b

Browse files
committed
[Config] added ClassExistenceResource
1 parent 362a8ac commit d98eb7b

File tree

2 files changed

+129
-0
lines changed

2 files changed

+129
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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\Config\Resource;
13+
14+
/**
15+
* ClassExistenceResource represents a class availability.
16+
* Freshness is only evaluated against resource availability.
17+
*
18+
* The resource must be a fully-qualified class name.
19+
*
20+
* @author Fabien Potencier <fabien@symfony.com>
21+
*/
22+
class ClassExistenceResource implements SelfCheckingResourceInterface, \Serializable
23+
{
24+
private $resource;
25+
private $exists;
26+
27+
/**
28+
* @param string $resource The fully-qualified class name
29+
*/
30+
public function __construct($resource)
31+
{
32+
$this->resource = $resource;
33+
$this->exists = class_exists($resource);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function __toString()
40+
{
41+
return $this->resource;
42+
}
43+
44+
/**
45+
* @return string The file path to the resource
46+
*/
47+
public function getResource()
48+
{
49+
return $this->resource;
50+
}
51+
52+
/**
53+
* {@inheritdoc}
54+
*/
55+
public function isFresh($timestamp)
56+
{
57+
return class_exists($this->resource) === $this->exists;
58+
}
59+
60+
/**
61+
* {@inheritdoc}
62+
*/
63+
public function serialize()
64+
{
65+
return serialize(array($this->resource, $this->exists));
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function unserialize($serialized)
72+
{
73+
list($this->resource, $this->exists) = unserialize($serialized);
74+
}
75+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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\Config\Tests\Resource;
13+
14+
use Symfony\Component\Config\Resource\ClassExistenceResource;
15+
16+
class ClassExistenceResourceTest extends \PHPUnit_Framework_TestCase
17+
{
18+
public function testToString()
19+
{
20+
$res = new ClassExistenceResource('BarClass');
21+
$this->assertSame('BarClass', (string) $res);
22+
}
23+
24+
public function testGetResource()
25+
{
26+
$res = new ClassExistenceResource('BarClass');
27+
$this->assertSame('BarClass', $res->getResource());
28+
}
29+
30+
public function testIsFreshWhenClassDoesNotExist()
31+
{
32+
$res = new ClassExistenceResource('Symfony\Component\Config\Tests\Fixtures\BarClass');
33+
34+
$this->assertTrue($res->isFresh(time()));
35+
36+
eval(<<<EOF
37+
namespace Symfony\Component\Config\Tests\Fixtures;
38+
39+
class BarClass
40+
{
41+
}
42+
EOF
43+
);
44+
45+
$this->assertFalse($res->isFresh(time()));
46+
}
47+
48+
public function testIsFreshWhenClassExists()
49+
{
50+
$res = new ClassExistenceResource('Symfony\Component\Config\Tests\Resource\ClassExistenceResourceTest');
51+
52+
$this->assertTrue($res->isFresh(time()));
53+
}
54+
}

0 commit comments

Comments
 (0)
0