[Config] added ClassExistenceResource

This commit is contained in:
Fabien Potencier 2016-10-01 13:54:25 -07:00
parent 362a8ac01a
commit d98eb7b5bb
2 changed files with 129 additions and 0 deletions

View File

@ -0,0 +1,75 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Resource;
/**
* ClassExistenceResource represents a class availability.
* Freshness is only evaluated against resource availability.
*
* The resource must be a fully-qualified class name.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ClassExistenceResource implements SelfCheckingResourceInterface, \Serializable
{
private $resource;
private $exists;
/**
* @param string $resource The fully-qualified class name
*/
public function __construct($resource)
{
$this->resource = $resource;
$this->exists = class_exists($resource);
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->resource;
}
/**
* @return string The file path to the resource
*/
public function getResource()
{
return $this->resource;
}
/**
* {@inheritdoc}
*/
public function isFresh($timestamp)
{
return class_exists($this->resource) === $this->exists;
}
/**
* {@inheritdoc}
*/
public function serialize()
{
return serialize(array($this->resource, $this->exists));
}
/**
* {@inheritdoc}
*/
public function unserialize($serialized)
{
list($this->resource, $this->exists) = unserialize($serialized);
}
}

View File

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Config\Tests\Resource;
use Symfony\Component\Config\Resource\ClassExistenceResource;
class ClassExistenceResourceTest extends \PHPUnit_Framework_TestCase
{
public function testToString()
{
$res = new ClassExistenceResource('BarClass');
$this->assertSame('BarClass', (string) $res);
}
public function testGetResource()
{
$res = new ClassExistenceResource('BarClass');
$this->assertSame('BarClass', $res->getResource());
}
public function testIsFreshWhenClassDoesNotExist()
{
$res = new ClassExistenceResource('Symfony\Component\Config\Tests\Fixtures\BarClass');
$this->assertTrue($res->isFresh(time()));
eval(<<<EOF
namespace Symfony\Component\Config\Tests\Fixtures;
class BarClass
{
}
EOF
);
$this->assertFalse($res->isFresh(time()));
}
public function testIsFreshWhenClassExists()
{
$res = new ClassExistenceResource('Symfony\Component\Config\Tests\Resource\ClassExistenceResourceTest');
$this->assertTrue($res->isFresh(time()));
}
}