Skip to content

Commit c1ae90b

Browse files
author
Corey Ballou
committed
Initial commit of all SPL examples.
0 parents  commit c1ae90b

34 files changed

+2191
-0
lines changed

README.md

Whitespace-only changes.

caching-iterator.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
/**
3+
* An example of using the caching iterator to perform a look-ahead for the last element
4+
* in a single dimension "navigation" array so we can accurately set classes for "last".
5+
*
6+
* Please note:
7+
* No safety measures have been taken to sanitize the output.
8+
*
9+
* @author Corey Ballou
10+
*/
11+
12+
// example navigation array
13+
$nav = array(
14+
'Home' => '/home',
15+
'Products' => '/products',
16+
'Company' => '/company',
17+
'Privacy Policy' => '/privacy-policy'
18+
);
19+
20+
// storage of output
21+
$output = new ArrayIterator();
22+
23+
try {
24+
25+
// create the caching iterator of the nav array
26+
$it = new CachingIterator(new ArrayIterator($nav));
27+
foreach ($it as $name => $url) {
28+
if ($it->hasNext()) {
29+
$output->append('<li><a href="' . $url . '">' . $name . '</a></li>');
30+
} else {
31+
$output->append('<li class="last"><a href="' . $url . '">' . $name . '</a></li>');
32+
}
33+
}
34+
35+
// if we have values, output the unordered list
36+
if ($output->count()) {
37+
echo '<ul id="nav">' . "\n" . implode("\n", (array) $output) . "\n" . '</ul>';
38+
}
39+
40+
} catch (Exception $e) {
41+
die($e->getMessage());
42+
}
43+
44+
/**
45+
* Below is the same example, but prettified in a nice, extensible class
46+
* allowing you to reuse it for nav, subnav, or any time you need to
47+
* determine the last element of an array.
48+
*/
49+
class NavBuilder extends CachingIterator {
50+
51+
/**
52+
* Override the current() method to modify the return value
53+
* for the given index.
54+
*
55+
* @access public
56+
* @return string
57+
*/
58+
public function current()
59+
{
60+
// get the name and url of the nav item
61+
$name = parent::key();
62+
$url = parent::current();
63+
64+
// determine if we're on the last element
65+
if ($this->hasNext()) {
66+
return '<li><a href="' . $url . '">' . $name . '</a></li>';
67+
} else {
68+
return '<li class="last"><a href="' . $url . '">' . $name . '</a></li>';
69+
}
70+
}
71+
72+
/**
73+
* Outputs the navigation.
74+
*/
75+
public function generate()
76+
{
77+
$inner = $this->getInnerIterator();
78+
var_dump(get_class_methods($inner));
79+
}
80+
81+
}
82+
83+
try {
84+
$it = new NavBuilder(new ArrayIterator($nav));
85+
echo $it->generate();
86+
} catch (Exception $e) {
87+
var_dump($e); die;
88+
}

directory-iterator-example/directory-iterator-recursive/file1.txt

Whitespace-only changes.

directory-iterator-example/directory-iterator-recursive/file2

Whitespace-only changes.

directory-iterator-example/directory-iterator-recursive/file3

Whitespace-only changes.

directory-iterator-example/directory-iterator-recursive/hello-world

Whitespace-only changes.

directory-iterator-example/file1.txt

Whitespace-only changes.

directory-iterator-example/file2.php

Whitespace-only changes.

directory-iterator-example/file2.txt

Whitespace-only changes.

directory-iterator-example/file3

Whitespace-only changes.

directory-iterator-example/file4.txt

Whitespace-only changes.

directory-iterator-example/file5

Whitespace-only changes.

directory-iterator-example/second-directory/newfile.php

Whitespace-only changes.

directory-iterator-example/second-directory/third-directory/you_found_me

Whitespace-only changes.

directory-iterator-example/test.php

Whitespace-only changes.

