1 | <?php |
||
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) |
|
138 | |||
139 | /** |
||
140 | * Prepend a source directory to the top of the heirachy. |
||
141 | * @param string $assetsDir |
||
142 | */ |
||
143 | public static function prependSourceDir($assetsDir) |
||
147 | |||
148 | /** |
||
149 | * Set the destination directory of loaded assets. |
||
150 | * @param string $publicBase |
||
151 | */ |
||
152 | 14 | public static function setDestinationDir($publicBase) |
|
156 | |||
157 | /** |
||
158 | * Get the destination directory of loaded assets. |
||
159 | * @return string |
||
160 | */ |
||
161 | 10 | public static function getDestinationDir() |
|
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) |
||
195 | |||
196 | public static function reset() |
||
200 | } |
||
201 |