mirror of
https://github.com/php/php-src.git
synced 2025-08-18 06:58:55 +02:00
More examples
This commit is contained in:
parent
1131d43b0b
commit
49277b2ac6
3 changed files with 228 additions and 0 deletions
92
ext/spl/examples/filter.inc
Executable file
92
ext/spl/examples/filter.inc
Executable file
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @brief Regular expression filter for string iterators
|
||||
* @author Marcus Boerger
|
||||
* @version 1.0
|
||||
*
|
||||
* Instances of this class act as a filter around iterators whose elements
|
||||
* are strings. In other words you can put an iterator into the constructor
|
||||
* and the instance will only return elements which match the given regular
|
||||
* expression.
|
||||
*/
|
||||
class filter implements spl_forward
|
||||
{
|
||||
protected $it;
|
||||
protected $regex;
|
||||
protected $curr;
|
||||
|
||||
/**
|
||||
* Constructs a filter around an iterator whose elemnts are strings.
|
||||
* If the given iterator is of type spl_sequence then its rewind()
|
||||
* method is called.
|
||||
*
|
||||
* @param it Object that implements at least spl_forward
|
||||
* @patam regex Regular expression used as a filter.
|
||||
*/
|
||||
function __construct(spl_forward $it, $regex) {
|
||||
if ($it instanceof spl_sequence) {
|
||||
$it->rewind();
|
||||
}
|
||||
$this->it = $it;
|
||||
$this->regex = $regex;
|
||||
$this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* Destruct the iterator.
|
||||
*/
|
||||
function __destruct() {
|
||||
unset($this->it);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch next element and store it.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function fetch() {
|
||||
$this->curr = false;
|
||||
while ($this->it->has_more()) {
|
||||
$curr = $this->it->current();
|
||||
if (ereg($this->regex, $curr)) {
|
||||
$this->curr = $curr;
|
||||
return;
|
||||
}
|
||||
$this->it->next();
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Move to next element
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function next() {
|
||||
$this->it->next();
|
||||
$this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Whether more elements are available
|
||||
*/
|
||||
function has_more() {
|
||||
return $this->curr !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The current value
|
||||
*/
|
||||
function current() {
|
||||
return $this->curr;
|
||||
}
|
||||
|
||||
/**
|
||||
* hidden __clone
|
||||
*/
|
||||
protected function __clone() {
|
||||
// disallow clone
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
20
ext/spl/examples/filter_tree.php
Executable file
20
ext/spl/examples/filter_tree.php
Executable file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
/* tree view example
|
||||
*
|
||||
* Usage: php filter_tree.php <path> <regex>
|
||||
*
|
||||
* Simply specify the path to tree with parameter <path>.
|
||||
* The regular expression <regex> is used to filter the tree.
|
||||
*
|
||||
* (c) Marcus Boerger
|
||||
*/
|
||||
|
||||
require_once("sub_dir.inc");
|
||||
require_once("filter.inc");
|
||||
|
||||
foreach(new filter(new sub_dir($argv[1]), $argv[2]) as $f) {
|
||||
echo "$f\n";
|
||||
}
|
||||
|
||||
?>
|
116
ext/spl/examples/sub_dir.inc
Executable file
116
ext/spl/examples/sub_dir.inc
Executable file
|
@ -0,0 +1,116 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @brief Subdirectory aware directory iterator.
|
||||
* @author Marcus Boerger
|
||||
* @version 1.0
|
||||
*
|
||||
* This directory iterator recursively returns all files and directories
|
||||
* within a given path.
|
||||
*/
|
||||
class sub_dir implements spl_sequence
|
||||
{
|
||||
protected $adir = array();
|
||||
protected $cnt = 0;
|
||||
protected $path = "";
|
||||
protected $curr = "";
|
||||
protected $nodots = true;
|
||||
|
||||
/**
|
||||
* Construct a directory from a path.
|
||||
*
|
||||
* @param path The path to iterate.
|
||||
* @param nodots Whether or not to display the entries '.' and '..'.
|
||||
*/
|
||||
function __construct($path, $nodots = true) {
|
||||
$this->cnt = 0;
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Rewind the directory.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function rewind() {
|
||||
while($this->cnt) {
|
||||
unset($this->adir[$this->cnt--]);
|
||||
}
|
||||
$dir = new spl_dir($this->path);
|
||||
$dir->path = "";
|
||||
$this->adir[1] = $dir;
|
||||
$this->cnt = 1;
|
||||
if ($this->nodots) {
|
||||
while ($this->has_more()) {
|
||||
$ent = $this->current();
|
||||
if ($ent != '.' && $ent != '..') {
|
||||
break;
|
||||
}
|
||||
$this->next();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Move to net dir or file entry.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function next() {
|
||||
if ($this->cnt) {
|
||||
$dir = $this->adir[$this->cnt];
|
||||
$ent = $dir->current();
|
||||
$path = $dir->get_path().'/'.$ent;
|
||||
if ($ent != '.' && $ent != '..' && is_dir($path)) {
|
||||
$new = new spl_dir($path);
|
||||
$new->path = $dir->path.$ent.'/';
|
||||
$new->cnt = $this->cnt++;
|
||||
$this->adir[$this->cnt] = $new;
|
||||
if ($this->nodots) {
|
||||
while ($new->has_more()) {
|
||||
$ent = $new->current();
|
||||
if ($ent != '.' && $ent != '..') {
|
||||
break;
|
||||
}
|
||||
$new->next();
|
||||
}
|
||||
}
|
||||
}
|
||||
$dir->next();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Whether more dirs or files entries are available.
|
||||
*/
|
||||
function has_more() {
|
||||
while ($this->cnt) {
|
||||
$dir = $this->adir[$this->cnt];
|
||||
if ($dir->has_more()) {
|
||||
return true;
|
||||
}
|
||||
unset($this->adir[$this->cnt--]);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The current dir or file entry.
|
||||
*/
|
||||
function current() {
|
||||
if ($this->cnt) {
|
||||
$dir = $this->adir[$this->cnt];
|
||||
return $dir->path . $dir->current();
|
||||
}
|
||||
throw new exception("No more elements available");
|
||||
}
|
||||
|
||||
/**
|
||||
* Hidden __clone
|
||||
*/
|
||||
protected function __clone() {
|
||||
// disallow clone
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Add table
Add a link
Reference in a new issue