Как можно получить дерево каталого для вывода с помощью SPL PHP?

Всем привет. Нужно вывести дерево каталогов (можно с файлами) + подкатологи, ну в общем, чтобы я мог в дальнейшем нажать на каталог, он раскрылся с помощью js, я увидел его подкаталоги и файлы и т.д. Хотелось бы, применяя SPL, но что-то идей нет пока. Help please :))

Всё, что написал пока что
class DirTree
{
	private $items = [];
	private $output = '';

	public function __construct($path)
	{
		$this->createDirTree($path, $this->items);
		$this->createViewTreeDir($this->output);
	}

	private function createDirTree($path, &$items)
	{
		$files = [];
		$directoryIterator = new \DirectoryIterator($path);
		foreach ($directoryIterator as $key => $value) {
			if ($value->isFile()) {
				$files[] = $value->getPathname();
			} elseif ($value->isDir() && !$value->isDot()) {
				$items[$value->getPathname()] = [];
				$this->createDirTree($value->getPathname(), $items[$value->getPathname()]);
			}
		}
		if (!empty($files)) {
			foreach ($files as $file) {
				$items[$file] = null;
			}
		}
	}

	public function getDirTree()
	{
		return $this->items;
	}

	public function isEmpty()
	{
		return !(bool)count($this->items);
	}

	private function createViewTreeDir(&$output, $items = [])
	{
		if ($this->isEmpty()) return false;

		if (empty($items)) $items = $this->items;

		$output .= '<ul class="dir-tree" id="dirTree">';
			foreach ($items as $key => $value) {
				$itemInfo = new \SplFileInfo($key);
				if (is_array($value) && !empty($value)) {
					$output .= '<li class="dir-tree__item dir-tree__item_dir" data-path="' . $key . '" data-type="dir"><span>' . $itemInfo->getBasename() . '</span>';
						$this->createViewTreeDir($this->output, $value);
					$output .= '</li>';
				} elseif (is_null($value)) {
					$output .= '<li class="dir-tree__item dir-tree__item_file" data-path="' . $key . '" data-type="file">' . $itemInfo->getBasename() . '</li>';
				}
			}
		$output .= '</ul>';
	}

	public function getViewDirTree()
	{
		return $this->output;
	}

	public function __toString()
	{
		return $this->getViewDirTree();
	}
}

$dirTree = new DirTree(__DIR__);
print_r($dirTree->getDirTree());
var_dump($dirTree->isEmpty());
echo $dirTree->getViewDirTree();
echo $dirTree;
  • Вопрос задан
  • 152 просмотра
Пригласить эксперта
Ответы на вопрос 2
@egormmm
Борітеся — поборете!
@seftomsk Автор вопроса
Накидал тупой код
echo '<pre>';
$path = __DIR__;
function createDir2($path)
{
	$directoryIterator = new DirectoryIterator($path);
	$files = [];
	echo '<ul>';
	foreach ($directoryIterator as $key => $item) {
		if ($item->isDir() && !$item->isDot()) {
			echo '<li>' . $item;
			createDir2($item->getPathname());
			//echo $item->getPathname() . "\n";
			echo '</li>';
		} elseif ($item->isFile()) {
			$files[] = $item->getFilename();
		}
	}
	if (!empty($files)) {
		printFiles($files);
	}
	echo '</ul>';
}
function printFiles($files)
{
	foreach ($files as $file) {
		echo '<li>' . $file . '</li>';
	}
}
createDir2($path);


Но это не совсем то, что мне нужно, во-первых, мне кажется, если использовать RecursiveDirectoryIterator, то это можно сократить + хотел бы на выход получать чисто массив, но пока нет идей ни для использования Recursive, ни для хранения этих данных
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы