Skip to content

Commit 584a49b

Browse files
committed
add Path&Phar Operator
1 parent c19ecdf commit 584a49b

File tree

3 files changed

+231
-0
lines changed

3 files changed

+231
-0
lines changed

src/Path.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace axios\tools;
6+
7+
class Path
8+
{
9+
const DS = \DIRECTORY_SEPARATOR;
10+
11+
/**
12+
* path join.
13+
*
14+
* @param mixed ...$paths
15+
*
16+
* @return string
17+
*/
18+
public static function join(...$paths)
19+
{
20+
if (0 === \count($paths)) {
21+
throw new \InvalidArgumentException('At least one parameter needs to be passed in.');
22+
}
23+
$pathResult = explode(self::DS, $paths[0]);
24+
unset($paths[0]);
25+
$pathResultLen = \count($pathResult);
26+
if ('' === $pathResult[$pathResultLen - 1]) {
27+
unset($pathResult[$pathResultLen - 1]);
28+
}
29+
foreach ($paths as $path) {
30+
$tmp = explode(self::DS, $path);
31+
foreach ($tmp as $str) {
32+
if ('..' === $str) {
33+
array_pop($pathResult);
34+
} elseif ('.' === $str || '' === $str) {
35+
continue;
36+
} else {
37+
array_push($pathResult, $str);
38+
}
39+
}
40+
}
41+
42+
return implode(self::DS, $pathResult);
43+
}
44+
45+
/**
46+
* search files.
47+
*
48+
* @param string $dir
49+
* @param string $extInclude
50+
* @param false $asc
51+
* @param int $sorting_type
52+
*
53+
* @return array
54+
*/
55+
public static function search($dir, $extInclude = '*', $asc = false, $sorting_type = SORT_FLAG_CASE)
56+
{
57+
$list = [];
58+
if (is_dir($dir)) {
59+
$dirHandle = opendir($dir);
60+
while (!\is_bool($dirHandle) && false !== ($file_name = readdir($dirHandle))) {
61+
$tmp = str_replace('.', '', $file_name);
62+
if ('' != $tmp) {
63+
$subFile = self::join($dir, $file_name);
64+
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
65+
if (is_dir($subFile)) {
66+
$list = array_merge($list, self::search($subFile, $extInclude, $asc, $sorting_type));
67+
} elseif (\is_string($extInclude)) {
68+
if ('*' == $extInclude || preg_match($extInclude, $file_name)) {
69+
$list[\count($list)] = $subFile;
70+
}
71+
} elseif (\is_array($extInclude) && \in_array($ext, $extInclude)) {
72+
$list[\count($list)] = $subFile;
73+
}
74+
}
75+
}
76+
closedir($dirHandle);
77+
}
78+
$asc ? ksort($list, $sorting_type) : krsort($list, $sorting_type);
79+
80+
return $list;
81+
}
82+
}

src/PharOperator.php

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace axios\tools;
6+
7+
class PharOperator
8+
{
9+
/**
10+
* @var \Phar
11+
*/
12+
private $phar;
13+
14+
/**
15+
* @var string
16+
*/
17+
private $zip_dir;
18+
19+
/**
20+
* @var array exclude files or folders
21+
*/
22+
private $exclude;
23+
24+
/**
25+
* @var mixed|string
26+
*
27+
* @example '/\.(json|php|ini)$/'
28+
*/
29+
private $include;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $index = 'autoload.php';
35+
36+
public function __construct($zip_dir = null, $include = '/\.*$/', $exclude = [])
37+
{
38+
if (!file_exists($zip_dir)) {
39+
throw new \InvalidArgumentException("{$zip_dir} is not exist.");
40+
}
41+
$this->zip_dir = Path::join($zip_dir);
42+
$this->exclude = $exclude;
43+
$this->include = $include;
44+
}
45+
46+
/**
47+
* @param $index
48+
*
49+
* @return $this
50+
*/
51+
public function setIndex($index)
52+
{
53+
$this->index = $index;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* @param string $exclude
60+
*
61+
* @return self
62+
*/
63+
public function addExclude($exclude)
64+
{
65+
array_push($this->exclude, Path::join($exclude));
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* @param string $output_path
72+
*/
73+
public function zip($output_path)
74+
{
75+
$save_path = $output_path;
76+
if (is_dir($save_path)) {
77+
throw new \InvalidArgumentException("{$output_path} must be file path.");
78+
}
79+
if (!file_exists(\dirname($save_path))) {
80+
@mkdir(\dirname($save_path), 0755, true);
81+
}
82+
83+
if (file_exists($save_path)) {
84+
@unlink($save_path);
85+
}
86+
87+
$this->phar = new \Phar($save_path);
88+
$this->phar->buildFromDirectory($this->zip_dir, $this->include);
89+
if (!empty($this->exclude)) {
90+
$this->remove($this->exclude);
91+
}
92+
$this->phar->setStub($this->phar->createDefaultStub($this->index));
93+
$this->phar->stopBuffering();
94+
}
95+
96+
/**
97+
* @param array $exclude
98+
*/
99+
private function remove($exclude)
100+
{
101+
foreach ($exclude as $file) {
102+
$path = Path::join($this->zip_dir, $file);
103+
if (is_dir($path)) {
104+
$files = Path::search($path, $this->include);
105+
foreach ($files as $filepath) {
106+
$file = str_replace($this->zip_dir . \DIRECTORY_SEPARATOR, '', $filepath);
107+
if ($this->phar->offsetExists($file)) {
108+
$this->phar->delete($file);
109+
}
110+
}
111+
} elseif ($this->phar->offsetExists($file)) {
112+
$this->phar->delete($file);
113+
}
114+
}
115+
}
116+
}

tests/unit/PathTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace axios\tools\tests\unit;
6+
7+
use axios\tools\Path;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/**
11+
* @internal
12+
* @coversNothing
13+
*/
14+
class PathTest extends TestCase
15+
{
16+
public function testJoin()
17+
{
18+
$this->assertEquals('/path/to', Path::join('/path/to/file.json', '..'));
19+
$this->assertEquals('/path/to/file.json', Path::join('/path/to/file.json', '.'));
20+
$this->assertEquals('/path/to/some/foo/bar', Path::join('/path/to/', 'some', './', 'foo', 'invalid', '..', 'bar'));
21+
}
22+
23+
public function testSearch()
24+
{
25+
$dir = \dirname(__DIR__);
26+
$files = Path::search($dir);
27+
$count = \count($files);
28+
$this->assertCount($count, Path::search($dir, '/\.(php|ini)$/'));
29+
$this->assertCount($count, Path::search($dir, ['php']));
30+
$this->assertCount(0, Path::search($dir, '/\.(xml|json)$/'));
31+
$this->assertCount(0, Path::search($dir, ['xml', 'json']));
32+
}
33+
}

0 commit comments

Comments
 (0)