sourcedir: guess basic package info
[pharext/pharext] / src / pharext / SourceDir / Git.php
1 <?php
2
3 namespace pharext\SourceDir;
4
5 use pharext\Cli\Args;
6 use pharext\Exception;
7 use pharext\ExecCmd;
8 use pharext\License;
9 use pharext\PackageInfo;
10 use pharext\SourceDir;
11 use pharext\Tempfile;
12
13 /**
14 * Extension source directory which is a git repo
15 */
16 class Git implements \IteratorAggregate, SourceDir
17 {
18 use License;
19 use PackageInfo;
20
21 /**
22 * Base directory
23 * @var string
24 */
25 private $path;
26
27 /**
28 * @inheritdoc
29 * @see \pharext\SourceDir::__construct()
30 */
31 public function __construct($path) {
32 $this->path = $path;
33 }
34
35 /**
36 * @inheritdoc
37 * @see \pharext\SourceDir::getBaseDir()
38 */
39 public function getBaseDir() {
40 return $this->path;
41 }
42
43 /**
44 * @inheritdoc
45 * @return array
46 */
47 public function getPackageInfo() {
48 return $this->findPackageInfo($this->getBaseDir());
49 }
50
51 /**
52 * @inheritdoc
53 * @return string
54 */
55 public function getLicense() {
56 if (($file = $this->findLicense($this->getBaseDir()))) {
57 return $this->readLicense($file);
58 }
59 return "UNKNOWN";
60 }
61
62 /**
63 * @inheritdoc
64 * @return array
65 */
66 public function getArgs() {
67 return [];
68 }
69
70 /**
71 * @inheritdoc
72 */
73 public function setArgs(Args $args) {
74 }
75
76 /**
77 * Generate a list of files by `git ls-files`
78 * @return Generator
79 */
80 private function generateFiles() {
81 $pwd = getcwd();
82 chdir($this->path);
83 if (($pipe = popen("git ls-tree -r --name-only HEAD", "r"))) {
84 $path = realpath($this->path);
85 while (!feof($pipe)) {
86 if (strlen($file = trim(fgets($pipe)))) {
87 /* there may be symlinks, so no realpath here */
88 yield "$path/$file";
89 }
90 }
91 pclose($pipe);
92 }
93 chdir($pwd);
94 }
95
96 /**
97 * Implements IteratorAggregate
98 * @see IteratorAggregate::getIterator()
99 */
100 public function getIterator() {
101 return $this->generateFiles();
102 }
103 }