-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Config] additions from ResourceWatcher #4619
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
45df2e6
[Config] updated resources API to be more explicit
everzet 6b39688
[Config] moved DirectoryResource childs retrieving to the special get…
everzet 1f9ba38
[Config] getFilteredChildResources() method added to DirectoryResource
everzet 45a45ba
[Config] updated DirectoryResource tests
everzet 9fe0d00
[Config] update FileResourceTest
everzet d7c24eb
[Config] added new methods and their tests to File and Directory reso…
everzet c9eaa72
[Config] made ResourceInterface extends Serializable
fabpot ece489f
[Config] skip dots in getFilteredChilds() (fixes test suite on Linux)
everzet ff9c132
[Config] added type prefixes to resource ids
everzet 56b60c8
[Config] use is_file in FileResource::exists()
everzet 241aa92
[Config] added existence check to some resource methods
everzet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class DirectoryResource implements ResourceInterface, \Serializable | ||
class DirectoryResource implements ResourceInterface | ||
{ | ||
private $resource; | ||
private $pattern; | ||
|
@@ -33,6 +33,82 @@ public function __construct($resource, $pattern = null) | |
$this->pattern = $pattern; | ||
} | ||
|
||
/** | ||
* Returns the list of filtered file and directory childs of directory resource. | ||
* | ||
* @return array An array of files | ||
*/ | ||
public function getFilteredChilds() | ||
{ | ||
if (!$this->exists()) { | ||
return array(); | ||
} | ||
|
||
$iterator = new \RecursiveIteratorIterator( | ||
new \RecursiveDirectoryIterator($this->resource, \FilesystemIterator::SKIP_DOTS), | ||
\RecursiveIteratorIterator::SELF_FIRST | ||
); | ||
|
||
$childs = array(); | ||
foreach ($iterator as $file) { | ||
// if regex filtering is enabled only return matching files | ||
if ($file->isFile() && !$this->hasFile($file)) { | ||
continue; | ||
} | ||
|
||
// always monitor directories for changes, except the .. entries | ||
// (otherwise deleted files wouldn't get detected) | ||
if ($file->isDir() && '/..' === substr($file, -3)) { | ||
continue; | ||
} | ||
|
||
$childs[] = $file; | ||
} | ||
|
||
return $childs; | ||
} | ||
|
||
/** | ||
* Returns child resources that matches directory filters. | ||
* | ||
* @return array | ||
*/ | ||
public function getFilteredResources() | ||
{ | ||
if (!$this->exists()) { | ||
return array(); | ||
} | ||
|
||
$iterator = new \DirectoryIterator($this->resource); | ||
|
||
$resources = array(); | ||
foreach ($iterator as $file) { | ||
// if regex filtering is enabled only return matching files | ||
if ($file->isFile() && !$this->hasFile($file)) { | ||
continue; | ||
} | ||
|
||
// always monitor directories for changes, except the .. entries | ||
// (otherwise deleted files wouldn't get detected) | ||
if ($file->isDir() && '/..' === substr($file, -3)) { | ||
continue; | ||
} | ||
|
||
// if file is dot - continue | ||
if ($file->isDot()) { | ||
continue; | ||
} | ||
|
||
if ($file->isFile()) { | ||
$resources[] = new FileResource($file->getRealPath()); | ||
} elseif ($file->isDir()) { | ||
$resources[] = new DirectoryResource($file->getRealPath()); | ||
} | ||
} | ||
|
||
return $resources; | ||
} | ||
|
||
/** | ||
* Returns a string representation of the Resource. | ||
* | ||
|
@@ -53,11 +129,62 @@ public function getResource() | |
return $this->resource; | ||
} | ||
|
||
/** | ||
* Returns check pattern. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getPattern() | ||
{ | ||
return $this->pattern; | ||
} | ||
|
||
/** | ||
* Checks that passed file exists in resource and matches resource filters. | ||
* | ||
* @param SplFileInfo|string $file | ||
* | ||
* @return Boolean | ||
*/ | ||
public function hasFile($file) | ||
{ | ||
if (!$file instanceof \SplFileInfo) { | ||
$file = new \SplFileInfo($file); | ||
} | ||
|
||
if (0 !== strpos($file->getRealPath(), realpath($this->resource))) { | ||
return false; | ||
} | ||
|
||
if ($this->pattern) { | ||
return (bool) preg_match($this->pattern, $file->getBasename()); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Returns resource mtime. | ||
* | ||
* @return integer | ||
*/ | ||
public function getModificationTime() | ||
{ | ||
if (!$this->exists()) { | ||
return -1; | ||
} | ||
|
||
clearstatcache(true, $this->resource); | ||
$newestMTime = filemtime($this->resource); | ||
|
||
foreach ($this->getFilteredChilds() as $file) { | ||
clearstatcache(true, (string) $file); | ||
$newestMTime = max($file->getMTime(), $newestMTime); | ||
} | ||
|
||
return $newestMTime; | ||
} | ||
|
||
/** | ||
* Returns true if the resource has not been updated since the given timestamp. | ||
* | ||
|
@@ -67,27 +194,31 @@ public function getPattern() | |
*/ | ||
public function isFresh($timestamp) | ||
{ | ||
if (!is_dir($this->resource)) { | ||
if (!$this->exists()) { | ||
return false; | ||
} | ||
|
||
$newestMTime = filemtime($this->resource); | ||
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->resource), \RecursiveIteratorIterator::SELF_FIRST) as $file) { | ||
// if regex filtering is enabled only check matching files | ||
if ($this->pattern && $file->isFile() && !preg_match($this->pattern, $file->getBasename())) { | ||
continue; | ||
} | ||
|
||
// always monitor directories for changes, except the .. entries | ||
// (otherwise deleted files wouldn't get detected) | ||
if ($file->isDir() && '/..' === substr($file, -3)) { | ||
continue; | ||
} | ||
return $this->getModificationTime() < $timestamp; | ||
} | ||
|
||
$newestMTime = max($file->getMTime(), $newestMTime); | ||
} | ||
/** | ||
* Returns true if the resource exists in the filesystem. | ||
* | ||
* @return Boolean | ||
*/ | ||
public function exists() | ||
{ | ||
return is_dir($this->resource); | ||
} | ||
|
||
return $newestMTime < $timestamp; | ||
/** | ||
* Returns unique resource ID. | ||
* | ||
* @return string | ||
*/ | ||
public function getId() | ||
{ | ||
return md5('d'.$this->resource.$this->pattern); | ||
} | ||
|
||
public function serialize() | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
class FileResource implements ResourceInterface, \Serializable | ||
class FileResource implements ResourceInterface | ||
{ | ||
private $resource; | ||
|
||
|
@@ -29,7 +29,7 @@ class FileResource implements ResourceInterface, \Serializable | |
*/ | ||
public function __construct($resource) | ||
{ | ||
$this->resource = realpath($resource); | ||
$this->resource = file_exists($resource) ? realpath($resource) : $resource; | ||
} | ||
|
||
/** | ||
|
@@ -52,6 +52,22 @@ public function getResource() | |
return $this->resource; | ||
} | ||
|
||
/** | ||
* Returns resource mtime. | ||
* | ||
* @return integer | ||
*/ | ||
public function getModificationTime() | ||
{ | ||
if (!$this->exists()) { | ||
return -1; | ||
} | ||
|
||
clearstatcache(true, $this->resource); | ||
|
||
return filemtime($this->resource); | ||
} | ||
|
||
/** | ||
* Returns true if the resource has not been updated since the given timestamp. | ||
* | ||
|
@@ -61,11 +77,31 @@ public function getResource() | |
*/ | ||
public function isFresh($timestamp) | ||
{ | ||
if (!file_exists($this->resource)) { | ||
if (!$this->exists()) { | ||
return false; | ||
} | ||
|
||
return filemtime($this->resource) < $timestamp; | ||
return $this->getModificationTime() <= $timestamp; | ||
} | ||
|
||
/** | ||
* Returns true if the resource exists in the filesystem. | ||
* | ||
* @return Boolean | ||
*/ | ||
public function exists() | ||
{ | ||
return is_file($this->resource); | ||
} | ||
|
||
/** | ||
* Returns unique resource ID. | ||
* | ||
* @return string | ||
*/ | ||
public function getId() | ||
{ | ||
return md5('f'.$this->resource); | ||
} | ||
|
||
public function serialize() | ||
|
This file contains hidden or 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 |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
* | ||
* @author Fabien Potencier <[email protected]> | ||
*/ | ||
interface ResourceInterface | ||
interface ResourceInterface extends \Serializable | ||
{ | ||
/** | ||
* Returns a string representation of the Resource. | ||
|
@@ -34,10 +34,31 @@ function __toString(); | |
*/ | ||
function isFresh($timestamp); | ||
|
||
/** | ||
* Returns resource mtime. | ||
* | ||
* @return integer | ||
*/ | ||
function getModificationTime(); | ||
|
||
/** | ||
* Returns true if the resource exists in the filesystem. | ||
* | ||
* @return Boolean | ||
*/ | ||
function exists(); | ||
|
||
/** | ||
* Returns the resource tied to this Resource. | ||
* | ||
* @return mixed The resource | ||
*/ | ||
function getResource(); | ||
|
||
/** | ||
* Returns unique resource ID. | ||
* | ||
* @return string | ||
*/ | ||
function getId(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ return should be FilesystemIterator[] as its an array of FilesystemIterator objects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sstok no, it's an array in any case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, wait. You mean using
FilesystemIterator[]
as description toarray
ofFilesystemIterator
? This sounds even crazier than old good docblocks now :) @fabpot @stof what do you think, guys? I'm not sure we stick to this@... ...[]
convention in Symfony2, do we?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a large number of Components that us it :) Templating, Translator, Config, Console, DepencyInjection, Routing, Security.
Use this regex
@(return|param)\s+[\w\d]+\[\]
to search there usage.