for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace nebula\application\filesystem;
use nebula\component\loader\PathTrait;
/**
* 文件路径推测
* 包括非UTF-8路径以及大小写敏感
*/
class CharsetGuesser
{
protected $gusse = ['GBK','GB2312','BIG5'];
protected $realPath;
protected $path;
public function __construct(string $path, array $charset = [])
$this->path = $this->toAbsolutePath($path);
$this->gusse = count($charset) > 0? $charset : $this->gusse;
}
* 获取真实路径
*
* @return string
public function getRealPath():string
return $this->exist() ? (realpath($this->path) ?? $this->realPath): '';
* 判断文件是否存在
* @return boolean
public function exist():bool
if ($this->existCase($this->path)) {
return true;
foreach ($this->gusse as $code) {
$file = iconv('UTF-8', $code, $this->path);
$abspath = $this->toAbsolutePath($file);
if ($file !== false && $this->existCase($abspath)) {
$this->realPath = $abspath;
return false;
* 正常文件系统中可以判断文件大小写
* @param string $path
protected function existCase(string $abspath):bool
// 真实文件系统
if (realpath($abspath) === $abspath) {
// 虚拟文件系统
if (\file_exists($abspath)) {
foreach (stream_get_wrappers() as $wrapper) {
if (strpos($abspath, $wrapper.'://') === 0) {
protected function toAbsolutePath(string $path)
return PathTrait::toAbsolutePath($path);