-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DependencyInjection] added a way to retrieve loaded resources
- Loading branch information
Showing
10 changed files
with
226 additions
and
39 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
66 changes: 66 additions & 0 deletions
66
src/Symfony/Components/DependencyInjection/FileResource.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,66 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\DependencyInjection; | ||
|
||
/* | ||
* This file is part of the symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* FileResource represents a resource stored on the filesystem. | ||
* | ||
* @package symfony | ||
* @subpackage dependency_injection | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
class FileResource implements ResourceInterface | ||
{ | ||
protected $resource; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $resource The file path to the resource | ||
*/ | ||
public function __construct($resource) | ||
{ | ||
$this->resource = $resource; | ||
} | ||
|
||
/** | ||
* Returns the resource tied to this Resource. | ||
* | ||
* @return mixed The resource | ||
*/ | ||
public function getResource() | ||
{ | ||
return $this->resource; | ||
} | ||
|
||
/** | ||
* Returns true if the resource has not been updated since the given timestamp. | ||
* | ||
* @param timestamp $timestamp The last time the resource was loaded | ||
* | ||
* @return Boolean true if the resource has not been updated, false otherwise | ||
*/ | ||
public function isUptodate($timestamp = null) | ||
{ | ||
if (!file_exists($this->resource)) | ||
{ | ||
return false; | ||
} | ||
|
||
if (null === $timestamp) | ||
{ | ||
$timestamp = time(); | ||
} | ||
|
||
return filemtime($this->resource) < $timestamp; | ||
} | ||
} |
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
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
38 changes: 38 additions & 0 deletions
38
src/Symfony/Components/DependencyInjection/ResourceInterface.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,38 @@ | ||
<?php | ||
|
||
namespace Symfony\Components\DependencyInjection; | ||
|
||
/* | ||
* This file is part of the symfony framework. | ||
* | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* ResourceInterface is the interface that must be implemented by all Resource classes. | ||
* | ||
* @package symfony | ||
* @subpackage dependency_injection | ||
* @author Fabien Potencier <fabien.potencier@symfony-project.com> | ||
*/ | ||
interface ResourceInterface | ||
{ | ||
/** | ||
* Returns true if the resource has not been updated since the given timestamp. | ||
* | ||
* @param timestamp $timestamp The last time the resource was loaded | ||
* | ||
* @return Boolean true if the resource has not been updated, false otherwise | ||
*/ | ||
function isUptodate($timestamp = null); | ||
|
||
/** | ||
* Returns the resource tied to this Resource. | ||
* | ||
* @return mixed The resource | ||
*/ | ||
function getResource(); | ||
} |
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
33 changes: 33 additions & 0 deletions
33
tests/unit/Symfony/Components/DependencyInjection/FileResourceTest.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,33 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the symfony package. | ||
* (c) Fabien Potencier <fabien.potencier@symfony-project.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
require_once __DIR__.'/../../../bootstrap.php'; | ||
|
||
use Symfony\Components\DependencyInjection\FileResource; | ||
|
||
$t = new LimeTest(5); | ||
|
||
// ->getResource() | ||
$t->diag('->getResource()'); | ||
$file = sys_get_temp_dir().'/tmp.xml'; | ||
touch($file); | ||
$resource = new FileResource($file); | ||
$t->is($resource->getResource(), $file, '->getResource() returns the path to the resource'); | ||
|
||
// ->isUptodate() | ||
$t->diag('->isUptodate()'); | ||
sleep(1); | ||
$t->ok($resource->isUptodate(), '->isUptodate() returns true if the resource has not changed'); | ||
$t->ok($resource->isUptodate(time() + 10), '->isUptodate() returns true if the resource has not changed'); | ||
$t->ok(!$resource->isUptodate(time() - 86400), '->isUptodate() returns false if the resource has been updated'); | ||
unlink($file); | ||
|
||
$resource = new FileResource('/____foo/foobar'.rand(1, 999999)); | ||
$t->ok(!$resource->isUptodate(), '->isUptodate() returns false if the resource does not exist'); |
Oops, something went wrong.