You are here

class vfsStreamPrintVisitor in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/mikey179/vfsStream/src/main/php/org/bovigo/vfs/visitor/vfsStreamPrintVisitor.php \org\bovigo\vfs\visitor\vfsStreamPrintVisitor

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

@since 0.10.0

Hierarchy

Expanded class hierarchy of vfsStreamPrintVisitor

See also

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

1 file declares its use of vfsStreamPrintVisitor
KernelTestBase.php in core/tests/Drupal/KernelTests/KernelTestBase.php
Contains \Drupal\KernelTests\KernelTestBase.

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;
  }

  /**
   * 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

Namesort descending Modifiers Type Description Overrides
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