1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117:
<?php
/*
* This file is part of the webmozart/console package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Webmozart\Console\UI\Layout;
use Webmozart\Console\Api\IO\IO;
use Webmozart\Console\UI\Alignment\LabelAlignment;
use Webmozart\Console\UI\Component;
use Webmozart\Console\UI\Component\LabeledParagraph;
/**
* Renders renderable objects in indented blocks.
*
* @since 1.0
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class BlockLayout implements Component
{
/**
* @var int
*/
private $currentIndentation = 0;
/**
* @var Component[]
*/
private $elements = array();
/**
* @var int[]
*/
private $indentations = array();
/**
* @var LabelAlignment
*/
private $alignment;
/**
* Creates a new layout.
*/
public function __construct()
{
$this->alignment = new LabelAlignment();
}
/**
* Adds a renderable element to the layout.
*
* @param Component $element The element to add.
*
* @return static The current instance.
*/
public function add(Component $element)
{
$this->elements[] = $element;
$this->indentations[] = $this->currentIndentation;
if ($element instanceof LabeledParagraph) {
$this->alignment->add($element, $this->currentIndentation);
$element->setAlignment($this->alignment);
}
return $this;
}
/**
* Starts a new indented block.
*
* @return static The current instance.
*/
public function beginBlock()
{
$this->currentIndentation += 2;
return $this;
}
/**
* Ends the current indented block.
*
* @return static The current instance.
*/
public function endBlock()
{
$this->currentIndentation -= 2;
return $this;
}
/**
* Renders all elements in the layout.
*
* @param IO $io The I/O.
* @param int $indentation The number of spaces to indent.
*/
public function render(IO $io, $indentation = 0)
{
$this->alignment->align($io, $indentation);
foreach ($this->elements as $i => $element) {
$element->render($io, $this->indentations[$i] + $indentation);
}
$this->elements = array();
}
}