定制 ralphjsmit/filesystem 二次开发

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

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

ralphjsmit/filesystem

Composer 安装命令:

composer require ralphjsmit/filesystem

包简介

A fluent filesystem package for PHP.

README 文档

README

Stubs Banner

A fluent filesystem package for PHP

This package helps you to speed up the process of moving and copying files. It also makes replacing namespaces much easier, thus making it an invaluable tool for heavy filesystem tasks. Enjoy!

Important

This package is only updated until Laravel 10.

⚡️ See the release article on my website or subscribe to my newsletter for an occasional update

Installation

You can install the package via composer:

composer require ralphjsmit/filesystem

Usage

This package works by providing you with a base Stub class and a File class. As the name implies, the Stub class is an object that contains specific configuration, like the namespaces and the base directory. The File class is used to represent an individual file.

Creating a Stub configuration

You can create a new Stub configuration with Stub::new():

use RalphJSmit\Filesystem\Stub;

$stub = Stub::new();

You can use a Stub configuration like this (I'll talk more about file actions below):

$stub->getFile(__DIR__ . '/tmp/testFileA.php')->move(__DIR__ . '/tmp/otherFolder');

You can also set a base directory for your Stub:

$stub = Stub::dir(__DIR__);

$stub->getFile('/tmp/testFileA.php')->move('/tmp/otherFolder');

If you already have a $stub instance, you can configure namespaces on them. Those namespaces are used on the File object for the ->namespace() action. It basically means that you define the directories for each namespace in your project.

$stubs = Stub::dir(__DIR__)->namespaces([
    'Support' => '/src/Support/',
    'Domain' => '/src/Domain/',
    'App' => '/src/App/',
]);

$stubs->getFile('/tmp/TestFileA.php')->namespace('Support/Models');
// Moves __DIR__ . `/tmp/testFileA.php` to __DIR__ . `/src/Support/Models/testFileA.php`.

You can also have multiple stub configurations at the same time:

$stubA = Stub::dir(__DIR__ . '/src');
$stubB = Stub::dir(__DIR__ . '/app');
$stubC = Stub::dir(__DIR__ . '/tmp');

Getting a File object

You can get a File object by directly calling file() on the Stub class:

$file = Stub::file(__DIR__ . '/tmp/testFileA.php');

You can also get a File object from a $stub instance:

$stub = Stub::dir(__DIR__);

$file = $stub->getFile('/tmp/testFileA.php');

Actions on a File object

If you have a File object, you can perform the following actions on it:

Copying a file

You can copy a file to a new directory using the copy() function:

$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->copy('/tmp/test');
// $file is now in __DIR__ . '/tmp/testFileA.php' AND in __DIR__ . '/tmp/test/testFileA.php'

Deleting a file

You can delete a file using the delete() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->delete();
// returns true on success

Getting the basename of a file

You can get the basename of a file using the getBasename() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getBasename();
// 'testFileA.php'

Getting the directory location of a file

You can get the location of the directory of a file using the getDirectory() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getDirectory();
// __DIR__ . '/tmp'

Getting the full path of a file

You can get the full path of a file using the getFilepath() function:

Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getFilepath();
// __DIR__ . '/testFileA.php'

Getting the file contents

You can get the contents of a file using the getContents() function:

$contents = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->getContents();

Moving a file

You can move a file to a new directory using the move() function:

$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->move('/tmp/test');
// $file is now in __DIR__ . '/tmp/test/testFileA.php'

Updating the namespace of a file

You may update the namespace of a file and move it to the correct directory with the namespace() helper. This is ideal if you need to move a PHP-file to a new directory and update its namespace. I use this technique in the ralphjsmit/tall-install package.

$basePath = __DIR__;

$stubs = Stub::dir($basePath)->namespaces([
    'Support' => '/src/Support/',
    'Domain' => '/src/Domain/',
    'App' => '/src/App/',
]);

$stubs->getFile('/app/Console/Kernel.php')->namespace('Support\App\Console');
// file is not in __DIR__ . '/src/Support/App/Console/Kernel.php'

Checkout a real-life example from one of my packages.

Updating the contents of a file

You may update the contents of a file with the putFile() method:

$newContents = 'Hello world!';

$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->putFile($newContents);

You may also specify a new location for the file:

$newContents = 'Hello world!';

$file = Stub::dir(__DIR__)->getFile('/tmp/testFileA.php')->putFile($newContents, '/tmp/test/myFile.php');
// Will create a file with the "Hello world!" in __DIR__ . '/tmp/test/myFile.php`
// Old file will still exist

If you just want to move or copy a file, you should use those methods.

Updating the namespace of a file

You may replace the namespace of a file with the replaceNamespace($newNamespace) method:

$file = Stub::dir(__DIR)->getFile('/tmp/test/MyClass.php');

$file->replaceNamespace('App\Models');

// $file will now have the namespace App\Models

General

🐞 If you spot a bug, please submit a detailed issue, and I'll try to fix it as soon as possible.

🔐 If you discover a vulnerability, please review our security policy.

🙌 If you want to contribute, please submit a pull request. All PRs will be fully credited. If you're unsure whether I'd accept your idea, feel free to contact me!

🙋‍♂️ Ralph J. Smit

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.9k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 17
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-01-18