Skip to content

Commit

Permalink
👔 up: update some helper methods and add more useful method for FTB
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Mar 22, 2024
1 parent 8c058c1 commit 910b3f8
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 146 deletions.
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ Some useful file system util for php
composer require toolkit/fsutil
```

## Usage

### File Finder
## File Finder

```php
use Toolkit\FsUtil\Extra\FileFinder;
Expand All @@ -44,10 +42,14 @@ foreach ($finder as $file) {
}
```

### File Tree Builder
## File Tree Builder

`FileTreeBuilder` - can be quickly create dirs and files, copy dir and files.

- can use path var in `dir()`, `copy()` ... methods. eg: `copy('{baseDir}/to/file', '{workdir}/dst/file')`

Quick start:

```php
use Toolkit\FsUtil\Extra\FileTreeBuilder;

Expand Down Expand Up @@ -78,7 +80,16 @@ Will create file tree like:
|-- new-file1.md
```

### Modify Watcher
### path vars

- `tplDir` The template dir path
- `baseDir` base workdir path, only init on first set workdir.
- `current,workdir` current workdir path.
- And all simple type var in `tplVars`.

Usage in path string: `{baseDir}/file`

## Modify Watcher

```php
use Toolkit\FsUtil\Extra\ModifyWatcher;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
}
],
"require": {
"php": ">=8.0.1",
"php": ">=8.1.1",
"toolkit/stdlib": "^2.0"
},
"autoload": {
Expand Down
18 changes: 8 additions & 10 deletions src/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ public static function mkSubDirs(string $parentDir, array $subDirs, int $mode =
public static function copy(string $oldDir, string $newDir, array $options = []): bool
{
if (!is_dir($oldDir)) {
throw new FileNotFoundException("copy errorsource dir does not exist!path: $oldDir");
throw new FileNotFoundException("Copy error: source dir does not exist!path: $oldDir");
}

self::doCopy($oldDir, $newDir, array_merge([
'skipExist' => true,
'skipExist' => true, // skip exist file
'filterFn' => null,
'beforeFn' => null,
'afterFn' => null,
Expand All @@ -310,7 +310,7 @@ private static function doCopy(string $oldDir, string $newDir, array $options):
{
self::create($newDir);
$beforeFn = $options['beforeFn'];
$filterFn = $options['filterFn'];
$filterFn = $options['filterFn']; // filter file or dir.

// use '{,.}*' match hidden files
foreach (glob($oldDir . '/{,.}*', GLOB_BRACE) as $old) {
Expand All @@ -319,15 +319,14 @@ private static function doCopy(string $oldDir, string $newDir, array $options):
continue;
}

$new = self::joinPath($newDir, $name);

if (is_dir($old)) {
self::doCopy($old, $new, $options);
// return false to skip copy
if ($filterFn && !$filterFn($old)) {
continue;
}

// return false to skip copy
if ($filterFn && !$filterFn($old)) {
$new = self::joinPath($newDir, $name);
if (is_dir($old)) {
self::doCopy($old, $new, $options);
continue;
}

Expand Down Expand Up @@ -361,7 +360,6 @@ private static function doCopy(string $oldDir, string $newDir, array $options):
public static function delete(string $path, bool $delSelf = true): bool
{
$dirPath = self::pathFormat($path);

if (is_file($dirPath)) {
return unlink($dirPath);
}
Expand Down
Loading

0 comments on commit 910b3f8

Please sign in to comment.