global.php ➔ src()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nc 3
nop 2
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
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 13.

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\ResourceValidator;
8
9
// Define this resource identifier
10
define('STATIC_RESOURCE_HANDLER', 'resourcer');
11
12
// Get current project web root directory
13
ResourceValidator::$webRoot = getcwd();
14
// Get current project root directory
15
ResourceValidator::$projectRoot = dirname(ResourceValidator::$webRoot);
16
17
/**
18
 * Static resource path builder.
19
 *
20
 * @deprecated Moving to use new samsonphp/view resource an $this->src() should be used
21
 *             in view file for generating paths to static resources
22
 *
23
 * @param string $path   Relative static resource resource path
24
 * @param null   $module Entity for path building, if not passed current resource is used
25
 *
26
 * @return string Static resource path
27
 * @throws \samsonphp\resource\exception\ResourceNotFound
28
 */
29
function src($path, $module = null)
30
{
31
    // Define resource
32
    switch (gettype($module)) {
33
        case 'string': // Find resource by identifier
34
            $module = m($module);
0 ignored issues
show
Deprecated Code introduced by
The function m() has been deprecated with message: Use $this->system->module() in module context

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
35
            break;
36
        case 'object': // Do nothing
37
            break;
38
        default: // Get current resource
39
            $module = m();
0 ignored issues
show
Deprecated Code introduced by
The function m() has been deprecated with message: Use $this->system->module() in module context

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
40
    }
41
42
    echo '/' . STATIC_RESOURCE_HANDLER . '/?p=' . ResourceValidator::getRelativePath($path, $module->path(), dirname(getcwd()));
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...
43
}
44
45
/** Collection of supported mime types */
46
$mimeTypes = array(
47
    'css' => 'text/css',
48
    'woff' => 'application/x-font-woff',
49
    'woff2' => 'application/x-font-woff2',
50
    'otf' => 'application/octet-stream',
51
    'ttf' => 'application/octet-stream',
52
    'eot' => 'application/vnd.ms-fontobject',
53
    'js' => 'application/x-javascript',
54
    'htm' => 'text/html;charset=utf-8',
55
    'htc' => 'text/x-component',
56
    'jpeg' => 'image/jpeg',
57
    'png' => 'image/png',
58
    'jpg' => 'image/jpg',
59
    'gif' => 'image/gif',
60
    'txt' => 'text/plain',
61
    'pdf' => 'application/pdf',
62
    'rtf' => 'application/rtf',
63
    'doc' => 'application/msword',
64
    'xls' => 'application/msexcel',
65
    'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
66
    'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
67
    'svg' => 'image/svg+xml',
68
    'mp4' => 'video/mp4',
69
    'ogg' => 'video/ogg'
70
);
71
72
// Perform custom simple URL parsing to match needed URL for static resource serving
73
$url = array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '';
74
75
// Get URL path from URL and split with "/"
76
$url = array_values(array_filter(explode('/', parse_url($url, PHP_URL_PATH))));
77
78
// Special hook to avoid further framework loading if this is static resource request
79
if (array_key_exists(0, $url) && $url[0] === STATIC_RESOURCE_HANDLER) {
80
    // Get full path to static resource
81
    $filename = realpath('../' . $_GET['p']);
82
83
    if (file_exists($filename)) {
84
        // Receive current ETag for resource(if present)
85
        $clientETag = array_key_exists('HTTP_IF_NONE_MATCH', $_SERVER) ? $_SERVER['HTTP_IF_NONE_MATCH'] : '';
86
87
        // Generate ETag for resource
88
        $serverETag = filemtime($filename);
89
90
        // Set old cache headers
91
        header('Cache-Control:max-age=1800');
92
93
        // Always set new ETag header independently on further actions
94
        header('ETag:' . $serverETag);
95
96
        // Get file extension
97
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
98
99
        // If server and client ETags are equal
100
        if ($clientETag === $serverETag) {
101
            header('HTTP/1.1 304 Not Modified');
102
        } elseif (array_key_exists($extension, $mimeTypes)) {
103
            // Set mime type
104
            header('Content-type: ' . $mimeTypes[$extension]);
105
106
            // Output resource
107
            echo file_get_contents($filename);
108
        } else { // File type is not supported
109
            header('HTTP/1.0 404 Not Found');
110
        }
111
    } else { // File not found
112
        header('HTTP/1.0 404 Not Found');
113
    }
114
115
    // Avoid further request processing
116
    die();
117
}
118