1
|
|
|
<?php |
2
|
|
|
defined('ROOT_PATH') || exit('Access denied'); |
3
|
|
|
/** |
4
|
|
|
* TNH Framework |
5
|
|
|
* |
6
|
|
|
* A simple PHP framework using HMVC architecture |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2017 TNH Framework |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file Assets.php |
33
|
|
|
* |
34
|
|
|
* This class contains static methods for generating static content links (images, Javascript, CSS, etc.). |
35
|
|
|
* |
36
|
|
|
* @package core |
37
|
|
|
* @author TNH Framework team |
38
|
|
|
* @copyright Copyright (c) 2017 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link http://www.iacademy.cf |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @since 1.0.0 |
43
|
|
|
* @filesource |
44
|
|
|
*/ |
45
|
|
|
class Assets extends BaseClass { |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Construct new instance |
49
|
|
|
*/ |
50
|
|
|
public function __construct() { |
51
|
|
|
parent::__construct(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Generate the link of the assets file. |
56
|
|
|
* |
57
|
|
|
* Generates the absolute link of a file inside ASSETS_PATH folder. |
58
|
|
|
* For example : |
59
|
|
|
* path('foo/bar/css/style.css'); => http://mysite.com/assets/foo/bar/css/style.css |
60
|
|
|
* Note: |
61
|
|
|
* The argument passed to this function must be the relative link to the |
62
|
|
|
* folder that contains the static contents defined by the constant ASSETS_PATH. |
63
|
|
|
* |
64
|
|
|
* @param string $asset the name of the assets file path with the extension. |
65
|
|
|
* @return string|null the absolute path of the assets file, if it exists o |
66
|
|
|
* therwise returns null if the file does not exist. |
67
|
|
|
*/ |
68
|
|
|
public function path($asset) { |
69
|
|
|
$path = ASSETS_PATH . $asset; |
70
|
|
|
$this->logger->debug('Including the Assets file [' . $path . ']'); |
71
|
|
|
//Check if the file exists |
72
|
|
|
if (file_exists($path)) { |
73
|
|
|
$this->logger->info('Assets file [' . $path . '] included successfully'); |
74
|
|
|
return get_instance()->url->mainUrl($path); |
75
|
|
|
} |
76
|
|
|
$this->logger->warning('Assets file [' . $path . '] does not exist'); |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Generate the link of the css file. |
82
|
|
|
* |
83
|
|
|
* Generates the absolute link of a file containing the CSS style. |
84
|
|
|
* For example : |
85
|
|
|
* css('mystyle'); => http://mysite.com/assets/css/mystyle.css |
86
|
|
|
* Note: |
87
|
|
|
* The argument passed to this function must be the relative link to the folder that |
88
|
|
|
* contains the static contents defined by the constant ASSETS_PATH. |
89
|
|
|
* |
90
|
|
|
* @param string $path the name of the css file without the extension. |
91
|
|
|
* |
92
|
|
|
* @return string|null the absolute path of the css file, if it exists otherwise returns |
93
|
|
|
* null if the file does not exist. |
94
|
|
|
* |
95
|
|
|
* @see Assets:link |
96
|
|
|
*/ |
97
|
|
|
public function css($path) { |
98
|
|
|
return $this->link($path, 'css'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Generate the link of the javascript file. |
103
|
|
|
* |
104
|
|
|
* Generates the absolute link of a file containing the javascript. |
105
|
|
|
* For example : |
106
|
|
|
* js('myscript'); => http://mysite.com/assets/js/myscript.js |
107
|
|
|
* Note: |
108
|
|
|
* The argument passed to this function must be the relative link to |
109
|
|
|
* the folder that contains the static contents defined by the constant ASSETS_PATH. |
110
|
|
|
* |
111
|
|
|
* @param string $path the name of the javascript file without the extension. |
112
|
|
|
* |
113
|
|
|
* @return string|null the absolute path of the javascript file, |
114
|
|
|
* if it exists otherwise returns null if the file does not exist. |
115
|
|
|
* |
116
|
|
|
* @see Assets:link |
117
|
|
|
*/ |
118
|
|
|
public function js($path) { |
119
|
|
|
return $this->link($path, 'js'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Generate the link of the image file. |
124
|
|
|
* |
125
|
|
|
* Generates the absolute link of a file containing the image. |
126
|
|
|
* For example : |
127
|
|
|
* img('myimage.png'); => http://mysite.com/assets/images/myimage.png |
128
|
|
|
* Note: |
129
|
|
|
* The argument passed to this function must be the relative link to |
130
|
|
|
* the folder that contains the static contents defined by the constant ASSETS_PATH. |
131
|
|
|
* |
132
|
|
|
* @param string $path the name of the image file with the extension. |
133
|
|
|
* |
134
|
|
|
* @return string|null the absolute path of the image file, if it exists |
135
|
|
|
* otherwise returns null if the file does not exist. |
136
|
|
|
* |
137
|
|
|
* @see Assets:link |
138
|
|
|
*/ |
139
|
|
|
public function img($path) { |
140
|
|
|
return $this->link($path, 'images'); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Generate the link of the css, js, images file. |
145
|
|
|
* |
146
|
|
|
* Generates the absolute link of a file containing the CSS style, Javascript and images. |
147
|
|
|
* |
148
|
|
|
* @param string $path the name of the file without the extension. |
149
|
|
|
* @param string $type the type of asset file value can be "css", "js" and "images" |
150
|
|
|
* |
151
|
|
|
* @return string|null the absolute path of the assets file, if it exists otherwise returns |
152
|
|
|
* null if the file does not exist. |
153
|
|
|
*/ |
154
|
|
|
protected function link($path, $type) { |
155
|
|
|
/* |
156
|
|
|
* if the file name contains the extension like ".css", ".js", replace it with |
157
|
|
|
* an empty string. |
158
|
|
|
*/ |
159
|
|
|
$extension = '.' . $type; |
160
|
|
|
$filePath = str_ireplace($extension, '', $path); |
161
|
|
|
$filePath = ASSETS_PATH . $type . '/' . $filePath . $extension; |
162
|
|
|
if ($type == 'images') { |
163
|
|
|
$filePath = ASSETS_PATH . 'images/' . $path; |
164
|
|
|
} |
165
|
|
|
$this->logger->debug('Including the Assets file [' . $filePath . '] for [' . $type . ']'); |
166
|
|
|
//Check if the file exists |
167
|
|
|
if (file_exists($filePath)) { |
168
|
|
|
$this->logger->info('Assets file [' . $filePath . '] for [' . $type . '] included successfully'); |
169
|
|
|
return get_instance()->url->mainUrl($filePath); |
170
|
|
|
} |
171
|
|
|
$this->logger->warning('Assets file [' . $filePath . '] for [' . $type . '] does not exist'); |
172
|
|
|
return null; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|