-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #20121 Class existence resource (fabpot)
This PR was merged into the 3.2-dev branch. Discussion ---------- Class existence resource | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | see #20094 | License | MIT | Doc PR | n/a Commits ------- 222b56d [TwigBundle] added support for ClassExistenceResource when relevant d98eb7b [Config] added ClassExistenceResource
- Loading branch information
Showing
4 changed files
with
140 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/Symfony/Component/Config/Resource/ClassExistenceResource.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 existence. | ||
* Freshness is only evaluated against resource existence. | ||
* | ||
* 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); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/Config/Tests/Resource/ClassExistenceResourceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |