承接 symplify/smart-file-system 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

symplify/smart-file-system

最新稳定版本:11.1.26

Composer 安装命令:

composer require symplify/smart-file-system

包简介

Sanitized FileInfo with safe getRealPath() and other handy methods

README 文档

README

Downloads total

Install

composer require symplify/smart-file-system

Use

Does SplFileInfo exist?

The SplFileInfo::getRealPath() method returns absolute path to the file... or FALSE, if the file doesn't exist. This default PHP behavior forces you to check all getRealPath() calls:

$fileInfo = new SplFileInfo('non_existing_file.txt');

if ($fileInfo->getRealPath() === false) {
    // damn, the files doesn't exist
    // throw exception or whatever
    // everytime!
}

$fileRealPath = $fileInfo->getRealPath();

While this has a reason - e.g. to be sure the file was not deleted since the construction, we actually have to call the method to find out the file was removed. Another annoyance is to tell this to static analyzers.

In reality, it's very rare to work with file that was existing a while ago, but now is gone, without us doing it on purpose. We usually use SplFileInfo to modify files or work with their paths.

What if:

  • we could remove this problem and make sure getRealPath() method always returns string?
  • get an exception of non-existing file on SplFileInfo creation?

Introducing SmartFileInfo

$fileInfo = new Symplify\SmartFileSystem\SmartFileInfo('non_existing_file.txt');
// throws Symplify\SmartFileSystem\Exception\FileNotFoundException

This class also bring new useful methods:

// current directory (cwd()) is "/var/www"
$smartFileInfo = new Symplify\SmartFileSystem\SmartFileInfo('/var/www/src/ExistingFile.php');

echo $smartFileInfo->getBasenameWithoutSuffix();
// "ExistingFile"

echo $smartFileInfo->getRelativeFilePath();
// "src/ExistingFile.php"

echo $smartFileInfo->getRelativeDirectoryPath();
// "src"

echo $smartFileInfo->getRelativeFilePathFromDirectory('/var');
// "www/src/ExistingFile.php"

It also fixes WTF behavior of Symfony\Component\Finder\SplFileInfo. Which one? When you run e.g. vendor/bin/ecs check src and use Finder, the getRelativeFilePath() in Symfony now returns all the relative paths to src. Which is useless, mainly with multiple dirs like: vendor/bin/ecs check src tests both containing file Post.php.

$smartFileInfo = new Symplify\SmartFileSystem\SmartFileInfo('/var/www/src/Post.php');

echo $smartFileInfo->getRelativeFilePathFromCwd();
// "src/Post.php"

File name Matching

Last but not least, matching a file comes useful in excluding files (typical for tools like ECS, PHPStan, Psalm, Rector, PHP CS Fixer or PHP_CodeSniffer):

$smartFileInfo = new Symplify\SmartFileSystem\SmartFileInfo('/var/www/src/PostRepository.php');

echo $smartFileInfo->endsWith('Repository.php');
// true

echo $smartFileInfo->doesFnmatch('*Repo*');
// true

Smart FileSystem - Just like Symfony, just Better

New method - readFile() (to read files):

$smartFileSystem = new Symplify\SmartFileSystem\SmartFileSystem();
$fileContent = $smartFileSystem->readFile(__DIR__ . '/SomeFile.php');
// if you plan to use SmartFileInfo, use this
$smartFileInfo = $smartFileSystem->readFileToSmartFileInfo(__DIR__ . '/SomeFile.php');

Sanitizer various files to SmartFileInfo[]

Do you have multiple file inputs that can mix-up?

$files = [new SplFileInfo('someFile.php')];

$files = [new Symfony\Component\Finder\SplFileInfo('someFile.php', 'someFile', '')];

// or
$files = (new Symfony\Component\Finder\Finder())->files();

// or
$files = ['someFile.php'];

Later, you wan to actually work with the files:

foreach ($files as $file) {
    // what methods do we have here
    // what kind of object?
    // is it even object or a string?
    $file->...
}

Use sanitized files, that have united format you can rely on:

use Symplify\SmartFileSystem\Finder\FinderSanitizer;

$finderSanitizer = new FinderSanitizer();
$smartFileInfos = $finderSanitizer->sanitize($files);

// always array of Symplify\SmartFileSystem\SmartFileInfo
var_dump($smartFileInfos);

Report Issues

In case you are experiencing a bug or want to request a new feature head over to the Symplify monorepo issue tracker

Contribute

The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.

symplify/smart-file-system 适用场景与选型建议

symplify/smart-file-system 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.15M 次下载、GitHub Stars 达 78, 最近一次更新时间为 2026 年 01 月 04 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 symplify/smart-file-system 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 symplify/smart-file-system 我们能提供哪些服务?
定制开发 / 二次开发

基于 symplify/smart-file-system 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 12.15M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 79
  • 点击次数: 22
  • 依赖项目数: 28
  • 推荐数: 0

GitHub 信息

  • Stars: 78
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04