Completed
Push — 9.0-dev ( bc2bbc...09a935 )
by Radu
02:28
created

AbstractOutputLoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 99
rs 10
c 0
b 0
f 0
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A htmlOutput() 0 3 1
A getRenderedHtml() 0 11 1
A json() 0 8 3
A html() 0 4 1
A __construct() 0 5 1
B htmlPage() 0 29 3
A jsonOutput() 0 3 1
A setHtmlTemplateData() 0 9 3
A cli() 0 5 2
1
<?php
2
namespace WebServCo\Framework;
3
4
abstract class AbstractOutputLoader
5
{
6
    protected $projectPath;
7
    protected $htmlOutput;
8
    protected $jsonOutput;
9
    
10
    public function __construct($projectPath, $htmlOutput = null, $jsonOutput = null)
11
    {
12
        $this->projectPath = $projectPath;
13
        $this->htmlOutput = $htmlOutput;
14
        $this->jsonOutput = $jsonOutput;
15
    }
16
    
17
    protected function htmlOutput()
18
    {
19
        return $this->htmlOutput;
20
    }
21
    
22
    protected function jsonOutput()
23
    {
24
        return $this->jsonOutput;
25
    }
26
    
27
    private function getRenderedHtml($template)
28
    {
29
        /**
30
         * Set template path.
31
         */
32
        $this->htmlOutput()->setPath("{$this->projectPath}resources/views/");
33
        /**
34
         * Set page template
35
         */
36
        $this->htmlOutput()->setTemplate($template);
37
        return $this->htmlOutput()->render();
38
    }
39
    
40
    protected function setHtmlTemplateData($data)
41
    {
42
        if (!is_array($data)) {
43
            return false;
44
        }
45
        foreach ($data as $key => $value) {
46
            $this->htmlOutput()->setData($key, $value);
47
        }
48
        return true;
49
    }
50
    
51
    public function html($data, $template)
52
    {
53
        $this->setHtmlTemplateData($data);
54
        return $this->getRenderedHtml($template);
55
    }
56
    
57
    public function htmlPage($data, $pageTemplate, $mainTemplate = null)
58
    {
59
        /**
60
         * Set template data.
61
         */
62
        $this->setHtmlTemplateData($data);
63
        
64
        /**
65
         * Partials
66
         */
67
        foreach ($this->htmlOutput()->setting('partials', []) as $partialTemplate) {
68
            $this->htmlOutput()->setData(
69
                "tpl_{$partialTemplate}",
70
                $this->getRenderedHtml($partialTemplate)
71
            );
72
        }
73
        /**
74
         * Page content
75
         */
76
        $this->htmlOutput()->setData(
77
            'tpl_content',
78
            $this->getRenderedHtml($pageTemplate)
79
        );
80
        /**
81
         * Main template
82
         */
83
        $mainTemplate = $mainTemplate ? $mainTemplate :
84
            $this->htmlOutput()->setting('main_template', 'layout');
85
        return $this->getRenderedHtml($mainTemplate);
86
    }
87
    
88
    public function json($data)
89
    {
90
        if (is_array($data)) {
91
            foreach ($data as $key => $value) {
92
                $this->jsonOutput()->setData($key, $value);
93
            }
94
        }
95
        return $this->jsonOutput()->render();
96
    }
97
    
98
    public function cli($string, $eol = true)
99
    {
100
        echo $string;
101
        if ($eol) {
102
            echo PHP_EOL;
103
        }
104
    }
105
}
106