Skip to content

Commit a4f8a56

Browse files
committed
add ability to extend Filesystem class with Macros.
1 parent a2a6cf7 commit a4f8a56

4 files changed

Lines changed: 44 additions & 1 deletion

File tree

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Filesystem Component provide a fluent, object-oriented interface for working wit
1111

1212
* [Installation](#installation)
1313
* [Usage](#usage)
14+
* [Exteding](#extending)
1415
* [Methods](#methods)
1516
- [Filesystem](#filesystem)
1617
- [File](#file)
@@ -38,6 +39,30 @@ $filesystem = new Filesystem();
3839
$filesystem = filesystem();
3940
```
4041

42+
### Extending
43+
44+
Filesystem are "macroable", which allows you to add additional methods to the Filesystem class at run time. For example, the following code adds a customMethod method to the Filesystem class:
45+
46+
```php
47+
use Atomastic\Filesystem\Filesystem;
48+
use Atomastic\Macroable\Macroable;
49+
50+
Filesystem::macro('countFiles', function($path) {
51+
return count(iterator_to_array($this->find()->in($path)->files(), false));
52+
});
53+
54+
$filesytem = new Filesystem();
55+
56+
echo $filesytem->countFiles('/directory');
57+
```
58+
59+
##### The above example will output:
60+
61+
```
62+
1
63+
```
64+
65+
4166
### Methods
4267

4368
#### Filesystem

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"ext-spl": "*",
2121
"ext-fileinfo": "*",
2222
"php": "^7.3 || ^8.0",
23-
"symfony/finder": "^5.1"
23+
"symfony/finder": "^5.1",
24+
"atomastic/macroable": "^1.0"
2425
},
2526
"autoload":{
2627
"psr-4": {

src/Filesystem.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Atomastic\Filesystem;
66

77
use Symfony\Component\Finder\Finder;
8+
use Atomastic\Macroable\Macroable;
89

910
use function chmod;
1011
use function fileperms;
@@ -16,6 +17,8 @@
1617

1718
class Filesystem
1819
{
20+
use Macroable;
21+
1922
/**
2023
* Create a Finder instance.
2124
*/

tests/FilesystemTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,17 @@
382382
$this->assertEquals($this->tempDir . '/1.txt', $filesytem->file($this->tempDir . '/1.txt')->path());
383383
$this->assertEquals($this->tempDir, $filesytem->directory($this->tempDir)->path());
384384
});
385+
386+
test('test macro() method', function (): void {
387+
@mkdir($this->tempDir . '/1');
388+
$filesytem = new Filesystem();
389+
$filesytem->file($this->tempDir . '/1/1.txt')->put('hello world');
390+
$filesytem->file($this->tempDir . '/1/2.txt')->put('hello world');
391+
392+
Filesystem::macro('countFiles', function($path) {
393+
return count(iterator_to_array($this->find()->in($path)->files(), false));
394+
});
395+
396+
$filesytem = new Filesystem();
397+
$this->assertEquals(2, $filesytem->countFiles($this->tempDir . '/1'));
398+
});

0 commit comments

Comments
 (0)