Intersection of all directory subdirectories

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)); } 
+4
source share
2 answers

I think the code is optimized correctly.
I created a script that lists all the directories and subdirectories, and then those that don’t have the less directories are deleted, and creates a new array for those that have it.
Then I checked both yours and mine with a cycle of 1000 times. Your script took an average of 0.93 s , and my script took 1.27s . So, in my opinion, your code is fine.

0
source

I would say that if he is fast enough and does this work, I would say that there is no need to optimize further. If you reach the point that it is not fast enough or does not do the job, then turn it over. A recursive iterator is unlikely to greatly change your implementation.

Sorry, I could no longer help.

0
source

All Articles