3 namespace pharext\SourceDir
;
11 * A PECL extension source directory containing a v2 package.xml
13 class Pecl
implements \IteratorAggregate
, SourceDir
17 * @var SimpleXmlElement
35 * @see \pharext\SourceDir::__construct()
37 public function __construct($path) {
38 if (is_file("$path/package2.xml")) {
39 $sxe = simplexml_load_file($this->file
= "$path/package2.xml");
40 } elseif (is_file("$path/package.xml")) {
41 $sxe = simplexml_load_file($this->file
= "$path/package.xml");
43 throw new Exception("Missing package.xml in $path");
46 $sxe->registerXPathNamespace("pecl", $sxe->getDocNamespaces()[""]);
49 $this->path
= realpath($path);
54 * @see \pharext\SourceDir::getBaseDir()
56 public function getBaseDir() {
61 * Retrieve gathered package info
64 public function getPackageInfo() {
65 if (($name = $this->sxe
->xpath("/pecl:package/pecl:name"))) {
66 yield
"name" => (string) $name[0];
68 if (($release = $this->sxe
->xpath("/pecl:package/pecl:version/pecl:release"))) {
69 yield
"release" => (string) $release[0];
71 if ($this->sxe
->xpath("/pecl:package/pecl:zendextsrcrelease")) {
78 * @see \pharext\SourceDir::getArgs()
80 public function getArgs() {
81 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
82 foreach ($configure as $cfg) {
83 yield
[null, $cfg["name"], ucfirst($cfg["prompt"]), Args
::OPTARG
,
84 strlen($cfg["default"]) ?
$cfg["default"] : null];
86 $configure = $this->sxe
->xpath("/pecl:package/pecl:zendextsrcrelease/pecl:configureoption");
87 foreach ($configure as $cfg) {
88 yield
[null, $cfg["name"], ucfirst($cfg["prompt"]), Args
::OPTARG
,
89 strlen($cfg["default"]) ?
$cfg["default"] : null];
95 * @see \pharext\SourceDir::setArgs()
97 public function setArgs(Args
$args) {
98 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
99 foreach ($configure as $cfg) {
100 if (isset($args[$cfg["name"]])) {
101 $args->configure
= "--{$cfg["name
"]}={$args[$cfg["name
"]]}";
104 $configure = $this->sxe
->xpath("/pecl:package/pecl:zendextsrcrelease/pecl:configureoption");
105 foreach ($configure as $cfg) {
106 if (isset($args[$cfg["name"]])) {
107 $args->configure
= "--{$cfg["name
"]}={$args[$cfg["name
"]]}";
113 * Compute the path of a file by parent dir nodes
114 * @param \SimpleXMLElement $ele
117 private function dirOf($ele) {
119 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
120 $path = trim($ele["name"], "/") ."/". $path ;
122 return trim($path, "/");
126 * Generate a list of files from the package.xml
129 private function generateFiles() {
132 fprintf($temp, "<?php\nreturn new %s(__DIR__);\n", get_class($this));
134 yield
"pharext_package.php" => $temp;
137 $dependencies = $this->sxe
->xpath("/pecl:package/pecl:dependencies/pecl:required/pecl:package");
138 foreach ($dependencies as $key => $dep) {
139 if (($glob = glob("{$this->path}/{$dep->name}-*.ext.phar*"))) {
140 usort($glob, function($a, $b) {
141 return version_compare(
142 substr($a, strpos(".ext.phar", $a)),
143 substr($b, strpos(".ext.phar", $b))
151 yield
realpath($this->file
);
152 foreach ($this->sxe
->xpath("//pecl:file") as $file) {
153 yield
realpath($this->path
."/". $this->dirOf($file) ."/". $file["name"]);
158 * Implements IteratorAggregate
159 * @see IteratorAggregate::getIterator()
161 public function getIterator() {
162 return $this->generateFiles();