1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 26.04.16 at 10:43 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
use samsonphp\resource\Resource; |
8
|
|
|
|
9
|
|
|
// Define this module identifier |
10
|
|
|
define('STATIC_RESOURCE_HANDLER', 'resourcer'); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Static resource path builder. |
14
|
|
|
* |
15
|
|
|
* @deprecated Moving to use new samsonphp/view module an $this->src() should be used |
16
|
|
|
* in view file for generating paths to static resources |
17
|
|
|
* |
18
|
|
|
* @param string $path Relative static resource module path |
19
|
|
|
* @param null $module Entity for path building, if not passed current module is used |
20
|
|
|
* |
21
|
|
|
* @return string Static resource path |
22
|
|
|
* @throws \samsonphp\resource\exception\ResourceNotFound |
23
|
|
|
*/ |
24
|
|
|
function src($path, $module = null) |
25
|
|
|
{ |
26
|
|
|
// Define module |
27
|
|
|
switch (gettype($module)) { |
28
|
|
|
case 'string': // Find module by identifier |
29
|
|
|
$module = m($module); |
30
|
|
|
break; |
31
|
|
|
case 'object': // Do nothing |
32
|
|
|
break; |
33
|
|
|
default: // Get current module |
34
|
|
|
$module = m(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
echo '/'.STATIC_RESOURCE_HANDLER.'/?p='. Resource::getRelativePath($path, $module->path()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** Collection of supported mime types */ |
41
|
|
|
$mimeTypes = array( |
42
|
|
|
'css' => 'text/css', |
43
|
|
|
'woff' => 'application/x-font-woff', |
44
|
|
|
'woff2' => 'application/x-font-woff2', |
45
|
|
|
'otf' => 'application/octet-stream', |
46
|
|
|
'ttf' => 'application/octet-stream', |
47
|
|
|
'eot' => 'application/vnd.ms-fontobject', |
48
|
|
|
'js' => 'application/x-javascript', |
49
|
|
|
'htm' => 'text/html;charset=utf-8', |
50
|
|
|
'htc' => 'text/x-component', |
51
|
|
|
'jpeg' => 'image/jpeg', |
52
|
|
|
'png' => 'image/png', |
53
|
|
|
'jpg' => 'image/jpg', |
54
|
|
|
'gif' => 'image/gif', |
55
|
|
|
'txt' => 'text/plain', |
56
|
|
|
'pdf' => 'application/pdf', |
57
|
|
|
'rtf' => 'application/rtf', |
58
|
|
|
'doc' => 'application/msword', |
59
|
|
|
'xls' => 'application/msexcel', |
60
|
|
|
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
61
|
|
|
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
62
|
|
|
'svg' => 'image/svg+xml', |
63
|
|
|
'mp4' => 'video/mp4', |
64
|
|
|
'ogg' => 'video/ogg' |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
// Perform custom simple URL parsing to match needed URL for static resource serving |
68
|
|
|
$url = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : ''; |
69
|
|
|
|
70
|
|
|
// Get URL path from URL and split with "/" |
71
|
|
|
$url = array_values(array_filter(explode('/', parse_url($url, PHP_URL_PATH)))); |
72
|
|
|
|
73
|
|
|
// Special hook to avoid further framework loading if this is static resource request |
74
|
|
|
if (array_key_exists(0, $url) && $url[0] === STATIC_RESOURCE_HANDLER) { |
75
|
|
|
// Get full path to static resource |
76
|
|
|
$filename = realpath('../' . $_GET['p']); |
77
|
|
|
|
78
|
|
|
if (file_exists($filename)) { |
79
|
|
|
// Receive current ETag for resource(if present) |
80
|
|
|
$clientETag = array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER) ? $_SERVER['HTTP_IF_NONE_MATCH'] : ''; |
81
|
|
|
|
82
|
|
|
// Generate ETag for resource |
83
|
|
|
$serverETag = filemtime($filename); |
84
|
|
|
|
85
|
|
|
// Set old cache headers |
86
|
|
|
header('Cache-Control:max-age=1800'); |
87
|
|
|
|
88
|
|
|
// Always set new ETag header independently on further actions |
89
|
|
|
header('ETag:' . $serverETag); |
90
|
|
|
|
91
|
|
|
// Get file extension |
92
|
|
|
$extension = pathinfo($filename, PATHINFO_EXTENSION); |
93
|
|
|
|
94
|
|
|
// If server and client ETags are equal |
95
|
|
|
if ($clientETag === $serverETag) { |
96
|
|
|
header('HTTP/1.1 304 Not Modified'); |
97
|
|
|
} elseif (array_key_exists($extension, $mimeTypes)) { |
98
|
|
|
// Set mime type |
99
|
|
|
header('Content-type: ' . $mimeTypes[$extension]); |
100
|
|
|
|
101
|
|
|
// Output resource |
102
|
|
|
echo file_get_contents($filename); |
103
|
|
|
} else { // File type is not supported |
104
|
|
|
header('HTTP/1.0 404 Not Found'); |
105
|
|
|
} |
106
|
|
|
} else { // File not found |
107
|
|
|
header('HTTP/1.0 404 Not Found'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Avoid further request processing |
111
|
|
|
die(); |
112
|
|
|
} |
113
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.