ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
LegacyPathHelper.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21namespace ILIAS\Filesystem\Util;
22
26
35{
37
38 public static function deriveLocationFrom(string $absolute_path): int
39 {
40 [
41 $web,
42 $webRelativeWithLeadingDot,
43 $webRelativeWithoutLeadingDot,
44 $storage,
45 $customizing,
46 $customizingRelativeWithLeadingDot,
47 $libs,
48 $libsRelativeWithLeadingDot,
49 $temp,
50 $nodeModules,
51 $nodeModulesWithLeadingDot
52 ] = self::listPaths();
53
54 return match (true) {
55 self::checkPossiblePath($temp, $absolute_path) => Location::TEMPORARY,
57 $webRelativeWithLeadingDot,
58 $absolute_path
59 ), self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path) => Location::WEB,
60 self::checkPossiblePath($storage, $absolute_path) => Location::STORAGE,
61 self::checkPossiblePath($customizing, $absolute_path), self::checkPossiblePath(
62 $customizingRelativeWithLeadingDot,
63 $absolute_path
65 default => throw new \InvalidArgumentException(
66 "Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'"
67 ),
68 };
69 }
70
84 public static function deriveFilesystemFrom(string $absolute_path): Filesystem
85 {
86 [
87 $web,
88 $webRelativeWithLeadingDot,
89 $webRelativeWithoutLeadingDot,
90 $storage,
91 $customizing,
92 $customizingRelativeWithLeadingDot,
93 $libs,
94 $libsRelativeWithLeadingDot,
95 $temp,
96 $nodeModules,
97 $nodeModulesWithLeadingDot
98 ] = self::listPaths();
99
100 return match (true) {
101 self::checkPossiblePath($temp, $absolute_path) => self::filesystems()->temp(),
102 self::checkPossiblePath($web, $absolute_path) => self::filesystems()->web(),
103 self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path) => self::filesystems()->web(),
104 self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path) => self::filesystems()->web(),
105 self::checkPossiblePath($storage, $absolute_path) => self::filesystems()->storage(),
106 self::checkPossiblePath($customizing, $absolute_path) => self::filesystems()->customizing(),
107 self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path) => self::filesystems(
108 )->customizing(),
109 self::checkPossiblePath($libs, $absolute_path) => self::filesystems()->libs(),
110 self::checkPossiblePath($libsRelativeWithLeadingDot, $absolute_path) => self::filesystems()->libs(),
111 self::checkPossiblePath($nodeModules, $absolute_path) => self::filesystems()->nodeModules(),
112 self::checkPossiblePath($nodeModulesWithLeadingDot, $absolute_path) => self::filesystems()->nodeModules(),
113 default => throw new \InvalidArgumentException(
114 "Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'"
115 ),
116 };
117 }
118
131 public static function createRelativePath(string $absolute_path): string
132 {
133 [
134 $web,
135 $webRelativeWithLeadingDot,
136 $webRelativeWithoutLeadingDot,
137 $storage,
138 $customizing,
139 $customizingRelativeWithLeadingDot,
140 $libs,
141 $libsRelativeWithLeadingDot,
142 $temp,
143 $nodeModules,
144 $nodeModulesWithLeadingDot
145 ] = self::listPaths();
146
147 return match (true) {
148 self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path) => self::resolveRelativePath(
149 $webRelativeWithoutLeadingDot,
150 $absolute_path
151 ),
152 self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path) => self::resolveRelativePath(
153 $webRelativeWithLeadingDot,
154 $absolute_path
155 ),
156 self::checkPossiblePath($web, $absolute_path) => self::resolveRelativePath($web, $absolute_path),
157 self::checkPossiblePath($temp, $absolute_path) => self::resolveRelativePath($temp, $absolute_path),
158 self::checkPossiblePath($storage, $absolute_path) => self::resolveRelativePath($storage, $absolute_path),
159 self::checkPossiblePath($customizing, $absolute_path) => self::resolveRelativePath(
160 $customizing,
161 $absolute_path
162 ),
163 self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path) => self::resolveRelativePath(
164 $customizingRelativeWithLeadingDot,
165 $absolute_path
166 ),
167 self::checkPossiblePath($libs, $absolute_path), self::checkPossiblePath(
168 $libsRelativeWithLeadingDot,
169 $absolute_path
170 ) => self::resolveRelativePath($libsRelativeWithLeadingDot, $absolute_path),
171 self::checkPossiblePath($nodeModules, $absolute_path), self::checkPossiblePath(
172 $nodeModulesWithLeadingDot,
173 $absolute_path
174 ) => self::resolveRelativePath($nodeModulesWithLeadingDot, $absolute_path),
175 default => throw new \InvalidArgumentException(
176 "Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'"
177 ),
178 };
179 }
180
181 private static function resolveRelativePath(string $possible_path, string $absolute_path): string
182 {
183 $real_possible_path = realpath($possible_path);
184
185 return match (true) {
186 $possible_path === $absolute_path, $real_possible_path === $absolute_path => "",
187 str_starts_with($absolute_path, $possible_path) => substr(
188 $absolute_path,
189 strlen($possible_path) + 1
190 ),
191 str_starts_with($absolute_path, $real_possible_path) => substr(
192 $absolute_path,
193 strlen($real_possible_path) + 1
194 ),
195 default => throw new \InvalidArgumentException(
196 "Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'"
197 ),
198 };
199 }
200
201 private static function checkPossiblePath(string $possible_path, string $absolute_path): bool
202 {
203 $real_possible_path = realpath($possible_path);
204
205 return match (true) {
206 $possible_path === $absolute_path => true,
207 $real_possible_path === $absolute_path => true,
208 is_string($possible_path) && str_starts_with($absolute_path, $possible_path) => true,
209 is_string($real_possible_path) && str_starts_with($absolute_path, $real_possible_path) => true,
210 default => false,
211 };
212 }
213
217 private static function listPaths(): array
218 {
219 $web = CLIENT_WEB_DIR;
220 $webRelativeWithLeadingDot = './' . ILIAS_WEB_DIR . '/' . CLIENT_ID;
221 $webRelativeWithoutLeadingDot = ILIAS_WEB_DIR . '/' . CLIENT_ID;
222 $storage = CLIENT_DATA_DIR;
223 $customizing = ILIAS_ABSOLUTE_PATH . '/public/Customizing';
224 $customizingRelativeWithLeadingDot = './Customizing';
225 $libs = ILIAS_ABSOLUTE_PATH . '/vendor';
226 $libsRelativeWithLeadingDot = "./vendor";
227 $temp = CLIENT_DATA_DIR . "/temp";
228 $nodeModules = ILIAS_ABSOLUTE_PATH . '/node_modules';
229 $nodeModulesWithLeadingDot = './node_modules';
230
231 return [
232 $web,
233 $webRelativeWithLeadingDot,
234 $webRelativeWithoutLeadingDot,
235 $storage,
236 $customizing,
237 $customizingRelativeWithLeadingDot,
238 $libs,
239 $libsRelativeWithLeadingDot,
240 $temp,
241 $nodeModules,
242 $nodeModulesWithLeadingDot
243 ];
244 }
245}
The legacy path helper provides convenient functions for the integration of the filesystem service wi...
static createRelativePath(string $absolute_path)
Creates a relative path from an absolute path which starts with a valid storage location.
static deriveFilesystemFrom(string $absolute_path)
Tries to fetch the filesystem responsible for the absolute path.
static deriveLocationFrom(string $absolute_path)
static checkPossiblePath(string $possible_path, string $absolute_path)
static resolveRelativePath(string $possible_path, string $absolute_path)
trait FilesystemsAware
Trait which ease the filesystem integration within legacy ILIAS components.
const CLIENT_ID
Definition: constants.php:41
const ILIAS_WEB_DIR
Definition: constants.php:45
const CLIENT_WEB_DIR
Definition: constants.php:47
const CLIENT_DATA_DIR
Definition: constants.php:46
Interface Location.
Definition: Location.php:33
const TEMPORARY
The ILIAS temporary directory.
Definition: Location.php:53
const CUSTOMIZING
The filesystem within the web root where all the skins and plugins are saved.
Definition: Location.php:48
const WEB
The filesystem within the ilias web root.
Definition: Location.php:38
const STORAGE
The filesystem outside of the ilias web root.
Definition: Location.php:43
The filesystem interface provides the public interface for the Filesystem service API consumer.
Definition: Filesystem.php:37
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static filesystems()
Returns the loaded filesystems.