Completed
Push — master ( 415b4f...916614 )
by Gjero
01:28
created

Transphporm::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 12
rs 9.4285
1
<?php
2
/**
3
 * View
4
 *
5
 * @copyright Copyright (c)  Garrett Whitehorn (http://garrettw.net/)
6
 * @license   http://opensource.org/licenses/MIT MIT License
7
 */
8
9
namespace Pimf\View;
10
11
use Pimf\Contracts\Reunitable;
12
use Pimf\View;
13
use Pimf\Config;
14
use Pimf\Cache\Storages\File as FileCache;
15
16
/**
17
 * A view for the Transphporm template engine which uses CSS-style syntax and
18
 * separates templates from data logic.
19
 *
20
 * For use please add the following code to the end of the config.app.php file:
21
 *
22
 * <code>
23
 *
24
 * 'view' => array(
25
 *
26
 *   'transphporm' => array(
27
 *     'cache'       => true,  // if compilation caching should be used
28
 *   ),
29
 *
30
 * ),
31
 *
32
 * </code>
33
 *
34
 * @link    https://github.com/Level-2/Transphporm
35
 * @package View
36
 * @author  Garrett Whitehorn <[email protected]>
37
 * @codeCoverageIgnore
38
 */
39
class Transphporm extends View implements Reunitable
40
{
41
    /**
42
     * @var \Pimf\Cache\Storages\File
43
     */
44
    protected $cache;
45
46
    /**
47
     * @param string $template
48
     * @param array  $data
49
     */
50
    public function __construct($template, array $data = [])
51
    {
52
        parent::__construct($template, $data);
53
54
        $conf = Config::get('view.transphporm');
55
56
        if ($conf['cache'] === true) {
57
            $this->cache = new FileCache($this->path . '/transphporm_cache/');
58
        }
59
60
        require_once BASE_PATH . "Transphporm/vendor/autoload.php";
61
    }
62
63
    /**
64
     * Puts the template and the TSS/data together.
65
     *
66
     * @return string
67
     */
68
    public function reunite()
69
    {
70
        $tss = $this->data['tss'];
71
        $tsspath = $this->path . '/' . $tss;
72
73
        $template = new \Transphporm\Builder(
74
            $this->path . '/' . $this->template,
75
            (is_file($tsspath)) ? $tsspath : $tss
76
        );
77
78
        if (isset($this->cache)) {
79
            $template->setCache($this->cache);
80
        }
81
82
        if (isset($this->data['data'])) {
83
            return $template->output($this->data['data'])->body;
84
        }
85
        return $template->output()->body;
86
    }
87
}
88