1<?php
2/**
3 * Zend Framework (http://framework.zend.com/)
4 *
5 * @link      http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license   http://framework.zend.com/license/new-bsd New BSD License
8 */
9
10namespace Zend\View\Renderer;
11
12use Zend\Filter\FilterChain;
13use Zend\View\Model\ModelInterface;
14use Zend\View\Resolver\ResolverInterface;
15
16/**
17 * Class for Zend\View\Model\ConsoleModel to help enforce private constructs.
18 *
19 * Note: all private variables in this class are prefixed with "__". This is to
20 * mark them as part of the internal implementation, and thus prevent conflict
21 * with variables injected into the renderer.
22 */
23class ConsoleRenderer implements RendererInterface, TreeRendererInterface
24{
25    /**
26     * @var FilterChain
27     */
28    protected $__filterChain;
29
30    /**
31     * Constructor.
32     *
33     *
34     * @todo handle passing helper manager, options
35     * @todo handle passing filter chain, options
36     * @todo handle passing variables object, options
37     * @todo handle passing resolver object, options
38     * @param array $config Configuration key-value pairs.
39     */
40    public function __construct($config = array())
41    {
42        $this->init();
43    }
44
45    public function setResolver(ResolverInterface $resolver)
46    {
47        return $this;
48    }
49
50    /**
51     * Return the template engine object
52     *
53     * Returns the object instance, as it is its own template engine
54     *
55     * @return ConsoleRenderer
56     */
57    public function getEngine()
58    {
59        return $this;
60    }
61
62    /**
63     * Allow custom object initialization when extending ConsoleRenderer
64     *
65     * Triggered by {@link __construct() the constructor} as its final action.
66     *
67     * @return void
68     */
69    public function init()
70    {
71    }
72
73    /**
74     * Set filter chain
75     *
76     * @param  FilterChain $filters
77     * @return ConsoleRenderer
78     */
79    public function setFilterChain(FilterChain $filters)
80    {
81        $this->__filterChain = $filters;
82        return $this;
83    }
84
85    /**
86     * Retrieve filter chain for post-filtering script content
87     *
88     * @return FilterChain
89     */
90    public function getFilterChain()
91    {
92        if (null === $this->__filterChain) {
93            $this->setFilterChain(new FilterChain());
94        }
95        return $this->__filterChain;
96    }
97
98    /**
99     * Recursively processes all ViewModels and returns output.
100     *
101     * @param  string|ModelInterface   $model        A ViewModel instance.
102     * @param  null|array|\Traversable $values       Values to use when rendering. If none
103     *                                               provided, uses those in the composed
104     *                                               variables container.
105     * @return string Console output.
106     */
107    public function render($model, $values = null)
108    {
109        if (!$model instanceof ModelInterface) {
110            return '';
111        }
112
113        $result = '';
114        $options = $model->getOptions();
115        foreach ($options as $setting => $value) {
116            $method = 'set' . $setting;
117            if (method_exists($this, $method)) {
118                $this->$method($value);
119            }
120            unset($method, $setting, $value);
121        }
122        unset($options);
123
124        $values = $model->getVariables();
125
126        if (isset($values['result'])) {
127            // filter and append the result
128            $result .= $this->getFilterChain()->filter($values['result']);
129        }
130
131        if ($model->hasChildren()) {
132            // recursively render all children
133            foreach ($model->getChildren() as $child) {
134                $result .= $this->render($child, $values);
135            }
136        }
137
138        return $result;
139    }
140
141    /**
142     * @see Zend\View\Renderer\TreeRendererInterface
143     * @return bool
144     */
145    public function canRenderTrees()
146    {
147        return true;
148    }
149}
150