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