h4kuna/dir 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

h4kuna/dir

最新稳定版本:v0.1.7

Composer 安装命令:

composer require h4kuna/dir

包简介

Create easily directories

README 文档

README

Downloads this Month Latest Stable Version Coverage Status Total Downloads License

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').

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-02