Используя шаблоны проектирования напишите код, который будет строить и выводить дерево (список) файлов и папок начиная с текущей, указывая при выводе класс (файл/папка) каждого объекта.
<?
/**
* Use pattern Сhain-of-command
*/
interface ICommand
{
function onCommand($name,$args);
}
class CommandChain
{
private $_commands = array();
public function addCommand($cmd) {
$this->_commands []= $cmd;
}
public function runCommand($name,$args) {
foreach($this->_commands as $command){
if($command->onCommand($name,$args))
return;
}
}
}
/**
* Displaying: class (path/directory)
*/
class DirectoryCommand implements ICommand
{
public function onCommand($name,$args){
if ( $name != 'directory' )
return false;
echo __CLASS__ . ' (' . $args. ')<br />';
return true;
}
}
/**
* Displaying: class (path/file)
*/
class FailCommand implements ICommand
{
public function onCommand($name,$args){
if ( $name != 'file' )
return false;
echo __CLASS__ . ' (' . $args. ')<br />';
return true;
}
}
class Trees{
public $structure;
public function __construct(){
$this->structure = new CommandChain();
$this->structure->addCommand( new DirectoryCommand() );
$this->structure->addCommand( new FailCommand() );
}
/**
* The output of the tree structure
* @param string $path
*/
public function tree($path = '.'){
$file = opendir($path);
while($files = readdir($file)){
if($files != "." && $files != ".."){
if(is_file($path.'/'.$files))
$this->structure->runCommand( 'file', $path.'/'.$files );
if(is_dir($path.'/'.$files)){
$this->structure->runCommand( 'directory', $path.'/'.$files );
$this->tree($path.'/'.$files);
}
}
}
closedir($file);
}
}
// The output of the tree structure
$treeStructure = new Trees();
$treeStructure->tree();
?>
|