1 | <?php namespace Spekkionu\Assetcachebuster; |
||
5 | class Assetcachebuster |
||
6 | { |
||
7 | |||
8 | /** |
||
9 | * Flag for if the package is anabled |
||
10 | * |
||
11 | * @var boolean $enabled |
||
12 | */ |
||
13 | protected $enabled = false; |
||
14 | |||
15 | /** |
||
16 | * CDN Url |
||
17 | * |
||
18 | * @var string $cdn The url for the cdn |
||
19 | */ |
||
20 | protected $cdn = '/'; |
||
21 | |||
22 | /** |
||
23 | * Asset cache busting hash |
||
24 | * |
||
25 | * @var string $hash The hash to use to bust the cache |
||
26 | */ |
||
27 | protected $hash = null; |
||
28 | |||
29 | /** |
||
30 | * Asset prefix |
||
31 | * |
||
32 | * @var string $prefix A prefix containing assets |
||
33 | */ |
||
34 | protected $prefix = null; |
||
35 | |||
36 | /** |
||
37 | * Class constructor |
||
38 | * |
||
39 | * @param array $options Array of options from the config file |
||
40 | */ |
||
41 | 8 | public function __construct(array $options) |
|
42 | { |
||
43 | 8 | if (isset($options['enable'])) { |
|
44 | 6 | $this->setEnabled($options['enable']); |
|
45 | } |
||
46 | 8 | if (isset($options['hash'])) { |
|
47 | 6 | $this->setHash($options['hash']); |
|
48 | } |
||
49 | 8 | if (isset($options['prefix'])) { |
|
50 | 6 | $this->setPrefix($options['prefix']); |
|
51 | } |
||
52 | 8 | if (isset($options['cdn'])) { |
|
53 | 6 | $this->setCdnUrl($options['cdn']); |
|
54 | } |
||
55 | 8 | } |
|
56 | |||
57 | /** |
||
58 | * Enables / Disables the package |
||
59 | * |
||
60 | * @param boolean $enabled True to enable, false to disable |
||
61 | */ |
||
62 | 6 | public function setEnabled($enabled = true) |
|
66 | |||
67 | /** |
||
68 | * Sets the hash |
||
69 | * |
||
70 | * @param string $hash The hash to use to bust the cache |
||
71 | */ |
||
72 | 7 | public function setHash($hash) |
|
73 | { |
||
74 | 7 | if (!preg_match("/[0-9a-f]{32}/", $hash)) { |
|
75 | 1 | throw new InvalidArgumentException("Asset cache buster hash must be a valid md5 hash."); |
|
76 | } |
||
77 | 6 | $hash = trim($hash, '/'); |
|
78 | |||
79 | 6 | $this->hash = $hash; |
|
80 | 6 | } |
|
81 | |||
82 | /** |
||
83 | * @return string |
||
84 | */ |
||
85 | 1 | public function getHash() |
|
86 | { |
||
87 | 1 | return $this->hash; |
|
88 | } |
||
89 | |||
90 | |||
91 | /** |
||
92 | * Sets the asset prefix path |
||
93 | * |
||
94 | * @param string $prefix A prefix containing assets |
||
95 | */ |
||
96 | 6 | public function setPrefix($prefix = null) |
|
102 | |||
103 | /** |
||
104 | * Sets the CDN url |
||
105 | * |
||
106 | * @param string $cdn The url for the cdn |
||
107 | */ |
||
108 | 6 | public function setCdnUrl($cdn = null) |
|
112 | |||
113 | /** |
||
114 | * Generates an asset url |
||
115 | * |
||
116 | * @param string $path The path to the asset |
||
117 | * |
||
118 | * @return string The asset url with the cache busting hash |
||
119 | */ |
||
120 | 5 | public function url($path = '') |
|
131 | |||
132 | /** |
||
133 | * Generate a random key for the application. |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | 1 | public function generateHash() |
|
141 | } |
||
142 |