Completed
Push — master ( f9e7b8...4546ab )
by Mihail
03:03
created

Alias::addPlainCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 10
rs 9.4286
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace Ffcms\Core;
4
5
use Ffcms\Core\Helper\Type\Arr;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Helper\Type\Str;
8
9
10
/**
11
 * Class Alias - fast alias for core property's
12
 * @package Ffcms\Core
13
 */
14
class Alias
15
{
16
17
    /**
18
     * Absolute path to current view folder
19
     * @var string
20
     */
21
    public $currentViewPath;
22
23
    /**
24
     * Return full URL of current view folder
25
     * @var string
26
     */
27
    public $currentViewUrl;
28
29
    /**
30
     * Current app basic domain address, obtained from request
31
     * @var string
32
     */
33
    public $baseDomain;
34
35
    /**
36
     * Current app basic URL address, obtained from request
37
     * @var string
38
     */
39
    public $baseUrl;
40
41
    /**
42
     * Current app basic URL without any changes in pathway(lang-defined, etc)
43
     * @var string
44
     */
45
    public $scriptUrl;
46
47
48
    /**
49
     * Default vendor library's
50
     * @var array
51
     */
52
    protected $vendorNamedLibrary = [
53
        'js' => null,
54
        'css' => null
55
    ];
56
57
    /**
58
     * Custom code library's
59
     * @var array
60
     */
61
    protected $codeCustomLibrary = [
62
        'js' => null,
63
        'css' => null
64
    ];
65
66
    /**
67
     * Custom code storage for templates
68
     * @var array
69
     */
70
    protected $plainCode = [
71
        'js' => null,
72
        'css' => null
73
    ];
74
75
76
    public function __construct()
77
    {
78
        // make alias for view pathway
79
        $this->currentViewPath = App::$View->themePath;
80
81
        // make alias for baseUrl, script url and domain
82
        $this->baseDomain = App::$Request->getHost();
83
        $this->scriptUrl = App::$Request->getSchemeAndHttpHost();
84
        $this->baseUrl = $this->scriptUrl;
85
        if (App::$Request->getBasePath() !== null) {
86
            $this->baseUrl .= App::$Request->getBasePath();
87
        }
88
        if (App::$Request->languageInPath() && App::$Request->getLanguage() !== null) {
89
            $this->baseUrl .= '/' . App::$Request->getLanguage();
90
        }
91
92
        // build vendor libs alias
93
        $this->vendorNamedLibrary['js']['jquery'] = $this->scriptUrl . '/vendor/bower/jquery/dist/jquery.min.js';
94
        $this->vendorNamedLibrary['css']['bootstrap'] = $this->scriptUrl . '/vendor/bower/bootstrap/dist/css/bootstrap.min.css';
95
        $this->vendorNamedLibrary['js']['bootstrap'] = $this->scriptUrl . '/vendor/bower/bootstrap/dist/js/bootstrap.min.js';
96
        $this->vendorNamedLibrary['css']['fa'] = $this->scriptUrl . '/vendor/bower/components-font-awesome/css/font-awesome.min.css';
97
        $this->vendorNamedLibrary['js']['jquery-ui'] = $this->scriptUrl . '/vendor/bower/jquery-ui/jquery-ui.min.js';
98
        $this->vendorNamedLibrary['css']['jquery-ui'] = $this->scriptUrl . '/vendor/bower/jquery-ui/themes/base/jquery-ui.min.css';
99
100
        $themeAll = App::$Properties->get('theme');
101
        if (!isset($themeAll[env_name]) || Str::length($themeAll[env_name]) < 1) {
102
            $themeAll[env_name] = 'default';
103
        }
104
        $this->currentViewUrl = $this->scriptUrl . '/Apps/View/' . env_name . '/' . $themeAll[env_name];
105
    }
106
107
    /**
108
     * @param string $type
109
     * @param string $name
110
     * @return string|null
111
     */
112
    public function getVendor($type, $name)
113
    {
114
        return $this->vendorNamedLibrary[$type][$name];
115
    }
116
117
    /**
118
     * Set custom library $link by $type
119
     * @param string $type
120
     * @param string $link
121
     */
122
    public function setCustomLibrary($type, $link)
123
    {
124
        $this->codeCustomLibrary[$type][] = $link;
125
    }
126
127
    /**
128
     * Get custom library array by type
129
     * @param string $type
130
     * @return array|null
131
     */
132
    public function getCustomLibraryArray($type)
133
    {
134
        return $this->codeCustomLibrary[$type];
135
    }
136
137
    /**
138
     * Unset custom library's by type
139
     * @param $type
140
     */
141
    public function unsetCustomLibrary($type)
142
    {
143
        unset($this->codeCustomLibrary[$type]);
144
    }
145
146
    /**
147
     * @param string $type
148
     * @param string $code
149
     * @return bool
150
     */
151
    public function addPlainCode($type, $code)
152
    {
153
        $allowed = ['css', 'js'];
154
        if (!Arr::in($type, $allowed)) {
155
            return false;
156
        }
157
158
        $this->plainCode[$type][] = $code;
159
        return true;
160
    }
161
162
    /**
163
     * Get plain code build container as string
164
     * @param string $type
165
     * @return null|string
166
     */
167
    public function getPlainCode($type)
168
    {
169
        return $this->plainCode[$type];
170
    }
171
172
173
}