I have a task that should be simple,
- Set the path, find all the children (level 1 depth) for the
less folder. - If the folder is found, add the full path as the key to the array
- Set the value for the same key, but replace
less with css - Inside the less directory, all child directories loop recursively
- Add auxiliary directories in the same way as source directories
So, given this structure
Note. All items except a random file are directories
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 that I truncated the path here 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 code below, but I donβt think it is optimized at all, for example. I know that there are RecursiveIteratorIterators , etc., but I canβt figure out how to use them for this task, so I had to resort to a recursive function that does the lifting. Basically, I wonder how I could write that this would be better optimized:
$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)); }
source share