1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Eliurkis\laravelcdn; |
4
|
|
|
|
5
|
|
|
use Illuminate\Config\Repository; |
6
|
|
|
use Eliurkis\laravelcdn\Contracts\CdnHelperInterface; |
7
|
|
|
use Eliurkis\laravelcdn\Exceptions\MissingConfigurationException; |
8
|
|
|
use Eliurkis\laravelcdn\Exceptions\MissingConfigurationFileException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class CdnHelper |
12
|
|
|
* Helper class containing shared functions. |
13
|
|
|
* |
14
|
|
|
* @category General Helper |
15
|
|
|
* |
16
|
|
|
* @author Mahmoud Zalt <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class CdnHelper implements CdnHelperInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* An object of the 'Repository' class that allows reading the laravel config files. |
22
|
|
|
* |
23
|
|
|
* @var \Illuminate\Config\Repository |
24
|
|
|
*/ |
25
|
|
|
protected $configurations; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param \Illuminate\Config\Repository $configurations |
29
|
|
|
*/ |
30
|
|
|
public function __construct(Repository $configurations) |
31
|
|
|
{ |
32
|
|
|
$this->configurations = $configurations; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Check if the config file exist and return it or |
37
|
|
|
* throw an exception. |
38
|
|
|
* |
39
|
|
|
* @throws Exceptions\MissingConfigurationFileException |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
public function getConfigurations() |
44
|
|
|
{ |
45
|
|
|
$configurations = $this->configurations->get('cdn'); |
46
|
|
|
|
47
|
|
|
if (!$configurations) { |
48
|
|
|
throw new MissingConfigurationFileException("CDN 'config file' (cdn.php) not found"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $configurations; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks for any required configuration is missed. |
56
|
|
|
* |
57
|
|
|
* @param $configuration |
58
|
|
|
* @param $required |
59
|
|
|
* |
60
|
|
|
* @throws \Eliurkis\laravelcdn\Exceptions\MissingConfigurationException |
61
|
|
|
*/ |
62
|
|
|
public function validate($configuration, $required) |
63
|
|
|
{ |
64
|
|
|
// search for any null or empty field to throw an exception |
65
|
|
|
$missing = ''; |
66
|
|
|
foreach ($configuration as $key => $value) { |
67
|
|
|
if (in_array($key, $required) && |
68
|
|
|
(empty($value) || $value == null || $value == '') |
69
|
|
|
) { |
70
|
|
|
$missing .= ' '.$key; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ($missing) { |
75
|
|
|
throw new MissingConfigurationException('Missed Configuration:'.$missing); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Take url as string and return it parsed object. |
81
|
|
|
* |
82
|
|
|
* @param $url |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
*/ |
86
|
|
|
public function parseUrl($url) |
87
|
|
|
{ |
88
|
|
|
return parse_url($url); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* check if a string starts with a string. |
93
|
|
|
* |
94
|
|
|
* @param $with |
95
|
|
|
* @param $str |
96
|
|
|
* |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
public function startsWith($with, $str) |
100
|
|
|
{ |
101
|
|
|
return substr($str, 0, strlen($with)) === $with; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* remove any extra slashes '/' from the path. |
106
|
|
|
* |
107
|
|
|
* @param $path |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function cleanPath($path) |
112
|
|
|
{ |
113
|
|
|
return rtrim(ltrim($path, '/'), '/'); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|