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