Vube\VagrantCatalog\DirectoryScan::countMetadataChildren PHP Method

countMetadataChildren() public method

public countMetadataChildren ( $dir, $depth )
    public function countMetadataChildren($dir, $depth = 0)
    {
        $dh = opendir($dir);
        if (!$dh) {
            throw new Exception("Cannot read directory: " . $this->dir);
        }
        $n = 0;
        while ($file = readdir($dh)) {
            // If we should ignore this file, do so
            if (in_array($file, $this->ignoreList)) {
                continue;
            }
            // If it's a directory, count it
            $path = $dir . '/' . $file;
            if (is_dir($path)) {
                $n += $this->countMetadataChildren($path, $depth + 1);
            } else {
                if ($file == 'metadata.json' && $depth > 0) {
                    $n++;
                }
            }
        }
        closedir($dh);
        return $n;
    }

Usage Example

 public function testCountMetadataChildren()
 {
     $tests = array('docroot' => 2, 'docroot/non-empty' => 1, 'docroot/non-empty/non-empty' => 0, 'docroot/non-empty/non-empty/empty' => 0, 'docroot/empty' => 0, 'docroot/empty/empty' => 0, 'docroot/empty/empty/empty' => 0);
     foreach ($tests as $scanDir => $expectedMetadataNodes) {
         $dir = vfsStream::url("root/{$scanDir}");
         $scanner = new DirectoryScan($dir);
         $actualMetadataNodes = $scanner->countMetadataChildren($dir);
         $this->assertSame($expectedMetadataNodes, $actualMetadataNodes, "Failed to count metadata.json nodes in {$dir}");
     }
 }