directory-iterator-pt2.php

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
<?php
2+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator' . DIRECTORY_SEPARATOR . 'filter-dots.php');
3+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator' . DIRECTORY_SEPARATOR . 'filter-extension.php');
4+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator' . DIRECTORY_SEPARATOR . 'filter-key.php');
5+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator' . DIRECTORY_SEPARATOR . 'directory-tree.php');
6+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator' . DIRECTORY_SEPARATOR . 'directory-graph.php');
7+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator' . DIRECTORY_SEPARATOR . 'directory-match.php');
8+
9+
/**
10+
* This class contains example usage for the entire directory-iterator sub
11+
* directory.
12+
*
13+
* http://www.phpclasses.org/package/4389-PHP-Retrieve-directory-listings-with-SPL-iterators.html
14+
*
15+
* @author Paul Scott <pscott@uwc.ac.za>
16+
*/
17+
class DirectoryUsage
18+
{
19+
/**
20+
* Recursively list the contents of a directory. Second parameter allows you
21+
* to specify whether you'd like to only return a list of directories or files.
22+
*
23+
* @param string $dir
24+
* @param string $type ['file'|'dir']
25+
* @return array
26+
*/
27+
public function dirListByType($dir, $type = 'file')
28+
{
29+
$output = array();
30+
31+
$it = new RecursiveDirectoryIterator($dir);
32+
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
33+
foreach ($it as $file) {
34+
if ($file->getType() == $type) {
35+
$output[] = $file->getPathname();
36+
}
37+
}
38+
39+
return $output;
40+
}
41+
42+
/**
43+
* Example of using SPL to clean up directories by removing specific filenames.
44+
*
45+
* @access public
46+
* @param string $directory
47+
* @param array $filter
48+
*/
49+
public function cleanDir($directory, $filter = array('_vti_cnf', '_vti_private', '_vti_txt', '_private', '_themes', 'msupdate', 'vti_pvt', 'vti_script', '_vti_log', '_template','Thumbs.db'))
50+
{
51+
$it = new RecursiveDirectoryIterator($directory);
52+
$it = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::CHILD_FIRST);
53+
foreach ($it as $file) {
54+
55+
// remove empty dirs
56+
if (sizeof($file->getSize()) == 0) {
57+
unlink($file->getPath());
58+
}
59+
60+
// remove instances of Thumbs.db
61+
if ($file->getFileName() == 'Thumbs.db') {
62+
unlink($file->getPath() . DIRECTORY_SEPARATOR . $file->getFilename());
63+
}
64+
65+
// if paths match filter, delete directory recursively
66+
$parts = explode(DIRECTORY_SEPARATOR, $file->getPath());
67+
if(in_array(end($parts), $filter)) {
68+
$this->deleteDir($file->getPath());
69+
}
70+
71+
}
72+
}
73+
74+
/**
75+
* Method to get information about all the files in a directory (recursive)
76+
*
77+
* @param string $directory
78+
* @param array $filter
79+
* @return array
80+
*/
81+
public function fileInfo($directory, $filter = array('php', 'xsl', 'xml', 'htm', 'html','css'))
82+
{
83+
$count_directories = 0;
84+
$count_files = 0;
85+
$count_lines = 0;
86+
$count_bytes = 0;
87+
88+
$it = new RecursiveDirectoryIterator($directory);
89+
$it = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST);
90+
91+
foreach ($it as $file) {
92+
if (false === $file->isDir()) {
93+
// get the file extension
94+
$ext = $file->getExtension();
95+
if (in_array($ext, $filter)) {
96+
$count_files++;
97+
$count_bytes += $file->getSize();
98+
$count_lines += sizeof(explode("n", file_get_contents($file->getPathName())));
99+
}
100+
} else if(false === strpos($file->getPathname(), 'CVS') && $file->isDir()) {
101+
$count_directories++;
102+
}
103+
}
104+
105+
return array(
106+
'bytes' => $count_bytes,
107+
'files' => $count_files,
108+
'lines' => $count_lines,
109+
'directories' => $count_directories
110+
);
111+
}
112+
113+
/**
114+
* Recursively delete a directory and all subdirectories.
115+
*
116+
* @access public
117+
* @param string $dir
118+
* @return void
119+
*/
120+
public function deleteDir($dir)
121+
{
122+
$it = new RecursiveDirectoryIterator($dir);
123+
$it = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST);
124+
foreach ($it as $file) {
125+
if ($file->isDir()) {
126+
rmdir($file->getPathname());
127+
} else {
128+
unlink($file->getPathname());
129+
@rmdir($dir);
130+
}
131+
}
132+
@rmdir($dir);
133+
}
134+
135+
/**
136+
* Find a file by regex in a given directory.
137+
*
138+
* @param string $path
139+
* @param string $regex
140+
* @return array
141+
*/
142+
public function fileFinder($path, $regex)
143+
{
144+
$matches = array();
145+
146+
$fileList = new DirMatch($path, $regex);
147+
foreach ($fileList as $file) {
148+
$matches[] = $file;
149+
}
150+
151+
return $matches;
152+
}
153+
154+
/**
155+
* List files in a given directory.
156+
*
157+
* @param string $dir
158+
* @return array
159+
*/
160+
public function fileLister($dir)
161+
{
162+
$files = array();
163+
164+
$filtered = new DirectoryFilterDots($dir);
165+
foreach ($filtered as $file) {
166+
if ($file->isDir()) {
167+
continue;
168+
}
169+
$files[] = $file->getFilename();
170+
}
171+
172+
return $files;
173+
}
174+
175+
}
176+
177+
// generate the directory path to the example dir
178+
$dir = __DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator-example';
179+
180+
// load up the class
181+
$DirectoryUsage = new DirectoryUsage();
182+
183+
echo '==================================' . PHP_EOL;
184+
echo 'Recursively show all files in a directory.' . PHP_EOL;
185+
echo '==================================' . PHP_EOL;
186+
187+
$files = $DirectoryUsage->dirListByType($dir, 'file');
188+
foreach ($files as $f) {
189+
echo $f . PHP_EOL;
190+
}
191+
192+
echo '==================================' . PHP_EOL;
193+
echo 'Recursively show all directories in a directory.' . PHP_EOL;
194+
echo '==================================' . PHP_EOL;
195+
196+
$dirs = $DirectoryUsage->dirListByType($dir, 'dir');
197+
foreach ($dirs as $d) {
198+
echo $d . PHP_EOL;
199+
}
200+
201+
echo '==================================' . PHP_EOL;
202+
echo 'Recursively iterate over all files in a directory.' . PHP_EOL;
203+
echo '==================================' . PHP_EOL;
204+
205+
// recursively generate a tree representation
206+
$files = new DirectoryTreeIterator($dir);
207+
foreach ($files as $f) {
208+
echo $f . PHP_EOL;
209+
}
210+
211+
echo '==================================' . PHP_EOL;
212+
echo 'Iterate over a all files in a directory, filtering out dots.' . PHP_EOL;
213+
echo '==================================' . PHP_EOL;
214+
215+
// recursively generate a tree representation
216+
$files = new DirectoryFilterDots($dir);
217+
foreach ($files as $f) {
218+
echo $f . PHP_EOL;
219+
}
220+
221+
echo '==================================' . PHP_EOL;
222+
echo 'Find all files with a PHP extension.' . PHP_EOL;
223+
echo '==================================' . PHP_EOL;
224+
225+
// filter by PHP file extension
226+
$phpFiles = new ExtensionFilter(new DirectoryIterator($dir), 'php', $whitelist=true);
227+
foreach ($phpFiles as $f) {
228+
echo $f->getPathName() . PHP_EOL;
229+
}
230+
231+
echo '==================================' . PHP_EOL;
232+
echo 'Find all files without a PHP extension.' . PHP_EOL;
233+
echo '==================================' . PHP_EOL;
234+
235+
// filter by PHP file extension
236+
$phpFiles = new ExtensionFilter(new DirectoryIterator($dir), 'php', $whitelist=false);
237+
foreach ($phpFiles as $f) {
238+
echo $f->getPathName() . PHP_EOL;
239+
}

