39127f6a7f95035e599e136ef3d135362abe94ad
[pharext/pharext] / src / pharext / SourceDir / Pecl.php
1 <?php
2
3 namespace pharext\SourceDir;
4
5 use pharext\Command;
6 use pharext\SourceDir;
7
8 /**
9 * A PECL extension source directory containing a v2 package.xml
10 */
11 class Pecl implements \IteratorAggregate, SourceDir
12 {
13 /**
14 * The Packager command
15 * @var pharext\Packager
16 */
17 private $cmd;
18
19 /**
20 * The package.xml
21 * @var SimpleXmlElement
22 */
23 private $sxe;
24
25 /**
26 * The base directory
27 * @var string
28 */
29 private $path;
30
31 /**
32 * @inheritdoc
33 * @see \pharext\SourceDir::__construct()
34 */
35 public function __construct(Command $cmd, $path) {
36 if (!realpath("$path/package.xml")) {
37 throw new \Exception("Missing package.xml in $path");
38 }
39 $sxe = simplexml_load_file("$path/package.xml");
40 $sxe->registerXPathNamespace("pecl", $sxe->getDocNamespaces()[""]);
41
42 $args = $cmd->getArgs();
43 if (!isset($args->name)) {
44 $name = (string) $sxe->xpath("/pecl:package/pecl:name")[0];
45 foreach ($args->parse(2, ["--name", $name]) as $error) {
46 $cmd->error("%s\n", $error);
47 }
48 }
49
50 if (!isset($args->release)) {
51 $release = (string) $sxe->xpath("/pecl:package/pecl:version/pecl:release")[0];
52 foreach ($args->parse(2, ["--release", $release]) as $error) {
53 $cmd->error("%s\n", $error);
54 }
55 }
56
57 $this->cmd = $cmd;
58 $this->sxe = $sxe;
59 $this->path = $path;
60 }
61
62 /**
63 * @inheritdoc
64 * @see \pharext\SourceDir::getBaseDir()
65 */
66 public function getBaseDir() {
67 return $this->path;
68 }
69
70 /**
71 * Compute the path of a file by parent dir nodes
72 * @param \SimpleXMLElement $ele
73 * @return string
74 */
75 private function dirOf($ele) {
76 $path = "";
77 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
78 $path = trim($ele["name"], "/") ."/". $path ;
79 }
80 return trim($path, "/");
81 }
82
83 /**
84 * Render installer hook
85 * @param array $configure
86 * @return string
87 */
88 private static function loadHook($configure, $dependencies) {
89 return include __DIR__."/../../pharext_install.tpl.php";
90 }
91
92 /**
93 * Create installer hook
94 * @return \Generator
95 */
96 private function generateHooks() {
97 $dependencies = $this->sxe->xpath("/pecl:package/pecl:dependencies/pecl:required/pecl:package");
98 foreach ($dependencies as $key => $dep) {
99 if (($glob = glob("{$this->path}/{$dep->name}-*.ext.phar*"))) {
100 usort($glob, function($a, $b) {
101 return version_compare(
102 substr($a, strpos(".ext.phar", $a)),
103 substr($b, strpos(".ext.phar", $b))
104 );
105 });
106 yield realpath($this->path."/".end($glob));
107 } else {
108 unset($dependencies[$key]);
109 }
110 }
111 $configure = $this->sxe->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
112 if ($configure) {
113 $fd = tmpfile();
114 ob_start(function($s) use($fd){
115 fwrite($fd, $s);
116 return null;
117 });
118 self::loadHook($configure, $dependencies);
119 ob_end_flush();
120 rewind($fd);
121 yield "pharext_install.php" => $fd;
122 }
123 }
124
125 /**
126 * Generate a list of files from the package.xml
127 * @return Generator
128 */
129 private function generateFiles() {
130 foreach ($this->generateHooks() as $file => $hook) {
131 if ($this->cmd->getArgs()->verbose) {
132 $this->cmd->info("Packaging %s\n", is_string($hook) ? $hook : $file);
133 }
134 yield $file => $hook;
135 }
136 foreach ($this->sxe->xpath("//pecl:file") as $file) {
137 $path = $this->path ."/". $this->dirOf($file) ."/". $file["name"];
138 if ($this->cmd->getArgs()->verbose) {
139 $this->cmd->info("Packaging %s\n", $path);
140 }
141 if (!($realpath = realpath($path))) {
142 $this->cmd->error("File %s does not exist", $path);
143 }
144 yield $realpath;
145 }
146 }
147
148 /**
149 * Implements IteratorAggregate
150 * @see IteratorAggregate::getIterator()
151 */
152 public function getIterator() {
153 return $this->generateFiles();
154 }
155 }