-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsset.class.php
More file actions
175 lines (150 loc) · 5.3 KB
/
Asset.class.php
File metadata and controls
175 lines (150 loc) · 5.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
* Helper.Asset
*
* @category codeHive Extras
* @package Helper
* @author Tamer Zorba <tamer.zorba@purecis.com>
* @copyright Copyright (c) 2013 - 2016, Purecis, Inc.
* @license http://package.hive.live/Helper.Asset/license MIT License
* @link http://package.hive.live/Helper.Asset
* @version Release: 3.0
*/
namespace App\Helper;
use \App\System\Module;
use \App\System\Event;
use \App\System\Request;
use \App\System\Str;
use \App\System\Scope;
use \App\System\Loader;
use \App\System\FileSystem;
class Asset extends Module{
/**
* initialize defaults.
*/
public function __bootstrap(){
// define default meta tags
$evt = new Event("AssetsManagment");
$evt->addListener('defaults', "<meta charset='utf-8'>");
$evt->addListener('defaults', "<meta name='viewport' content='width=device-width, initial-scale=1.0'>");
$evt->addListener('defaults', "<meta http-equiv='X-UA-Compatible' content='IE=edge' />");
// define default javascript codeHive path
$hive = new Scope("config.hive");
$this->jsDefine('path.base', $this->request->base);
$this->jsDefine('path.app', $hive->app_path);
$this->jsDefine('path.view', $hive->app_path . "/view");
$this->jsDefine('path.vendor', $hive->app_path . "/vendor");
$this->jsDefine('path.assets', $hive->assets);
$this->jsDefine('path.library', $hive->assets . "/library");
$this->jsDefine('path.domain', $this->request->domain);
}
/**
* define javascript code.
*
* @param string $name_path
* @param Mixen $value
* @param boolean $isPush
*/
protected static $jsDefined = [];
public function jsDefine()
{
$args = func_get_args();
$evt = new Event("AssetsManagment");
// check codeHive JS Object Container is exists
if(sizeof(self::$jsDefined) == 0){
$evt->addListener('definer', "var codeHive = codeHive || {};");
array_push(self::$jsDefined, "codeHive");
}
// extract params
$names = isset($args[0]) ? explode('.', $args[0]) : [];
$value = isset($args[1]) ? $args[1] : '';
$isPush = isset($args[2]);
// handle objects and arrays
if(is_array($value) || is_object($value)){
$value = json_encode($value, JSON_NUMERIC_CHECK);
}else{
$value = '"' . $value . '"';
}
foreach($names as $idx => $name){
$temp = $names;
array_splice($temp, $idx+1);
$name = implode('.', $temp);
if(sizeof($names)-1 == $idx){
// define variables
if($isPush){
$evt->addListener('definer', "codeHive.{$name} = codeHive.{$name} || [];");
$evt->addListener('definer', "codeHive.{$name}.push({$value});");
}else{
$evt->addListener('definer', "codeHive.{$name} = {$value};");
}
}else{
// fallback address
if(in_array("codeHive.{$name}", self::$jsDefined)){
continue;
}
$evt->addListener('definer', "codeHive.{$name} = codeHive.{$name} || {};");
array_push(self::$jsDefined, "codeHive.{$name}");
}
}
}
/**
* push string or array to javascript array.
*
* @param string $src Path of js file
* @param string $folder default load folder
* @param string $ext extension to load inside folder
*
* @return string
*/
public function jsPush()
{
$args = func_get_args();
array_push($args, true);
call_user_func_array(["self", "jsDefine"], $args);
}
/**
* Parse Source string Load assets.
*
* @param string $src Path of js file
* @param string $folder default load folder
* @param string $ext extension to load inside folder
*
* @return string
*/
private function srcParser($src, $folder, $ext = false)
{
$hive = new Scope('config.hive');
$request = new Request;
// check if not external link then
if(!Str::contains($src, '//')) {
$src = Loader::getDir($folder . "/" . $src);
$src = $request->base . $src;
}
return $src;
}
/**
* add Script to Event.
*
* @param string $src Path of js file
* @param string $folder default load folder
*/
public function script($src, $folder = 'vendor')
{
$src = $this->srcParser($src, $folder);
$evt = new Event("AssetsManagment");
$evt->addListener('script', "<script type='text/javascript' src='{$src}'></script>");
}
/**
* add Style to Event.
*
* @param string $src Path of js file
* @param string $folder default load folder
*/
public function style($src, $folder = 'vendor')
{
$src = $this->srcParser($src, $folder);
$type = FileSystem::extension($src) == 'less' ? '/less' : '';
$evt = new Event("AssetsManagment");
$evt->addListener('style', "<link rel='stylesheet{$type}' type='text/css' href='{$src}' />");
}
}