h4kuna/dir
Composer 安装命令:
composer require h4kuna/dir
包简介
Create easily directories
README 文档
README
One abstract class provide path and prepare filesystem.
Install by composer
composer require h4kuna/dir
How to use
Your path is represented by your class. In this repository is prepared class TempDir. By same way create own class.
These dirs are exist:
- temp dir
/documet/root/temp - log dir
/documet/root/log - storage dir
/documet/root/data
Example
Create StorageDir.
class StorageDir extends \h4kuna\Dir\Dir { }
Start to use.
$storageDir = new StorageDir('/documet/root/data'); // dir in constructor does not check $storageDir->create(); // if dir from constructor does not exist, let's check and create $subDir = $storageDir->dir('foo/bar'); $filepath = $subDir->filename('lucky', 'jpg'); $filepath2 = $storageDir->filename('baz/foo/happy.jpg'); echo $filepath; // /documet/root/data/foo/bar/lucky.jpg echo $filepath2; // /documet/root/data/baz/foo/happy.jpg
On file system exists path /documet/root/data/foo/bar and /documet/root/data/baz/foo.
Your storage dir is represented by class StorageDir and you can use it by dependency injection.
class MyClass { public function __construct(private StorageDir $storageDir) { } }
Check dir
If directory does not exist, the method create throw IOException. If directory exists, but is not writeable, the method checkWriteable throw DirIsNotWriteableException extends from IOException.
use h4kuna\Dir; try { $fileInfo = (new Dir\Dir('/any/path')) ->create() ->checkWriteable() ->fileInfo('foo.txt'); } catch (Dir\Exceptions\IOException $e) { // dir is not writable } var_dump($fileInfo->getPathname()); // /any/path/foo.txt
Incorrect use
In constructor use only absolute path without last slash like in example.
This is incorrect
new StorageDir('/documet/root/data/')new StorageDir('documet/root/data/')new StorageDir('documet/root/data')
Correct is only new StorageDir('/documet/root/data').
In methods dir() and filename() don't use slashes on the begin and end path.
This is incorrect
$storageDir->dir('/foo/')$storageDir->dir('/foo')$storageDir->dir('foo/')
Correct is only $storageDir->dir('foo') or sub dir $storageDir->dir('foo/bar').
h4kuna/dir 适用场景与选型建议
h4kuna/dir 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 90.5k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 h4kuna/dir 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 h4kuna/dir 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 90.5k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 15
- 依赖项目数: 6
- 推荐数: 2
其他信息
- 授权协议: MIT
- 更新时间: 2023-02-02