1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Ntentan Framework |
4
|
|
|
* Copyright (c) 2010-2015 James Ekow Abaka Ainooson |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
7
|
|
|
* a copy of this software and associated documentation files (the |
8
|
|
|
* "Software"), to deal in the Software without restriction, including |
9
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
10
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
11
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
12
|
|
|
* the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be |
15
|
|
|
* included in all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
18
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
19
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
20
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
21
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
22
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
23
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace ntentan\honam; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Responsible for loading assets, placing them in a common directory and |
30
|
|
|
* could perform CDN url operations if needed. |
31
|
|
|
* This class employs a heirachy of directories from which assets are loaded. |
32
|
|
|
* Assets found in directories higher in the heirachy are chosen first. This |
33
|
|
|
* mechanism provides room for third party extensions to override assets provided |
34
|
|
|
* by core applications. |
35
|
|
|
*/ |
36
|
|
|
class AssetsLoader |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* Heirachy of directories within which assets are loaded. |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private static $assetsPathHeirachy = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Destination public directory of all loaded assets. |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
private static $publicPath = 'public'; |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* Base site URL. |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
private static $siteUrl = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Returns the actual path for a particular asset. |
58
|
|
|
* You pass a specific asset destination path to this method and it |
59
|
|
|
* scans the asset path till it finds a matching source asset. The path to |
60
|
|
|
* this matching asset is then returned. |
61
|
|
|
* |
62
|
|
|
* @param string $asset |
63
|
|
|
* @return string |
64
|
|
|
* @throws exceptions\FileNotFoundException |
65
|
|
|
*/ |
66
|
3 |
|
public static function getAssetPath($asset) |
67
|
|
|
{ |
68
|
3 |
|
$path = ''; |
69
|
3 |
|
foreach(self::$assetsPathHeirachy as $assetPath) |
70
|
|
|
{ |
71
|
3 |
|
if(file_exists("$assetPath/$asset")) |
72
|
|
|
{ |
73
|
3 |
|
$path = "$assetPath/$asset"; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
if($path == '') |
78
|
|
|
{ |
79
|
1 |
|
throw new exceptions\FileNotFoundException("Asset file $asset not found"); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return $path; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Loads an asset into the public directory. |
87
|
|
|
* This method returns the site url path appended with the asset path. |
88
|
|
|
* |
89
|
|
|
* @param string $asset Asset path |
90
|
|
|
* @param string $copyFrom Path to copy files from |
91
|
|
|
* @return |
92
|
|
|
*/ |
93
|
10 |
|
public static function load($asset, $copyFrom = null) |
94
|
|
|
{ |
95
|
10 |
|
if($copyFrom === null) |
96
|
|
|
{ |
97
|
3 |
|
$assetPath = self::getAssetPath($asset); |
98
|
|
|
} |
99
|
|
|
else |
100
|
|
|
{ |
101
|
8 |
|
$assetPath = $copyFrom; |
102
|
|
|
} |
103
|
|
|
|
104
|
9 |
|
$publicPath = self::$publicPath . "/$asset"; |
105
|
9 |
|
$publicDirectory = dirname($publicPath); |
106
|
|
|
|
107
|
9 |
|
if(file_exists($assetPath) && file_exists($publicDirectory) && is_writable($publicDirectory)) |
108
|
|
|
{ |
109
|
6 |
|
copy($assetPath, $publicPath); |
110
|
|
|
} |
111
|
|
|
else |
112
|
|
|
{ |
113
|
3 |
|
self::throwTemplateEngineExceptions($assetPath, $publicPath); |
114
|
|
|
} |
115
|
6 |
|
return self::getSiteUrl() . "/$asset"; |
116
|
|
|
} |
117
|
|
|
|
118
|
3 |
|
private static function throwTemplateEngineExceptions($assetPath, $publicPath) |
119
|
|
|
{ |
120
|
3 |
|
if(!file_exists($assetPath)) |
121
|
|
|
{ |
122
|
2 |
|
throw new \ntentan\honam\exceptions\FileNotFoundException("File not found [$assetPath]"); |
123
|
|
|
} |
124
|
1 |
|
else if(!is_writable($publicPath)) |
125
|
|
|
{ |
126
|
1 |
|
throw new \ntentan\honam\exceptions\FilePermissionException("Destination [$publicPath] is not writable"); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Append a source directory to the bottom of the heirachy. |
132
|
|
|
* @param string $assetsDir |
133
|
|
|
*/ |
134
|
14 |
|
public static function appendSourceDir($assetsDir) |
135
|
|
|
{ |
136
|
14 |
|
self::$assetsPathHeirachy[] = $assetsDir; |
137
|
14 |
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Prepend a source directory to the top of the heirachy. |
141
|
|
|
* @param string $assetsDir |
142
|
|
|
*/ |
143
|
|
|
public static function prependSourceDir($assetsDir) |
144
|
|
|
{ |
145
|
|
|
array_unshift(self::$assetsPathHeirachy, $assetsDir); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set the destination directory of loaded assets. |
150
|
|
|
* @param string $publicBase |
151
|
|
|
*/ |
152
|
14 |
|
public static function setDestinationDir($publicBase) |
153
|
|
|
{ |
154
|
14 |
|
self::$publicPath = $publicBase; |
155
|
14 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Get the destination directory of loaded assets. |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
10 |
|
public static function getDestinationDir() |
162
|
|
|
{ |
163
|
10 |
|
return self::$publicPath; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the base url of the current site. |
168
|
|
|
* Used internally mainly for the purposes of generating HTML files, this |
169
|
|
|
* function would either return a preset site URL or the current |
170
|
|
|
* destination directory. Using this function makes it easy to generate |
171
|
|
|
* HTML files which depend on relative paths to locate assets. |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
*/ |
175
|
10 |
|
public static function getSiteUrl() |
176
|
|
|
{ |
177
|
10 |
|
if(self::$siteUrl === null) |
178
|
|
|
{ |
179
|
10 |
|
return self::getDestinationDir(); |
180
|
|
|
} |
181
|
|
|
else |
182
|
|
|
{ |
183
|
|
|
return self::$siteUrl; |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Set the base URL of your site to aid in generating full URLs for assets. |
189
|
|
|
* @param string $siteUrl |
190
|
|
|
*/ |
191
|
|
|
public static function setSiteUrl($siteUrl) |
192
|
|
|
{ |
193
|
|
|
self::$siteUrl = $siteUrl; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public static function reset() |
197
|
|
|
{ |
198
|
|
|
self::$assetsPathHeirachy = []; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|