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