1

私はディレクトリの任意のレベルからクラスを見つけるためにこの機能を持っています

function _findFile($path, $class) {
        $founded_file = "";
        $dir = scandir($path);
        foreach ($dir as $file) {
            $current_path = $path . $file;
//            echo $current_path . "\n";
            if ($file != "." && $file != "..") {
//                    echo $current_path . "\n";
                if (is_dir($current_path)) {
                    return $this->_findFile($current_path . "/", $class);
                } else if (is_file($current_path) && end(explode(".", $current_path)) === "php") {
                    if (end(explode("/", $current_path)) === ($class . ".php")) {
                        return $current_path;
                    }
                }
            }
        }

        return $founded_file;
    }

私のディレクトリ構造

system
  -base
     -core.php
     -exceptions.php
  -database
     -database.php

ファイルが見つかりませんsystem > database

最初のコメントのコメントを外すと、関数がsystem > databaseパスに入っていないことがわかります

疑問があれば聞いてください

4

2 に答える 2

0

おそらく、 RecursiveDirectoryIteratorFilterIteratorクラスを知りたいと思うでしょう。これらは、コードを大幅に簡素化するのに役立ちます。

<?php

class FileFilterIterator extends FilterIterator 
{
    private $filename;

    public function __construct(Iterator $iterator, $filename)
    {
        parent::__construct($iterator);
        $this->filename = $filename;
    }

    public function accept()
    {
        return ($this->getInnerIterator()->current()->getFilename() == $this->filename);
    }
}

function _findFile($path, $className)
{
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
    $files = array();

    foreach (new FileFilterIterator($iterator, "$className.php") as $file) {
        $files[] = $file->getPathname();
    }

    return $files;
}
于 2013-11-06T06:18:28.560 に答える
0

交換してみてください$this->_findFile($current_path . "/", $class)

$file = $this->_findFile($current_path . "/", $class);
if ($file) {
     return $file;
}
于 2013-11-06T06:11:55.297 に答える