Skip to content

[Config] Allow nonexistent file resources #20836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
*/
class FileExistenceResource implements SelfCheckingResourceInterface, \Serializable
{
private $resource;
protected $resource;

private $exists;
protected $exists;

/**
* Constructor.
Expand Down
44 changes: 15 additions & 29 deletions src/Symfony/Component/Config/Resource/FileResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,8 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class FileResource implements SelfCheckingResourceInterface, \Serializable
class FileResource extends FileExistenceResource
{
/**
* @var string|false
*/
private $resource;

/**
* Constructor.
*
Expand All @@ -34,44 +29,35 @@ class FileResource implements SelfCheckingResourceInterface, \Serializable
*/
public function __construct($resource)
{
$this->resource = realpath($resource) ?: (file_exists($resource) ? $resource : false);
$resource = realpath($resource) ?: $resource;

if (false === $this->resource) {
throw new \InvalidArgumentException(sprintf('The file "%s" does not exist.', $resource));
}
parent::__construct($resource);
}

/**
* {@inheritdoc}
*/
public function __toString()
public function isFresh($timestamp)
{
return $this->resource;
}
if (!parent::isFresh($timestamp)) {
return false;
}

/**
* @return string The canonicalized, absolute path to the resource
*/
public function getResource()
{
return $this->resource;
return !file_exists($this->resource) || filemtime($this->resource) <= $timestamp;
}

/**
* {@inheritdoc}
*/
public function isFresh($timestamp)
public function unserialize($serialized)
{
return file_exists($this->resource) && @filemtime($this->resource) <= $timestamp;
}
$unserialized = unserialize($serialized);

public function serialize()
{
return serialize($this->resource);
}
// compatibility with previously serialized resource
if (!is_array($unserialized)) {
$unserialized = array($unserialized, true);
}

public function unserialize($serialized)
{
$this->resource = unserialize($serialized);
list($this->resource, $this->exists) = $unserialized;
}
}
23 changes: 12 additions & 11 deletions src/Symfony/Component/Config/Tests/Resource/FileResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,33 +52,34 @@ public function testToString()
$this->assertSame(realpath($this->file), (string) $this->resource);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The file ".*" does not exist./
*/
public function testResourceDoesNotExist()
{
$resource = new FileResource('/____foo/foobar'.mt_rand(1, 999999));
}

public function testIsFresh()
{
$this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
$this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
$this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
}

public function testIsFreshForCreatedResources()
{
unlink($this->file);

$this->resource = new FileResource($this->file);
touch($this->file, $this->time);

$this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource is created');
}

public function testIsFreshForDeletedResources()
{
unlink($this->file);

$this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist');
$this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource is deleted');
}

public function testSerializeUnserialize()
{
$unserialized = unserialize(serialize($this->resource));

$this->assertSame(realpath($this->file), $this->resource->getResource());
$this->assertSame(realpath($this->file), $unserialized->getResource());
}
}