1 | <?php declare(strict_types=1); |
||
15 | class ResourceValidator |
||
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 | /** @var string Full path to project cache root directory */ |
||
24 | public static $cacheRoot; |
||
25 | |||
26 | /** |
||
27 | * Build relative path to static resource relatively to web root path. |
||
28 | * |
||
29 | * @param string $relativePath Relative path to static resource |
||
30 | * @param string $parentPath Path to parent entity |
||
31 | * |
||
32 | * @return string Validated relative path to static resource relatively to web root path |
||
33 | * @throws ResourceNotFound |
||
34 | */ |
||
35 | public static function getWebRelativePath($relativePath, $parentPath = '') |
||
39 | |||
40 | /** |
||
41 | * Build correct relative path to static resource using relative path and parent path. |
||
42 | * |
||
43 | * @param string $relativePath Relative path to static resource |
||
44 | * @param string $parentPath Path to parent entity |
||
45 | * @param string $rootPath Root path for relative path building |
||
46 | * |
||
47 | * @return string Validated relative path to static resource |
||
48 | * @throws ResourceNotFound |
||
49 | */ |
||
50 | public static function getRelativePath($relativePath, $parentPath = '', $rootPath = '') |
||
51 | { |
||
52 | // If parent path if not passed - use project root path |
||
53 | $parentPath = $parentPath === '' ? static::$projectRoot : $parentPath; |
||
54 | |||
55 | // Build full path to resource from given relative path |
||
56 | $fullPath = rtrim($parentPath, DIRECTORY_SEPARATOR) |
||
57 | . DIRECTORY_SEPARATOR |
||
58 | . ltrim($relativePath, DIRECTORY_SEPARATOR); |
||
59 | |||
60 | // Make real path with out possible "../" |
||
61 | $realPath = realpath($fullPath); |
||
62 | |||
63 | // Output link to static resource handler with relative path to project root |
||
64 | if ($realPath) { |
||
65 | // Build relative path to static resource |
||
66 | return str_replace($rootPath, '', $realPath); |
||
67 | } |
||
68 | |||
69 | throw new ResourceNotFound($fullPath); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Build relative path to static resource relatively to project root path. |
||
74 | * |
||
75 | * @param string $relativePath Relative path to static resource |
||
76 | * @param string $parentPath Path to parent entity |
||
77 | * |
||
78 | * @return string Validated relative path to static resource relatively to project root path |
||
79 | * @throws ResourceNotFound |
||
80 | */ |
||
81 | public static function getProjectRelativePath($relativePath, $parentPath = '') |
||
85 | } |