SPL to dość fajne narzędzie do implementacji szczególnie iteracji w PHP, warte poznania. Za pmocą SPL’a da się
pewne uciążliwe funkcje napisać prościej. Poniżej przykład funkcji, która wypisuje drzewo katalogów:
class DirectoryAdapter extends RecursiveIteratorIterator
{
private $dirArray;
private $path;
public function __construct($path)
{
$this->path = $path;
parent::__construct(new RecursiveDirectoryIterator($path), parent::CHILD_FIRST);
}
public function getDirs()
{
foreach($this as $item)
{
if($item->isDir())
{
$current = count($this->dirArray);
$mtime = date("Y-m-d H:m", $this->getMTime());
$this->dirArray[$current]['mtime'] = $mtime;
$atime = date("Y-m-d H:m", $this->getATime());
$this->dirArray[$current]['atime'] = $atime;
$this->dirArray[$current]['dir'] = str_replace($this->path,'', $this->key());
}
}
return $this->dirArray;
}
public function deleteDir()
{
foreach ($this as $item)
{
if ($item->isDir())
{
rmdir($this->key());
}
else
{
unlink($this->key());
}
}
rmdir($this->path);
}
}
{
private $dirArray;
private $path;
public function __construct($path)
{
$this->path = $path;
parent::__construct(new RecursiveDirectoryIterator($path), parent::CHILD_FIRST);
}
public function getDirs()
{
foreach($this as $item)
{
if($item->isDir())
{
$current = count($this->dirArray);
$mtime = date("Y-m-d H:m", $this->getMTime());
$this->dirArray[$current]['mtime'] = $mtime;
$atime = date("Y-m-d H:m", $this->getATime());
$this->dirArray[$current]['atime'] = $atime;
$this->dirArray[$current]['dir'] = str_replace($this->path,'', $this->key());
}
}
return $this->dirArray;
}
public function deleteDir()
{
foreach ($this as $item)
{
if ($item->isDir())
{
rmdir($this->key());
}
else
{
unlink($this->key());
}
}
rmdir($this->path);
}
}
Wykorzystanie: