preserve symlinks
[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 $path = realpath($this->path);
51 while (!feof($pipe)) {
52 if (strlen($file = trim(fgets($pipe)))) {
53 if ($this->cmd->getArgs()->verbose) {
54 $this->cmd->info("Packaging %s\n", $file);
55 }
56 /* there may be symlinks, so no realpath here */
57 if (!file_exists("$path/$file")) {
58 $this->cmd->error("File %s does not exist in %s\n", $file, $path);
59 }
60 yield "$path/$file";
61 }
62 }
63 pclose($pipe);
64 }
65 chdir($pwd);
66 }
67
68 /**
69 * Implements IteratorAggregate
70 * @see IteratorAggregate::getIterator()
71 */
72 public function getIterator() {
73 return $this->generateFiles();
74 }
75 }