-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPath.php
More file actions
144 lines (122 loc) · 4.33 KB
/
Path.php
File metadata and controls
144 lines (122 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
declare(strict_types=1);
/**
* You may not change or alter any portion of this comment or credits
* of supporting developers from this source code or any supporting source code
* which is considered copyrighted (c) material of the original comment or credit authors.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* @copyright 2000-2026 XOOPS Project (https://xoops.org/)
* @license GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
* @author XOOPS Development Team
*/
namespace Xoops\Helpers\Service;
use Xoops\Helpers\Contracts\PathLocatorInterface;
use Xoops\Helpers\Provider\DefaultPathLocator;
/**
* Static facade for XOOPS path resolution.
*
* Zero-config: works out of the box using XOOPS constants.
* Override via Path::use() for testing or custom installations.
*
* Usage:
* Path::base(); // XOOPS_ROOT_PATH
* Path::storage('caches/xmf'); // XOOPS_VAR_PATH/caches/xmf
* Path::uploads('images/logo.png'); // XOOPS_UPLOAD_PATH/images/logo.png
* Path::module('news', 'class'); // XOOPS_ROOT_PATH/modules/news/class
*/
final class Path
{
private static ?PathLocatorInterface $locator = null;
/**
* Inject a custom path locator (useful for testing).
*/
public static function use(PathLocatorInterface $locator): void
{
self::$locator = $locator;
}
/**
* Reset to the default locator.
*/
public static function reset(): void
{
self::$locator = null;
}
public static function base(string $path = ''): string
{
return self::locator()->basePath($path);
}
public static function public(string $path = ''): string
{
return self::locator()->publicPath($path);
}
public static function storage(string $path = ''): string
{
return self::locator()->storagePath($path);
}
public static function uploads(string $path = ''): string
{
return self::locator()->uploadsPath($path);
}
public static function modules(string $path = ''): string
{
return self::locator()->modulesPath($path);
}
public static function themes(string $path = ''): string
{
return self::locator()->themesPath($path);
}
public static function module(string $dirname, string $path = ''): string
{
return self::locator()->modulePath($dirname, $path);
}
public static function theme(string $name, string $path = ''): string
{
return self::locator()->themePath($name, $path);
}
/**
* Resolve the path to a module language file with English fallback.
*
* Checks for the file in the requested language directory first.
* Falls back to 'english' if the language-specific file does not exist.
* Returns the primary (most specific) path if neither file exists,
* so the caller can handle a missing file in the normal way.
*
* Replaces the standard XOOPS boilerplate:
*
* // Old — 6 lines, XOOPS_ROOT_PATH repeated 4 times
* $f = XOOPS_ROOT_PATH . '/modules/' . $dir . '/language/' . $lang . '/main.php';
* if (!is_file($f) && $lang !== 'english') {
* $f = XOOPS_ROOT_PATH . '/modules/' . $dir . '/language/english/main.php';
* }
*
* // New — one line
* $f = Path::languageFile($dir, $lang, 'main.php');
*
* @param string $dirname Module directory name
* @param string $language Language code (e.g. 'english', 'french')
* @param string $file Filename within the language directory (e.g. 'main.php')
*/
public static function languageFile(string $dirname, string $language, string $file): string
{
$primary = self::module($dirname, 'language/' . $language . '/' . $file);
if (is_file($primary)) {
return $primary;
}
if ($language !== 'english') {
$fallback = self::module($dirname, 'language/english/' . $file);
if (is_file($fallback)) {
return $fallback;
}
}
return $primary;
}
private static function locator(): PathLocatorInterface
{
return self::$locator ??= new DefaultPathLocator();
}
}