directory-iterator.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* The below example is the simplest case of iterating over all direct children of a directory,
4+
* without recursing into sub-directories. The "." and ".." references are skipped.
5+
*/
6+
7+
// iterate over all files from the child directory "directory-iterator-example"
8+
$files = new DirectoryIterator(__DIR__ . DIRECTORY_SEPARATOR . 'directory-iterator-example');
9+
foreach ($files as $file) {
10+
// skip over dots ("." and "..")
11+
if (!$file->isDot()) {
12+
// example of the
13+
echo $file->getRealPath() . PHP_EOL;
14+
}
15+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Handle matching and filtering directories by a regular expression.
4+
*/
5+
class DirectoryMatch extends KeyFilter
6+
{
7+
public function __construct($path , $regex)
8+
{
9+
parent::__construct(new DirTreeIterator($path), $regex);
10+
}
11+
12+
/**
13+
* Override the current element to simply return the key.
14+
*
15+
* @access public
16+
* @return string
17+
*/
18+
public function current()
19+
{
20+
return parent::key();
21+
}
22+
23+
/**
24+
* Override the key element to simply return the key.
25+
*
26+
* @access public
27+
* @return string
28+
*/
29+
public function key()
30+
{
31+
return parent::key();
32+
}
33+
}

0 commit comments

Comments
 (0)