简体   繁体   English

循环遍历目录的所有子目录

[英]Looping over all subdirectories of a directory

I have a task that should be simple, 我的任务应该很简单,

  • Given a path, search all children (1 level deep) for a less folder. 给定一个路径,搜索所有子项(1级深)以获取less文件夹。
  • If the folder is found add the full path as a key to an array 如果找到该文件夹​​,请将完整路径添加为数组的键
  • Set the value for the key to be the same path, but replace less with css 将键的值设置为相同的路径,但用css替换为less
  • Inside the less directory recursively loop all child directories 在less目录内递归循环所有子目录
  • Add the sub directories in the same manner as the original directories 以与原始目录相同的方式添加子目录

So, given this structure 所以,鉴于这种结构

Note: All items below except for randomfile are directories 注意:除randomfile之外的所有项目都是目录

matthew@vaio:/var/www/constructor/public/bundles$ tree
.
├── first
│   └── less
│       ├── secondtester
│       └── tester
│           ├── anothersubtester
│           ├── randomfile
│           └── subtester
├── second
│   └── less
│       ├── secondtester
│       └── tester
│           ├── anothersubtester
│           ├── randomfile
│           └── subtester
└── third
    └── noless
        ├── secondtester
        └── tester
            ├── anothersubtester
            ├── randomfile
            └── subtester

18 directories, 3 files

I want to end up with this array (note I have truncated the path here just to make it easier to read) 我想最终得到这个数组(注意我已经截断了这里的路径只是为了让它更容易阅读)

Array
    (
    [/b/second/less] => /b/second/css
    [/b/second/less/secondtester] => /b/second/css/secondtester
    [/b/second/less/tester] => /b/second/css/tester
    [/b/second/less/tester/subtester] => /b/second/css/tester/subtester
    [/b/second/less/tester/anothersubtester] => /b/second/css/tester/anothersubtester
    [/b/first/less] => /b/first/css
    [/b/first/less/secondtester] => /b/first/css/secondtester
    [/b/first/less/tester] => /b/first/css/tester
    [/b/first/less/tester/subtester] => /b/first/css/tester/subtester
    [/b/first/less/tester/anothersubtester] => /b/first/css/tester/anothersubtester
)

Now I have the below code, but I don't think this is optimized at all, eg I know there are the RecursiveIteratorIterators etc, but I cannot work out how to use them for this task, so have had to resort to a recursive function that does the lifting. 现在我有下面的代码,但我认为这根本没有优化,例如我知道有RecursiveIteratorIterators等,但我无法弄清楚如何将它们用于此任务,因此不得不求助于递归函数提升。 Basically, I am wondering how I could write this to be optimized better: 基本上,我想知道如何写这个以更好地进行优化:

$directories = array();
$bundlePath = realpath('/public/bundles');

function lessSearcher($lessPath, $cssPath){
    $directories = array($lessPath => $cssPath);

    $lessDirs = new DirectoryIterator($lessPath);
    foreach ($lessDirs as $lessDir) {
        //we only want the directories and not the .'s
        if ($lessDir->isDot() || !$lessDir->isDir()) continue;
        $lessCurrent = $lessPath . '/' . $lessDir->getFileName();
        $cssCurrent = $cssPath . '/' . $lessDir->getFileName();
        $directories[$lessCurrent] = $cssCurrent;
        $directories = array_merge($directories, lessSearcher($lessCurrent, $cssCurrent));
    }

    return $directories;
}

$bundles = new DirectoryIterator($bundlePath);
foreach ($bundles as $bundle) {
    //we only want the directories and not the .'s
    if($bundle->isDot() || !$bundle->isDir()) continue;
    //we only want the directories that have a less directory
    if(!realpath($bundlePath.'/'.$bundle->getFileName().'/less')) continue;

    $lessPath = realpath($bundlePath . '/' . $bundle->getFileName()) . '/less';
    $cssPath = realpath($bundlePath . '/' . $bundle->getFileName()) . '/css';

    $directories = array_merge($directories, lessSearcher($lessPath, $cssPath));
}

I think the code is correctly optimized. 我认为代码已正确优化。
I made a script that lists all the directories and sub-directories and then removes the ones that doesn't have the 'less' directory and creates a new array for the ones that do have it. 我创建了一个列出所有目录和子目录的脚本,然后删除那些没有'less'目录的脚本,并为拥有它的那些创建一个新数组。
Then I tested both yours and mine with a loop of 1000 times. 然后我用1000次循环测试你和我的。 Your script took an average of 0.93s and my script took 1.27s . 你的脚本平均花了0.93秒 ,我的脚本花了1.27秒 So in my opinion, your code is okay. 所以在我看来,你的代码还可以。

I'd have to say that if its fast enough and does the job then I'd say there's no need to optimize further. 我不得不说,如果它足够快并完成工作,那么我会说没有必要进一步优化。 If you reach a point that it isn't fast enough or doesn't do the job then revist it then. 如果你达到一个不够快或不能完成工作的点,那么就将其复活。 The recursive iterator isn't likely to do much different to your implementation at any rate. 无论如何,递归迭代器不太可能与您的实现有很大不同。

Sorry I couldn't help more. 对不起,我忍不住多了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM