Bearsampp 2026.5.5
Loading...
Searching...
No Matches
class.util.path.php
Go to the documentation of this file.
1<?php
2/*
3 *
4 * * Copyright (c) 2022-2025 Bearsampp
5 * * License: GNU General Public License version 3 or later; see LICENSE.txt
6 * * Website: https://bearsampp.com
7 * * Github: https://github.com/Bearsampp
8 *
9 */
10
25{
30 private static $pathFormatCache = [];
31
36 private static $pathFormatCacheMaxSize = 500;
37
42 private static $pathFormatStats = [
43 'unix_hits' => 0,
44 'unix_misses' => 0,
45 'windows_hits' => 0,
46 'windows_misses' => 0,
47 ];
48
60 public static function formatWindowsPath($path)
61 {
62 if (empty($path)) {
63 return $path;
64 }
65
66 $cacheKey = 'w_' . $path;
67 if (isset(self::$pathFormatCache[$cacheKey])) {
68 self::$pathFormatStats['windows_hits']++;
69 return self::$pathFormatCache[$cacheKey];
70 }
71
72 self::$pathFormatStats['windows_misses']++;
73
74 $result = str_replace('/', '\\', $path);
75
76 if (count(self::$pathFormatCache) < self::$pathFormatCacheMaxSize) {
77 self::$pathFormatCache[$cacheKey] = $result;
78 } else {
79 $removeCount = (int)(self::$pathFormatCacheMaxSize * 0.1);
80 self::$pathFormatCache = array_slice(self::$pathFormatCache, $removeCount, null, true);
81 self::$pathFormatCache[$cacheKey] = $result;
82 }
83
84 return $result;
85 }
86
98 public static function formatUnixPath($path)
99 {
100 if (empty($path)) {
101 return $path;
102 }
103
104 $cacheKey = 'u_' . $path;
105 if (isset(self::$pathFormatCache[$cacheKey])) {
106 self::$pathFormatStats['unix_hits']++;
107 return self::$pathFormatCache[$cacheKey];
108 }
109
110 self::$pathFormatStats['unix_misses']++;
111
112 $result = str_replace('\\', '/', $path);
113
114 if (count(self::$pathFormatCache) < self::$pathFormatCacheMaxSize) {
115 self::$pathFormatCache[$cacheKey] = $result;
116 } else {
117 $removeCount = (int)(self::$pathFormatCacheMaxSize * 0.1);
118 self::$pathFormatCache = array_slice(self::$pathFormatCache, $removeCount, null, true);
119 self::$pathFormatCache[$cacheKey] = $result;
120 }
121
122 return $result;
123 }
124
131 public static function getPathFormatStats()
132 {
133 return self::$pathFormatStats;
134 }
135
142 public static function clearPathFormatCache()
143 {
144 self::$pathFormatCache = [];
145 self::$pathFormatStats = [
146 'unix_hits' => 0,
147 'unix_misses' => 0,
148 'windows_hits' => 0,
149 'windows_misses' => 0,
150 ];
151 }
152
158 public static function getPathFormatCacheSize()
159 {
160 return count(self::$pathFormatCache);
161 }
162}
$result
static getPathFormatCacheSize()
static clearPathFormatCache()
static formatWindowsPath($path)
static formatUnixPath($path)
static $pathFormatCacheMaxSize
static $pathFormatStats
static getPathFormatStats()
static $pathFormatCache