Completed
Push — master ( 40c839...4a6871 )
by Flo
05:59
created

AssetList   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 4
dl 0
loc 86
c 0
b 0
f 0
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B _collectAssetsContent() 0 24 3
C __invoke() 0 39 8
1
<?php
2
3
namespace Faulancer\View\Helper;
4
5
use Faulancer\Http\Request;
6
use Faulancer\Service\Config;
7
use Faulancer\Service\RequestService;
8
use Faulancer\View\AbstractViewHelper;
9
use Faulancer\View\ViewController;
10
11
/**
12
 * Class AssetList
13
 *
14
 * @category ViewHelper
15
 * @package  Faulancer\View\Helper
16
 * @author   Florian Knapp <[email protected]>
17
 * @license  MIT License
18
 * @link     No link provided
19
 */
20
class AssetList extends AbstractViewHelper
21
{
22
23
    /**
24
     * Render a asset list by type
25
     *
26
     * @param ViewController $view   The current view
27
     * @param string         $type   The asset type
28
     * @param bool           $inHead If all assets should be concatenated
29
     *                               within style tag in head
30
     *
31
     * @return string
32
     */
33
    public function __invoke(ViewController $view, $type, $inHead = false)
34
    {
35
        $result  = '';
36
        $pattern = '';
37
38
        switch ($type) {
39
40
        case 'js':
41
            $pattern = '<script src="%s"></script>';
42
            break;
43
44
        case 'css':
45
            $pattern = '<link rel="stylesheet" type="text/css" href="%s">';
46
            break;
47
48
        }
49
50
        /** @var array $files */
51
        $files = $view->getVariable('assets' . ucfirst($type));
52
53
        if (empty($files)) {
54
            return '';
55
        }
56
57
        if (defined('APPLICATION_ENV') && APPLICATION_ENV === 'production' && $inHead) {
58
59
            $result  = '<style type="text/css">';
60
            $result .= $this->_collectAssetsContent($files);
61
            $result .= '</style>';
62
            return $result;
63
64
        }
65
66
        foreach ($files AS $file) {
67
            $result .= sprintf($pattern, $file) . "\n";
68
        }
69
70
        return $result;
71
    }
72
73
    /**
74
     * Collect all assets content for concatenation
75
     *
76
     * @param array $files The asset files
77
     *
78
     * @return string
79
     */
80
    private function _collectAssetsContent(array $files)
81
    {
82
        /** @var Config $config */
83
        $config  = $this->getServiceLocator()->get(Config::class);
84
        $docRoot = realpath($config->get('projectRoot') . '/public');
85
86
        $contents = [];
87
88
        foreach ($files as $file) {
89
90
            if (file_exists($docRoot . $file)) {
91
                $contents[] = file_get_contents($docRoot . $file);
92
            }
93
94
        }
95
96
        $content = str_replace(
97
            ["\n", "\t", "  ", ": ", " {", "{ ", " }", ";}"],
98
            ["", "", "", ":", "{", "{", "}", "}"],
99
            implode('', $contents)
100
        );
101
102
        return $content;
103
    }
104
105
}