forked from shnhrrsn/laravel-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAsset.php
114 lines (87 loc) · 2.64 KB
/
Asset.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
108
109
110
111
112
113
114
<?php namespace Assets;
use InvalidArgumentException;
use Symfony\Component\Process\Process;
class Asset {
public static $autoMinifyDefault = false;
protected static $compilers = [
'scss' => 'ScssCompiler',
'less' => 'LessCompiler',
'coffee' => 'CoffeeCompiler',
'js' => 'JavascriptCompiler'
];
protected $autoMinify;
protected $path;
protected $compiler;
public static function make($path, $autoMinify = null) {
return new static($path, $autoMinify);
}
public function __construct($path, $autoMinify = null) {
$this->path = $path;
$this->autoMinify = $autoMinify === null ? static::$autoMinifyDefault : $autoMinify;
}
/**
* @return Symfony\Component\Process\Process
*/
public function getCompileProcess($context = null) {
return $this->getCompiler()->getCompileProcess($this->path, $context);
}
public function getLastModified($newest = 0) {
return $this->getCompiler()->getLastModified($this->path, $newest);
}
public function getMime() {
return $this->getCompiler()->getMime();
}
public function getType() {
return $this->getCompiler()->getType();
}
public function getExtension() {
return $this->getCompiler()->getExtension();
}
public function getCompiler() {
if($this->compiler === null) {
$this->compiler = static::getCompilerFromPath($this->path);
}
return $this->compiler;
}
public static function registerCompiler($extension, \Assets\Compilers\Compiler $compiler) {
static::$compilers[$extension] = $compiler;
}
public static function getCompilerFromPath($path) {
$extension = array_first(array_keys(static::$compilers), function($key, $value) use ($path) {
return ends_with($path, $value);
});
if($extension === null) {
throw new InvalidArgumentException('Unrecognized extension in file: ' . $path);
}
$compiler = static::$compilers[$extension];
if(is_string($compiler)) {
$className = '\Assets\Compilers\\' . $compiler;
$compiler = new $className(static::$autoMinifyDefault);
static::$compilers[$extension] = $compiler;
}
return $compiler;
}
public static function isPathSupported($path) {
$extension = array_first(array_keys(static::$compilers), function($key, $value) use ($path) {
return ends_with($path, $value);
});
return $extension !== null;
}
public static function publishedPath($path) {
static $published = null;
if($published === null) {
$published = config('published_assets');
}
if(strpos($path, 'assets/') !== 0) {
$path = 'assets/' . $path;
}
if(isset($published[$path])) {
$path = $published[$path];
}
if(strpos($path, '://') === false) {
return app('url')->asset($path);
} else {
return $path;
}
}
}