1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by Vitaly Iegorov <[email protected]>. |
4
|
|
|
* on 15.05.16 at 10:52 |
5
|
|
|
*/ |
6
|
|
|
namespace samsonphp\resource; |
7
|
|
|
|
8
|
|
|
use samsonphp\resource\exception\ResourceNotFound; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Static resource entity class. |
12
|
|
|
* |
13
|
|
|
* @package samsonphp\resource |
14
|
|
|
*/ |
15
|
|
|
class Resource |
16
|
|
|
{ |
17
|
|
|
/** @var string Full path to project web root directory */ |
18
|
|
|
public static $webRoot; |
19
|
|
|
|
20
|
|
|
/** @var string Full path to project root directory */ |
21
|
|
|
public static $projectRoot; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Build relative path to static resource relatively to web root path. |
25
|
|
|
* |
26
|
|
|
* @param string $relativePath Relative path to static resource |
27
|
|
|
* @param string $parentPath Path to parent entity |
28
|
|
|
* |
29
|
|
|
* @return string Validated relative path to static resource relatively to web root path |
30
|
|
|
* @throws ResourceNotFound |
31
|
|
|
*/ |
32
|
|
|
public static function getWebRelativePath($relativePath, $parentPath = '') |
33
|
|
|
{ |
34
|
|
|
return static::getRelativePath($relativePath, $parentPath, static::$webRoot); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Build correct relative path to static resource using relative path and parent path. |
39
|
|
|
* |
40
|
|
|
* @param string $relativePath Relative path to static resource |
41
|
|
|
* @param string $parentPath Path to parent entity |
42
|
|
|
* @param string $rootPath Root path for relative path building |
43
|
|
|
* |
44
|
|
|
* @return string Validated relative path to static resource |
45
|
|
|
* @throws ResourceNotFound |
46
|
|
|
*/ |
47
|
|
|
public static function getRelativePath($relativePath, $parentPath = '', $rootPath = '') |
48
|
|
|
{ |
49
|
|
|
// If parent path if not passed - use project root path |
50
|
|
|
$parentPath = $parentPath === '' ? static::$projectRoot : $parentPath; |
51
|
|
|
|
52
|
|
|
// Build full path to resource from given relative path |
53
|
|
|
$fullPath = rtrim($parentPath, DIRECTORY_SEPARATOR) |
54
|
|
|
.DIRECTORY_SEPARATOR |
55
|
|
|
.ltrim($relativePath, DIRECTORY_SEPARATOR); |
56
|
|
|
|
57
|
|
|
// Make real path with out possible "../" |
58
|
|
|
$realPath = realpath($fullPath); |
59
|
|
|
|
60
|
|
|
// Output link to static resource handler with relative path to project root |
61
|
|
|
if ($realPath) { |
62
|
|
|
// Build relative path to static resource |
63
|
|
|
return str_replace($rootPath, '', $realPath); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
throw new ResourceNotFound($fullPath); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Build relative path to static resource relatively to project root path. |
71
|
|
|
* |
72
|
|
|
* @param string $relativePath Relative path to static resource |
73
|
|
|
* @param string $parentPath Path to parent entity |
74
|
|
|
* |
75
|
|
|
* @return string Validated relative path to static resource relatively to project root path |
76
|
|
|
* @throws ResourceNotFound |
77
|
|
|
*/ |
78
|
|
|
public static function getProjectRelativePath($relativePath, $parentPath = '') |
79
|
|
|
{ |
80
|
|
|
return static::getRelativePath($relativePath, $parentPath, static::$projectRoot); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|