Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. vfsStreamPrintVisitor.php

class vfsStreamPrintVisitor

Visitor which traverses a content structure recursively to print it to an output stream.

@since 0.10.0

Hierarchy

  • class \org\bovigo\vfs\visitor\vfsStreamAbstractVisitor implements \org\bovigo\vfs\visitor\vfsStreamVisitor
    • class \org\bovigo\vfs\visitor\vfsStreamPrintVisitor extends \org\bovigo\vfs\visitor\vfsStreamAbstractVisitor

Expanded class hierarchy of vfsStreamPrintVisitor

See also

https://github.com/mikey179/vfsStream/issues/10

File

vendor/mikey179/vfsstream/src/main/php/org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php, line 22

Namespace

org\bovigo\vfs\visitor
View source
class vfsStreamPrintVisitor extends vfsStreamAbstractVisitor {
    
    /**
     * target to write output to
     *
     * @type  resource
     */
    protected $out;
    
    /**
     * current depth in directory tree
     *
     * @type  int
     */
    protected $depth;
    
    /**
     * constructor
     *
     * If no file pointer given it will fall back to STDOUT.
     *
     * @param   resource  $out  optional
     * @throws  \InvalidArgumentException
     * @api
     */
    public function __construct($out = STDOUT) {
        if (is_resource($out) === false || get_resource_type($out) !== 'stream') {
            throw new \InvalidArgumentException('Given filepointer is not a resource of type stream');
        }
        $this->out = $out;
        $this->depth = 0;
    }
    
    /**
     * visit a file and process it
     *
     * @param   vfsStreamFile  $file
     * @return  vfsStreamPrintVisitor
     */
    public function visitFile(vfsStreamFile $file) {
        $this->printContent($file->getName());
        return $this;
    }
    
    /**
     * visit a block device and process it
     *
     * @param   vfsStreamBlock  $block
     * @return  vfsStreamPrintVisitor
     */
    public function visitBlockDevice(vfsStreamBlock $block) {
        $name = '[' . $block->getName() . ']';
        $this->printContent($name);
        return $this;
    }
    
    /**
     * visit a directory and process it
     *
     * @param   vfsStreamDirectory  $dir
     * @return  vfsStreamPrintVisitor
     */
    public function visitDirectory(vfsStreamDirectory $dir) {
        $this->printContent($dir->getName());
        $this->depth++;
        foreach ($dir as $child) {
            $this->visit($child);
        }
        $this->depth--;
        return $this;
    }
    
    /**
     * helper method to print the content
     *
     * @param  string   $name
     */
    protected function printContent($name) {
        fwrite($this->out, str_repeat('  ', $this->depth) . '- ' . $name . "\n");
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
vfsStreamAbstractVisitor::visit public function visit a content and process it Overrides vfsStreamVisitor::visit
vfsStreamPrintVisitor::$depth protected property current depth in directory tree
vfsStreamPrintVisitor::$out protected property target to write output to
vfsStreamPrintVisitor::printContent protected function helper method to print the content
vfsStreamPrintVisitor::visitBlockDevice public function visit a block device and process it Overrides vfsStreamAbstractVisitor::visitBlockDevice
vfsStreamPrintVisitor::visitDirectory public function visit a directory and process it Overrides vfsStreamVisitor::visitDirectory
vfsStreamPrintVisitor::visitFile public function visit a file and process it Overrides vfsStreamVisitor::visitFile
vfsStreamPrintVisitor::__construct public function constructor
RSS feed
Powered by Drupal