Skip to content

Commit

Permalink
feature #20121 Class existence resource (fabpot)
Browse files Browse the repository at this point in the history
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
fabpot committed Oct 5, 2016
2 parents 63308cd + 222b56d commit 5e63ad0
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@

namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;

use Symfony\Component\Config\Resource\ClassExistenceResource;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Stopwatch\Stopwatch;
use Symfony\Component\Yaml\Parser as YamlParser;

/**
* @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
Expand Down Expand Up @@ -72,15 +76,18 @@ public function process(ContainerBuilder $container)
$container->getDefinition('twig.extension.assets')->addTag('twig.extension');
}

if (class_exists('Symfony\Component\Yaml\Parser')) {
$container->addResource(new ClassExistenceResource(YamlParser::class));
if (class_exists(YamlParser::class)) {
$container->getDefinition('twig.extension.yaml')->addTag('twig.extension');
}

if (class_exists('Symfony\Component\Stopwatch\Stopwatch')) {
$container->addResource(new ClassExistenceResource(Stopwatch::class));
if (class_exists(Stopwatch::class)) {
$container->getDefinition('twig.extension.debug.stopwatch')->addTag('twig.extension');
}

if (class_exists('Symfony\Component\ExpressionLanguage\ExpressionLanguage')) {
$container->addResource(new ClassExistenceResource(ExpressionLanguage::class));
if (class_exists(ExpressionLanguage::class)) {
$container->getDefinition('twig.extension.expression')->addTag('twig.extension');
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/TwigBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"symfony/stopwatch": "~2.8|~3.0",
"symfony/dependency-injection": "~2.8|~3.0",
"symfony/expression-language": "~2.8|~3.0",
"symfony/config": "~2.8|~3.0",
"symfony/config": "~3.2",
"symfony/finder": "~2.8|~3.0",
"symfony/routing": "~2.8|~3.0",
"symfony/templating": "~2.8|~3.0",
Expand Down
75 changes: 75 additions & 0 deletions src/Symfony/Component/Config/Resource/ClassExistenceResource.php
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);
}
}
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()));
}
}

0 comments on commit 5e63ad0

Please sign in to comment.