Completed
Push — master ( cc0315...505aa7 )
by Vitaly
02:04
created

global.php ➔ src()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 3
Metric Value
cc 3
eloc 10
c 5
b 0
f 3
nc 3
nop 2
dl 0
loc 15
rs 9.4285
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 41.

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.

Loading history...
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());
0 ignored issues
show
Bug introduced by
It seems like $module is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
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