1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Arubacao\AssetCdn; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Config\Repository; |
6
|
|
|
|
7
|
|
|
class Config |
8
|
|
|
{ |
9
|
|
|
const INCLUDE = 'include'; |
10
|
|
|
const EXCLUDE = 'exclude'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
private $config; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $publicPath; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param \Illuminate\Contracts\Config\Repository $config |
24
|
|
|
* @param string $publicPath |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Repository $config, string $publicPath) |
27
|
|
|
{ |
28
|
|
|
$this->config = $config->get('asset-cdn.files'); |
29
|
|
|
$this->publicPath = $publicPath; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function ignoreDotFiles(): bool |
33
|
|
|
{ |
34
|
|
|
return $this->config['ignoreDotFiles']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function ignoreVCS(): bool |
38
|
|
|
{ |
39
|
|
|
return $this->config['ignoreVCS']; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getIncludedPaths(): array |
43
|
|
|
{ |
44
|
|
|
return $this->paths(self::INCLUDE); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getExcludedPaths(): array |
48
|
|
|
{ |
49
|
|
|
return $this->paths(self::EXCLUDE); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getIncludedExtensions(): array |
53
|
|
|
{ |
54
|
|
|
return $this->extensions(self::INCLUDE); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getExcludedExtensions(): array |
58
|
|
|
{ |
59
|
|
|
return $this->extensions(self::EXCLUDE); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getIncludedPatterns(): array |
63
|
|
|
{ |
64
|
|
|
return $this->config[self::INCLUDE]['patterns']; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getExcludedPatterns(): array |
68
|
|
|
{ |
69
|
|
|
return $this->config[self::EXCLUDE]['patterns']; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getIncludedFiles(): array |
73
|
|
|
{ |
74
|
|
|
return $this->files(self::INCLUDE); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function getExcludedFiles(): array |
78
|
|
|
{ |
79
|
|
|
return $this->files(self::EXCLUDE); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function paths(string $type): array |
83
|
|
|
{ |
84
|
|
|
return array_map( |
85
|
|
|
function ($path) { |
86
|
|
|
return $this->cleanPath($path).'/'; |
87
|
|
|
}, |
88
|
|
|
$this->config[$type]['paths'] |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function files(string $type): array |
93
|
|
|
{ |
94
|
|
|
return array_map( |
95
|
|
|
function ($path) { |
96
|
|
|
return $this->cleanPath($path); |
97
|
|
|
}, |
98
|
|
|
$this->config[$type]['files'] |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function extensions(string $type): array |
103
|
|
|
{ |
104
|
|
|
return array_map( |
105
|
|
|
function ($extension) { |
106
|
|
|
return '*'.$this->start($extension, '.'); |
107
|
|
|
}, |
108
|
|
|
$this->config[$type]['extensions'] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Remove any extra slashes '/' from the path. |
114
|
|
|
* |
115
|
|
|
* @param string $path |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
private function cleanPath(string $path): string |
119
|
|
|
{ |
120
|
|
|
return rtrim(ltrim($path, '/'), '/'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Begin a string with a single instance of a given value. |
125
|
|
|
* |
126
|
|
|
* @param string $value |
127
|
|
|
* @param string $prefix |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
|
|
private function start($value, $prefix) |
131
|
|
|
{ |
132
|
|
|
$quoted = preg_quote($prefix, '/'); |
133
|
|
|
|
134
|
|
|
return $prefix.preg_replace('/^(?:'.$quoted.')+/u', '', $value); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
|
|
public function getPublicPath(): string |
141
|
|
|
{ |
142
|
|
|
return $this->publicPath; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|