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