-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPlugin.php
107 lines (89 loc) · 2.94 KB
/
Plugin.php
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
<?php namespace Uit\MinifyMe;
use Cms\Classes\Theme;
use Illuminate\Support\Facades\Storage;
use MatthiasMullie\Minify;
use System\Classes\PluginBase;
class Plugin extends PluginBase
{
public function registerComponents()
{
}
public function registerSettings()
{
}
public function registerMarkupTags()
{
return [
'filters' => [
// A global function, i.e str_plural()
'minifyCss' => [$this, 'minifyCss'],
'minifyJs' => [$this, 'minifyJs'],
],
'functions' => [
// A static method call, i.e Form::open()
// 'form_open' => ['October\Rain\Html\Form', 'open'],
]
];
}
public function minifyCss($styles, $name = null)
{
if (is_null($name))
$name = 'minifed';
$minifier = new Minify\CSS();
if (is_array($styles)) {
foreach ($styles as $style) {
$path = $this->getAssetPath($style);
$minifier->add($path);
}
} else {
$path = $this->getAssetPath($styles);
$minifier->add($path);
}
$css = $minifier->minify();
$exists = Storage::exists('minifyme/' . $name . '.css');
if (!$exists)
Storage::put('minifyme/' . $name . '.css', $css);
return url('storage/app/minifyme/' . $name . '.css');
}
public function minifyJs($scripts, $name = null)
{
if (is_null($name))
$name = 'minifed';
$minifier = new Minify\JS();
if (is_array($scripts)) {
foreach ($scripts as $script) {
$path = $this->getAssetPath($script);
$minifier->add($path);
}
} else {
$path = $this->getAssetPath($scripts);
$minifier->add($path);
}
$js = $minifier->minify();
$exists = Storage::exists('minifyme/' . $name . '.js');
if (!$exists)
Storage::put('minifyme/' . $name . '.js', $js);
return url('storage/app/minifyme/' . $name . '.js');
}
public function getThemePath()
{
$theme = Theme::getActiveTheme();
return $theme->getPath();
}
public function getAssetPath($pathname)
{
$themePath = $this->getThemePath();
if ($pathname == '@jquery') {
$path = public_path('modules/backend/assets/js/vendor/jquery.min.js');
} else if ($pathname == '@framework') {
$path = public_path('modules/system/assets/js/framework.js');
} else if ($pathname == '@framework.extras.js') {
$path = public_path('modules/system/assets/js/framework.extras.js');
} else if ($pathname == '@framework.extras.css') {
$path = public_path('modules/system/assets/css/framework.extras.css');
} else {
$path = $themePath . '/' . $pathname;
}
return $path;
}
}