Repository navigation
Expand file tree
/
Copy pathBundle.php
More file actions
93 lines (82 loc) · 2.22 KB
/
Copy pathBundle.php
File metadata and controls
93 lines (82 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact [email protected]
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace Hyperf\Phar;
use ArrayIterator;
use IteratorAggregate;
use SplFileInfo;
use Symfony\Component\Finder\Finder;
use Traversable;
class Bundle implements IteratorAggregate
{
/**
* @var Finder[]|string[]
*/
private array $resources = [];
/**
* Add a file to the resource bundle.
*/
public function addFile(string $file): static
{
$this->resources[] = $file;
return $this;
}
/**
* @param string[] $dirs
*/
public function addDirs(array $dirs): static
{
return $this->addFinder((new Finder())->files()->ignoreVCS(true)->in($dirs));
}
/**
* Add a directory package to a resource package.
*/
public function addFinder(Finder $dir): static
{
$this->resources[] = $dir;
return $this;
}
/**
* Determines whether the file exists in the resource bundle.
*/
public function checkContains(string $resource): bool
{
foreach ($this->resources as $containedResource) {
if ($containedResource instanceof Finder && $this->directoryContains($containedResource, $resource)) {
return true;
}
if (is_string($containedResource) && $containedResource === $resource) {
return true;
}
}
return false;
}
/**
* Returns an iterator for a list of resources.
*/
public function getIterator(): Traversable
{
return new ArrayIterator($this->resources);
}
/**
* Determines whether the file exists in the folder resource bundle.
*/
private function directoryContains(Finder $dir, string $resource): bool
{
$resourceStrLength = strlen($resource);
foreach ($dir as $containedResource) {
/* @var $containedResource SplFileInfo */
if (substr($containedResource->getRealPath(), 0, $resourceStrLength) == $resource) {
return true;
}
}
return false;
}
}