fix builds of relative paths
[pharext/pharext] / src / pharext / SourceDir / Basic.php
1 <?php
2
3 namespace pharext\SourceDir;
4
5 use pharext\Cli\Args;
6 use pharext\License;
7 use pharext\PackageInfo;
8 use pharext\SourceDir;
9
10 use FilesystemIterator;
11 use IteratorAggregate;
12 use RecursiveCallbackFilterIterator;
13 use RecursiveDirectoryIterator;
14 use RecursiveIteratorIterator;
15
16
17 class Basic implements IteratorAggregate, SourceDir
18 {
19 use License;
20 use PackageInfo;
21
22 private $path;
23
24 public function __construct($path) {
25 $this->path = realpath($path);
26 }
27
28 public function getBaseDir() {
29 return $this->path;
30 }
31
32 /**
33 * @inheritdoc
34 * @return array
35 */
36 public function getPackageInfo() {
37 return $this->findPackageInfo($this->getBaseDir());
38 }
39
40 public function getLicense() {
41 if (($file = $this->findLicense($this->getBaseDir()))) {
42 return $this->readLicense($file);
43 }
44 return "UNKNOWN";
45 }
46
47 public function getArgs() {
48 return [];
49 }
50
51 public function setArgs(Args $args) {
52 }
53
54 public function filter($current, $key, $iterator) {
55 $sub = $current->getSubPath();
56 if ($sub === ".git" || $sub === ".hg" || $sub === ".svn") {
57 return false;
58 }
59 return true;
60 }
61
62 public function getIterator() {
63 $rdi = new RecursiveDirectoryIterator($this->path,
64 FilesystemIterator::CURRENT_AS_SELF | // needed for 5.5
65 FilesystemIterator::KEY_AS_PATHNAME |
66 FilesystemIterator::SKIP_DOTS);
67 $rci = new RecursiveCallbackFilterIterator($rdi, [$this, "filter"]);
68 $rii = new RecursiveIteratorIterator($rci);
69 foreach ($rii as $path => $child) {
70 if (!$child->isDir()) {
71 yield realpath($path);
72 }
73 }
74 }
75 }