ILIAS  release_7 Revision v7.30-3-g800a261c036
LegacyPathHelper.php
Go to the documentation of this file.
1<?php
2declare(strict_types=1);
3
5
8
18{
20
29 public static function deriveFilesystemFrom(string $absolute_path) : Filesystem
30 {
31 list(
32 $web,
33 $webRelativeWithLeadingDot,
34 $webRelativeWithoutLeadingDot,
35 $storage,
36 $customizing,
37 $customizingRelativeWithLeadingDot,
38 $libs,
39 $libsRelativeWithLeadingDot,
40 $temp,
41 $nodeModules,
42 $nodeModulesWithLeadingDot
43 ) = self::listPaths();
44
45 switch (true) {
46 case self::checkPossiblePath($temp, $absolute_path):
47 return self::filesystems()->temp();
48 case self::checkPossiblePath($web, $absolute_path):
49 return self::filesystems()->web();
50 case self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path):
51 return self::filesystems()->web();
52 case self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path):
53 return self::filesystems()->web();
54 case self::checkPossiblePath($storage, $absolute_path):
55 return self::filesystems()->storage();
56 case self::checkPossiblePath($customizing, $absolute_path):
57 return self::filesystems()->customizing();
58 case self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path):
59 return self::filesystems()->customizing();
60 case self::checkPossiblePath($libs, $absolute_path):
61 return self::filesystems()->libs();
62 case self::checkPossiblePath($libsRelativeWithLeadingDot, $absolute_path):
63 return self::filesystems()->libs();
64 case self::checkPossiblePath($nodeModules, $absolute_path):
65 return self::filesystems()->nodeModules();
66 case self::checkPossiblePath($nodeModulesWithLeadingDot, $absolute_path):
67 return self::filesystems()->nodeModules();
68 default:
69 throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
70 }
71 }
72
81 public static function createRelativePath(string $absolute_path) : string
82 {
83 list(
84 $web,
85 $webRelativeWithLeadingDot,
86 $webRelativeWithoutLeadingDot,
87 $storage,
88 $customizing,
89 $customizingRelativeWithLeadingDot,
90 $libs,
91 $libsRelativeWithLeadingDot,
92 $temp,
93 $nodeModules,
94 $nodeModulesWithLeadingDot
95 ) = self::listPaths();
96
97 switch (true) {
98 // web without ./
99 case self::checkPossiblePath($webRelativeWithoutLeadingDot, $absolute_path):
100 return self::resolveRelativePath($webRelativeWithoutLeadingDot, $absolute_path);
101 // web with ./
102 case self::checkPossiblePath($webRelativeWithLeadingDot, $absolute_path):
103 return self::resolveRelativePath($webRelativeWithLeadingDot, $absolute_path);
104 // web/
105 case self::checkPossiblePath($web, $absolute_path):
106 return self::resolveRelativePath($web, $absolute_path);
107 // temp/
108 case self::checkPossiblePath($temp, $absolute_path):
109 return self::resolveRelativePath($temp, $absolute_path);
110 // iliasdata/
111 case self::checkPossiblePath($storage, $absolute_path):
112 return self::resolveRelativePath($storage, $absolute_path);
113 // Customizing/
114 case self::checkPossiblePath($customizing, $absolute_path):
115 return self::resolveRelativePath($customizing, $absolute_path);
116 // ./Customizing/
117 case self::checkPossiblePath($customizingRelativeWithLeadingDot, $absolute_path):
118 return self::resolveRelativePath($customizingRelativeWithLeadingDot, $absolute_path);
119 // libs/
120 case self::checkPossiblePath($libs, $absolute_path):
121 // ./libs
122 case self::checkPossiblePath($libsRelativeWithLeadingDot, $absolute_path):
123 return self::resolveRelativePath($libsRelativeWithLeadingDot, $absolute_path);
124 // node_modules/
125 case self::checkPossiblePath($nodeModules, $absolute_path):
126 // ./node_modules
127 case self::checkPossiblePath($nodeModulesWithLeadingDot, $absolute_path):
128 return self::resolveRelativePath($nodeModulesWithLeadingDot, $absolute_path);
129 default:
130 throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
131 }
132 }
133
134 private static function resolveRelativePath(string $possible_path, string $absolute_path) : string
135 {
136 $real_possible_path = realpath($possible_path);
137
138 switch (true) {
139 case $possible_path === $absolute_path:
140 case $real_possible_path === $absolute_path:
141 return "";
142 case strpos($absolute_path, $possible_path) === 0:
143 return substr($absolute_path,
144 strlen($possible_path) + 1); //also remove the trailing slash
145 case strpos($absolute_path, $real_possible_path) === 0:
146 return substr($absolute_path,
147 strlen($real_possible_path) + 1); //also remove the trailing slash
148 default:
149 throw new \InvalidArgumentException("Invalid path supplied. Path must start with the web, storage, temp, customizing or libs storage location. Path given: '{$absolute_path}'");
150 }
151 }
152
158 private static function checkPossiblePath(string $possible_path, string $absolute_path) : bool
159 {
160 $real_possible_path = realpath($possible_path);
161
162 switch (true) {
163 case $possible_path === $absolute_path:
164 return true;
165 case $real_possible_path === $absolute_path:
166 return true;
167 case is_string($possible_path) && strpos($absolute_path, $possible_path) === 0:
168 return true;
169 case is_string($real_possible_path) && strpos($absolute_path, $real_possible_path) === 0:
170 return true;
171 default:
172 return false;
173 }
174 }
175
179 private static function listPaths() : array
180 {
181 $web = CLIENT_WEB_DIR;
182 $webRelativeWithLeadingDot = './' . ILIAS_WEB_DIR . '/' . CLIENT_ID;
183 $webRelativeWithoutLeadingDot = ILIAS_WEB_DIR . '/' . CLIENT_ID;
184 $storage = CLIENT_DATA_DIR;
185 $customizing = ILIAS_ABSOLUTE_PATH . '/Customizing';
186 $customizingRelativeWithLeadingDot = './Customizing';
187 $libs = ILIAS_ABSOLUTE_PATH . '/libs';
188 $libsRelativeWithLeadingDot = "./libs";
189 $temp = CLIENT_DATA_DIR . "/temp";
190 $nodeModules = ILIAS_ABSOLUTE_PATH . '/node_modules';
191 $nodeModulesWithLeadingDot = './node_modules';
192
193 return array($web,
194 $webRelativeWithLeadingDot,
195 $webRelativeWithoutLeadingDot,
196 $storage,
197 $customizing,
198 $customizingRelativeWithLeadingDot,
199 $libs,
200 $libsRelativeWithLeadingDot,
201 $temp,
202 $nodeModules,
203 $nodeModulesWithLeadingDot
204 );
205 }
206}
An exception for terminatinating execution or to throw for unit testing.
Class LegacyPathHelper The legacy path helper provides convenient functions for the integration of th...
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 checkPossiblePath(string $possible_path, string $absolute_path)
static resolveRelativePath(string $possible_path, string $absolute_path)
const CLIENT_ID
Definition: constants.php:39
const ILIAS_WEB_DIR
Definition: constants.php:43
const CLIENT_WEB_DIR
Definition: constants.php:45
const CLIENT_DATA_DIR
Definition: constants.php:44
Interface Filesystem The filesystem interface provides the public interface for the Filesystem servic...
Definition: Filesystem.php:22
Class FlySystemFileAccessTest \Provider\FlySystem @runTestsInSeparateProcesses @preserveGlobalState d...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
trait FilesystemsAware
Trait FilesystemsAware.
static filesystems()
Returns the loaded filesystems.