HomePHP Page 4 - Working with Directory Iterators and Proxy Classes with PHP 5
Completing the proxy pattern - PHP
If you’re a strong advocate of using pattern-based programming for developing your PHP applications, then this pair of comprehensive tutorials might find a place on your quick reference list. Welcome to the concluding part of the series “Building Proxy Classes with PHP 5.” In two parts, this series introduces the foundations of how to apply the proxy pattern with PHP 5, and it accompanies the corresponding theory with extensive code samples.
As I stated in the section that you just read, in order to complete the programmatic model imposed by the proxy pattern, I need to define the signature that corresponds to the "DirectoryProcessor" class. As you just learned, this class is used by the aforementioned proxy.
Since my intention here is to demonstrate how a proxy class can be coupled with a directory iterator, the directory process that I plan to build will consist of a simple wrapper for the "DirectoryIterator" class that comes with PHP 5. It's just as simple as it sounds.
Having said that, here is the definition for this brand new class:
// define 'DirectoryProcessor' class
class DirectoryProcessor{
private $dirProcessor;
public function __construct($dirPath){
if(!$this->dirProcessor=new
DirectoryIterator($dirPath)){
throw new Exception('Error instantiating
directory processor object');
}
}
// get path of selected directory
public function getPath(){
return $this->dirProcessor->getPath();
}
// get size of directory entries
public function getSize(){
$output='Sizes for directory entries are as
following:<br />';
foreach($this->dirProcessor as $dirElement){
$output.='Size for current element
is :'.$dirElement->getSize().'<br />';
}
return $output;
}
// get names of directory entries
public function getFileNames(){
$output='Names for directory entries are as
following:<br />';
foreach($this->dirProcessor as $dirElement){
if($dirElement->isFile()){
$output.='Name of current file
is : '.$dirElement->getFileName().'<br />';
}
}
return $output;
}
// get timestamps of directory entries
public function getTimeStamps(){
$output='Timestamps for directory entries are as
following:<br />';
foreach($this->dirProcessor as $dirElement){
if($dirElement->isFile()){
$output.='Timestamp of current file
is : '.$dirElement->getMTime().'<br />';
}
}
return $output;
}
// get last access timestamps of directory entries
public function getLastTimeStamps(){
$output='Last access timestamps for directory entries
are as following:<br />';
foreach($this->dirProcessor as $dirElement){
if($dirElement->isFile()){
$output.='Last access timestamp of current
file is : '.$dirElement->getATime().'<br />';
}
}
return $output;
}
// get last access timestamps of inodes
public function getInodeLastTimeStamps(){
$output='Last inode modification timestamp for
directory entries are as following:<br />';
foreach($this->dirProcessor as $dirElement){
if($dirElement->isFile()){
$output.='Last inode modification timestamp of current
file is :'.$dirElement->getCTime().'<br />';
}
}
return $output;
}
// get directory processor object
public function getDirectoryProcessor(){
return $this;
}
}
Now, definitely things are getting really interesting! As you can see, the above "DirectoryProcessor" class behaves like a simple wrapping layer for the popular "DirectoryIterator" class that you've used many times with your PHP 5-based applications. In this case in particular, all the functionality provided by the original proxy class is implemented concretely by the corresponding iterator.
Now that you know how the previous directory processor class looks, it's time to jump forward and tackle the final section of the article, where I'll be demonstrating how the pair of neat classes that I created can be integrated in a single example.
Provided that you're interested in learning how these classes fit each other, go ahead and read the next few lines. I'll be there, waiting for you.