定制 fidry/filesystem 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

fidry/filesystem

Composer 安装命令:

composer require fidry/filesystem

包简介

Symfony Filesystem with a few more utilities.

关键字:

README 文档

README

This is a tiny wrapper around the Symfony filesystem. It provides a FileSystem interface and a few more utilities.

New methods

interface FileSystem extends SymfonyFileSystem
{
    /**
     * Replaces the path directory separator with the system one.
     *
     * For example, on Windows:
     * 'C:/path/to/file' => 'C:\path\to\file',
     */
    public function escapePath(string $path): string;

    /**
     * Returns the absolute path, but the path will not be normalized.
     *
     * For example, `::realpath('C:\Users\Name\file.txt')` on Windows will
     * return "C:\Users\Name\file.txt" (backslashes).
     *
     * @see https://php.net/manual/en/function.realpath.php
     *
     * @throws IOException When the file or symlink target does not exist.
     */
    public function realPath(string $file): string;

    /**
     * Returns the absolute normalized path.
     *
     * For example, `::realpath('C:\Users\Name\file.txt')` on Windows will
     * return "C:/Users/Name/file.txt".
     *
     * @see https://php.net/manual/en/function.realpath.php
     *
     * @throws IOException When the file or symlink target does not exist.
     */
    public function normalizedRealPath(string $file): string;

    /**
     * Creates a temporary file with support for custom stream wrappers. Same as tempnam(),
     * but targets the system default temporary directory by default and has a more consistent
     * name with tmpDir.
     *
     * For example:
     *
     *  ```php
     *  tmpFile('build')
     *
     *  // on OSX
     *  => '/var/folders/p3/lkw0cgjj2fq0656q_9rd0mk80000gn/T/build8d9e0f1a'
     *  // on Windows
     *  => C:\Windows\Temp\build8d9e0f1a.tmp
     *  ```
     *
     * @param string $prefix          The prefix of the generated temporary file name.
     * @param string $suffix          The suffix of the generated temporary file name.
     * @param string $targetDirectory The directory where to create the temporary directory.
     *                                Defaults to the system default temporary directory.
     *
     * @return string The new temporary file pathname.
     *
     * @throws IOException
     *
     * @see tempnam()
     * @see SymfonyFileSystem::tempnam()
     * @see self::tmpDir()
     */
    public function tmpFile(string $prefix, string $suffix = '', ?string $targetDirectory = null): string;

    /**
     * Creates a temporary directory with support for custom stream wrappers. Similar to tempnam()
     * but creates a directory instead of a file.
     *
     * For example:
     *
     * ```php
     * tmpDir('build')
     *
     * // on OSX
     * => '/var/folders/p3/lkw0cgjj2fq0656q_9rd0mk80000gn/T/build8d9e0f1a'
     * // on Windows
     * => C:\Windows\Temp\build8d9e0f1a.tmp
     * ```
     *
     * @param string|null $prefix          The prefix of the generated temporary directory name.
     * @param string      $targetDirectory The directory where to create the temporary directory.
     *                                     Defaults to the system default temporary directory.
     *
     * @throws IOException
     *
     * @return string The new temporary directory pathname.
     *
     * @see tempnam()
     */
    public function tmpDir(string $prefix, ?string $targetDirectory = null): string;

    /**
     * Tells whether a file exists and is readable.
     *
     * @throws IOException When Window's path is longer than 258 characters
     */
    public function isReadable(string $filename): bool;

    public function isReadableFile(string $filename): bool;

    public function isReadableDirectory(string $filename): bool;

    public function createFinder(): Finder;
}

FileSystemTestCase

An example of a PHPUnit test:

<?php declare(strict_types=1);

namespace App\Tests;

use Fidry\FileSystem\FS;
use Fidry\FileSystem\Test\FileSystemTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use Symfony\Component\Finder\Finder;
use function getenv;
use function is_string;

final class MyAppFileSystemTest extends FileSystemTestCase
{
    public function test_it_works(): void
    {
        // The temporary directory can be changed by overriding `::getTmpDirPrefix()`.
    
        // This file is dumped into a temporary directory. Here,
        // something like '/private/var/folders/p3/lkw0cgjj2fq0656q_9rd0mk80000gn/T/AppTestsMyAppFileSystemTest10000'
        // on OSX.
        FS::dumpFile('file1', '');
        
        $files = Finder::create()
            ->files()
            ->in($this->tmp);

        self::assertSame(['file1'], $this->normalizePaths($files));
    }
    
    // Utility methods available:
    /**
     * @param iterable<string|Stringable> $paths
     *
     * @return list<string> File real paths relative to the current temporary directory
     */
    function normalizePaths(iterable $paths): array;
    
    static function safeChdir(string $directory): void;
    static function safeGetCurrentWorkingDirectory(): string;
}

SplFileInfo utilities

The Symfony Finder SplFileInfo distinguish itself from \SplFileInfo in two ways:

  • Files found by a finder are relative to the root directory. Hence, the relative path and pathname.
  • The ::getContents() method.

Prior to Symfony 7.1, the ::getContents() method was very useful. But now the method Filesystem::readfile() is available. Note that this method was backported in this library.

However, a lot of applications may be using the Symfony Finder SplFileInfo because of it still. Whilst for the source code it makes little difference, for tests it is more annoying as it is more complicated to create a fake Symfony Finder SplFileInfo object. You will have to create a mock, which may be more or less verbose depending on how much of the SplFileInfo API is used.

To help to fill in the gap, this library provides two utilities.

SplFileInfoFactory

SplFileInfoFactory allows to easily create a real Symfony Finder SplFileInfo instance without using a Finder:

use PHPUnit\Framework\TestCase;

class DemoTest extends TestCase {

    function test_it_allows_to_compare_finder_splfileinfo_files(): void
    {
        $actual = $myService->getFileInfo();
        
        $expected = SplFileInfoFactory::fromPath('/path/to/expected', __DIR__);

        self::assertEquals($expected, $actual);
    }
}

SplFileInfoBuilder

SplFileInfoBuilder allows to easily create a fake SplFileInfo instance. It is often simpler than using a mock:

- private function createSplFileInfoMock(string $file): SplFileInfo&MockObject
+ private function createSplFileInfo(string $file): SplFileInfo
{
-    $splFileInfoMock = $this->createMock(SplFileInfo::class);
-    $splFileInfoMock->method('__toString')->willReturn($file);
-    $splFileInfoMock->method('getFilename')->willReturn($file);
-    $splFileInfoMock->method('getRealPath')->willReturn($file);
-    $splFileInfoMock->method('getContents')->willReturn(
-        file_exists($file) ? file_get_contents($file) : 'content',
-    );
-
-    return $splFileInfoMock;
+    return SplFileInfoBuilder::withTestData()
+        ->withFile($file)
+        ->withContents(
+            file_exists($file) ? file_get_contents($file) : 'content',
+        )
+        ->build();
}

ReadOnlyFileSystem

// Write operations will throw a `DomainException` exception.
new ReadOnlyFileSystem(failOnWrite: true);

// Write operations will do nothing. Methods that return a path, e.g.
// `::tempnam()` will return an empty string.
new ReadOnlyFileSystem(failOnWrite: false);

FS

A FS static class for when you are not interested of using dependency injection for your filesystem layer or for usage within tests.

FS::touch('file');

// instead of
(new NativeFileSystem)->touch('file');

Contributing

GNU Make is your friend. Try make or make help!.

fidry/filesystem 适用场景与选型建议

fidry/filesystem 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 933.94k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2023 年 10 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「filesystem」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 fidry/filesystem 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 933.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 13
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2023-10-01