df52101b3f043bb9bbd74eba1276c3b4d40409f4
[pharext/pharext] / src / pharext / SourceDir / Git.php
1 <?php
2
3 namespace pharext\SourceDir;
4
5 use pharext\Command;
6 use pharext\SourceDir;
7
8 /**
9 * Extension source directory which is a git repo
10 */
11 class Git implements \IteratorAggregate, SourceDir
12 {
13 /**
14 * The Packager command
15 * @var pharext\Command
16 */
17 private $cmd;
18
19 /**
20 * Base directory
21 * @var string
22 */
23 private $path;
24
25 /**
26 * @inheritdoc
27 * @see \pharext\SourceDir::__construct()
28 */
29 public function __construct(Command $cmd, $path) {
30 $this->cmd = $cmd;
31 $this->path = $path;
32 }
33
34 /**
35 * @inheritdoc
36 * @see \pharext\SourceDir::getBaseDir()
37 */
38 public function getBaseDir() {
39 return $this->path;
40 }
41
42 /**
43 * Generate a list of files by `git ls-files`
44 * @return Generator
45 */
46 private function generateFiles() {
47 $pwd = getcwd();
48 chdir($this->path);
49 if (($pipe = popen("git ls-tree -r --name-only HEAD", "r"))) {
50 while (!feof($pipe)) {
51 if (strlen($file = trim(fgets($pipe)))) {
52 if ($this->cmd->getArgs()->verbose) {
53 $this->cmd->info("Packaging %s\n", $file);
54 }
55 if (!($realpath = realpath($file))) {
56 $this->cmd->error("File %s does not exist\n", $file);
57 }
58 yield $realpath;
59 }
60 }
61 pclose($pipe);
62 }
63 chdir($pwd);
64 }
65
66 /**
67 * Implements IteratorAggregate
68 * @see IteratorAggregate::getIterator()
69 */
70 public function getIterator() {
71 return $this->generateFiles();
72 }
73 }