2675a7899836d70d07b9e36b4b0d1ee637ad2150
[pharext/pharext] / src / pharext / SourceDir / Pharext.php
1 <?php
2
3 namespace pharext\SourceDir;
4
5 use pharext\Command;
6 use pharext\SourceDir;
7
8 /**
9 * A source directory containing pharext_package.php and eventually pharext_install.php
10 */
11 class Pharext implements \IteratorAggregate, SourceDir
12 {
13 /**
14 * @var pharext\Command
15 */
16 private $cmd;
17
18 /**
19 * @var string
20 */
21 private $path;
22
23 /**
24 * @var callable
25 */
26 private $iter;
27
28 /**
29 * @inheritdoc
30 * @see \pharext\SourceDir::__construct()
31 */
32 public function __construct(Command $cmd, $path) {
33 $this->cmd = $cmd;
34 $this->path = $path;
35
36 $callable = include "$path/pharext_package.php";
37 if (!is_callable($callable)) {
38 throw new \Exception("Package hook did not return a callable");
39 }
40 $this->iter = $callable($cmd, $path);
41 }
42
43 /**
44 * @inheritdoc
45 * @see \pharext\SourceDir::getBaseDir()
46 */
47 public function getBaseDir() {
48 return $this->path;
49 }
50
51 /**
52 * Implements IteratorAggregate
53 * @see IteratorAggregate::getIterator()
54 */
55 public function getIterator() {
56 if (!is_callable($this->iter)) {
57 return new Git($this->cmd, $this->path);
58 }
59 return call_user_func($this->iter, $this->cmd, $this->path);
60 }
61 }