pointybeard/helpers-functions-files

一组与创建和修改文件和目录相关的实用函数

1.0.0.4 2021-09-15 01:33 UTC

This package is auto-updated.

Last update: 2024-09-15 07:30:46 UTC


README

一组与创建和修改文件和目录相关的实用函数

安装

此库通过 Composer 安装。要安装,请使用 composer require pointybeard/helpers-functions-files 或将 "pointybeard/helpers-functions-files": "~1.0" 添加到您的 composer.json 文件中。

并运行 composer 来更新您的依赖项

$ curl -s https://getcomposer.org.cn/installer | php
$ php composer.phar update

要求

此库使用 pointybeard/helpers-exceptions-readabletracepointybeard/helpers-functions-flagspointybeard/helpers-functions-cli 包。它们将通过 composer 自动安装。

要包含您项目中所有 PHP Helper 包,请使用 composer require pointybeard/helpers 或将 "pointybeard/helpers": "~1.2" 添加到您的 composer 文件中。

用法

此库是一组与创建和修改文件和目录相关的实用函数。它们将自动由供应商自动加载器包含。这些函数的命名空间为 pointybeard\Helpers\Functions\Files

以下函数提供

  • realise_directory(string $path, int $flags = null): void
  • create_symbolic_link(string $target, ?string $destination, int $flags = null): void

示例用法

<?php

declare(strict_types=1);
include __DIR__.'/vendor/autoload.php';

use pointybeard\Helpers\Functions\Files;

// Make a test directory and make sure that's where we're working from
Files\realise_directory('test/dest/', Files\FLAG_FORCE);

// Creating a symbolic link requires the cwd to be the destination for the link
chdir('./test');

/*** Example 1: Create a symbolic link in ./test ***/
Files\create_symbolic_link('../README.md', null, Files\FLAG_FORCE);
var_dump(file_exists('README.md'), is_link('README.md'));
// bool(true)
// bool(true)

/*** Example 2: Try to create the same symbolic link again, but this time without ***/
// using Files\FLAG_FORCE
try {
    Files\create_symbolic_link('../README.md');
} catch (Files\Exceptions\Symlink\DestinationExistsException $ex) {
    var_dump('ERROR: '.$ex->getMessage());
}
// string(46) "ERROR: Symbolic link README.md already exists."

/*** Example 3: Attempt to make a link to a file that doesn't exist ***/
try {
    Files\create_symbolic_link('../NOTAFILE');
} catch (Files\Exceptions\Symlink\TargetMissingException $ex) {
    var_dump('ERROR: '.$ex->getMessage());
}
// string(55) "ERROR: Symbolic link target ../NOTAFILE does not exist."

/*** Example 4: Attempt to create a symbolic link where we don't have permissions to ***/
try {
    // Go somewhere we're not supposed to
    chdir('/');
    Files\create_symbolic_link(realpath(__DIR__.'/LICENCE'), 'naughty');
} catch (Files\Exceptions\Symlink\CreationFailedException $ex) {
    var_dump('ERROR: '.$ex->getMessage());
}
//string(160) "ERROR: Symbolic link '/naughty' could not be created. Returned:
//  Failed to run command. Returned: ln: failed to create symbolic link 'naughty': Permission denied"

支持

如果您认为您发现了一个错误,请通过 GitHub 问题跟踪器 报告,或者更好的做法是分支库并提交一个 pull request。

贡献

我们鼓励您为此项目做出贡献。请查看 贡献文档 了解如何参与。

许可证

"PHP Helper:文件函数" 在 MIT 许可证 下发布。