Skip to content

Commit f607e0d

Browse files
committed
Implement Directory class
1 parent de24089 commit f607e0d

File tree

2 files changed

+61
-28
lines changed

2 files changed

+61
-28
lines changed

src/Directory.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the PHP Code Compiler project
5+
*
6+
* Copyright (c) Yannoff (https://github.com/yannoff)
7+
*
8+
* @project PHP Code Compiler (yannoff/phpcc)
9+
* @homepage https://github.com/yannoff/phpcc
10+
* @license https://github.com/yannoff/phpcc/blob/main/LICENSE
11+
*
12+
* For the full copyright and license information, please view
13+
* the LICENSE file that was distributed with this source code.
14+
*/
15+
16+
namespace Yannoff\PhpCodeCompiler;
17+
18+
class Directory
19+
{
20+
/**
21+
* Return the full list of files in the directory, optionally filtered by a REGEXP pattern
22+
*
23+
* @param string $directory
24+
* @param ?string $pattern
25+
*
26+
* @return array
27+
*/
28+
public static function find(string $directory, string $pattern = null): array
29+
{
30+
$files = [];
31+
32+
foreach (self::scan($directory) as $value) {
33+
$path = sprintf('%s/%s', rtrim($directory, '/'), $value);
34+
if (is_file($path)) {
35+
if (preg_match($pattern ?: '/.*/', $path)) {
36+
$files[] = $path;
37+
}
38+
continue;
39+
}
40+
$files = array_merge($files, self::find($path, $pattern));
41+
}
42+
43+
return $files;
44+
}
45+
46+
/**
47+
* Perform a scandir() and remove "dots" dirs from results
48+
*
49+
* @param string $directory
50+
*
51+
* @return array
52+
*/
53+
public static function scan(string $directory): array
54+
{
55+
return array_filter(
56+
scandir($directory),
57+
function ($name) { return !in_array($name, ['.', '..']); }
58+
);
59+
}
60+
}

src/Phar.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Phar extends BuiltinPhar
3131
*/
3232
public function addDirectory(string $directory, string $filter = ''): self
3333
{
34-
$files = $this->find($directory, $filter);
34+
$files = Directory::find($directory, $filter);
3535
array_walk($files, function ($file) { $this->addFileContents($file); });
3636

3737
return $this;
@@ -53,31 +53,4 @@ public function addFileContents(string $filename, string $localName = null, bool
5353
$contents = $minify ? php_strip_whitespace($filename) : file_get_contents($filename);
5454
$this[$key] = $contents;
5555
}
56-
57-
/**
58-
* Return the full list of files in the directory, optionally filtered by a REGEXP pattern
59-
*
60-
* @param string $directory
61-
* @param ?string $pattern
62-
*
63-
* @return array
64-
*/
65-
public function find(string $directory, string $pattern = null): array
66-
{
67-
$files = [];
68-
$root = array_filter(scandir($directory), function ($value) { return !in_array($value, ['.', '..']); });
69-
70-
foreach ($root as $value) {
71-
$path = sprintf('%s/%s', rtrim($directory, '/'), $value);
72-
if (is_file($path)) {
73-
if (preg_match($pattern ?: '/.*/', $path)) {
74-
$files[] = $path;
75-
}
76-
continue;
77-
}
78-
$files = array_merge($files, $this->find($path, $pattern));
79-
}
80-
81-
return $files;
82-
}
8356
}

0 commit comments

Comments
 (0)