3 namespace pharext\SourceDir
;
9 * A PECL extension source directory containing a v2 package.xml
11 class Pecl
implements \IteratorAggregate
, SourceDir
14 * The Packager command
15 * @var pharext\Packager
21 * @var SimpleXmlElement
33 * @see \pharext\SourceDir::__construct()
35 public function __construct(Command
$cmd, $path) {
36 if (!realpath("$path/package.xml")) {
37 throw new \
Exception("Missing package.xml in $path");
39 $sxe = simplexml_load_file("$path/package.xml");
40 $sxe->registerXPathNamespace("pecl", $sxe->getDocNamespaces()[""]);
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);
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);
64 * @see \pharext\SourceDir::getBaseDir()
66 public function getBaseDir() {
71 * Compute the path of a file by parent dir nodes
72 * @param \SimpleXMLElement $ele
75 private function dirOf($ele) {
77 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
78 $path = trim($ele["name"], "/") ."/". $path ;
80 return trim($path, "/");
84 * Render installer hook
85 * @param array $configure
88 private static function loadHook($configure, $dependencies) {
89 return include __DIR__
."/../../pharext_install.tpl.php";
93 * Create installer hook
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))
106 yield
realpath($this->path
."/".end($glob));
108 unset($dependencies[$key]);
111 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
114 ob_start(function($s) use($fd){
118 self
::loadHook($configure, $dependencies);
121 yield
"pharext_install.php" => $fd;
126 * Generate a list of files from the package.xml
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);
134 yield
$file => $hook;
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);
141 if (!($realpath = realpath($path))) {
142 $this->cmd
->error("File %s does not exist", $path);
149 * Implements IteratorAggregate
150 * @see IteratorAggregate::getIterator()
152 public function getIterator() {
153 return $this->generateFiles();