Completed
Push — master ( f42a52...0a9499 )
by Thierry
02:50 queued 01:16
created

Renderer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 109
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A store() 0 8 2
A set() 0 5 1
A share() 0 5 1
A render() 0 25 3
1
<?php
2
3
namespace Jaxon\Utils\View;
4
5
class Renderer implements \Jaxon\Contracts\Template\Renderer
6
{
7
    /**
8
     * The view data store
9
     *
10
     * @var Store
11
     */
12
    protected $xStore = null;
13
14
    /**
15
     * The view global data
16
     *
17
     * @var array
18
     */
19
    protected $aViewData = [];
20
21
    /**
22
     * The view manager
23
     *
24
     * @var Manager
25
     */
26
    protected $xManager;
27
28
    /**
29
     * The constructor
30
     */
31
    public function __construct(Manager $xManager)
32
    {
33
        $this->xManager = $xManager;
34
    }
35
36
    /**
37
     * Get the current store or create a new store
38
     *
39
     * @return Store
40
     */
41
    protected function store()
42
    {
43
        if(!$this->xStore)
44
        {
45
            $this->xStore = new Store();
46
        }
47
        return $this->xStore;
48
    }
49
50
    /**
51
     * Make a piece of data available for the rendered view
52
     *
53
     * @param string        $name            The data name
54
     * @param string        $value           The data value
55
     *
56
     * @return Renderer
57
     */
58
    public function set($name, $value)
59
    {
60
        $this->store()->with($name, $value);
61
        return $this;
62
    }
63
64
    /**
65
     * Make a piece of data available for all views
66
     *
67
     * @param string        $name            The data name
68
     * @param string        $value           The data value
69
     *
70
     * @return Renderer
71
     */
72
    public function share($name, $value)
73
    {
74
        $this->aViewData[$name] = $value;
75
        return $this;
76
    }
77
78
    /**
79
     * Render a view using a store
80
     *
81
     * The store returned by this function will later be used with the make() method to render the view.
82
     *
83
     * @param string        $sViewName        The view name
84
     * @param array         $aViewData        The view data
85
     *
86
     * @return Store        A store populated with the view data
87
     */
88
    public function render($sViewName, array $aViewData = [])
89
    {
90
        // Get the store
91
        $xStore = $this->store();
92
        // Get the default view namespace
93
        $sNamespace = $this->xManager->getDefaultNamespace();
94
        // Get the namespace from the view name
95
        $iSeparatorPosition = strrpos($sViewName, '::');
96
        if($iSeparatorPosition !== false)
97
        {
98
            $sNamespace = substr($sViewName, 0, $iSeparatorPosition);
99
        }
100
        $aRenderers = $this->xManager->getRenderers();
101
        if(!key_exists($sNamespace, $aRenderers))
102
        {
103
            // Cannot render a view if there's no renderer corresponding to the namespace.
104
            return null;
105
        }
106
        $xRenderer = $this->xManager->getRenderer($aRenderers[$sNamespace]);
107
        $xStore->setView($xRenderer, $sNamespace, $sViewName, array_merge($this->aViewData, $aViewData));
108
        // Set the store to null so a new store will be created for the next view.
109
        $this->xStore = null;
110
        // Return the store
111
        return $xStore;
112
    }
113
}
114