3 namespace pharext\SourceDir
;
11 * A PECL extension source directory containing a v2 package.xml
13 class Pecl
implements \IteratorAggregate
, SourceDir
19 * @var SimpleXmlElement
37 * @see \pharext\SourceDir::__construct()
39 public function __construct($path) {
40 if (is_file("$path/package2.xml")) {
41 $sxe = simplexml_load_file($this->file
= "$path/package2.xml");
42 } elseif (is_file("$path/package.xml")) {
43 $sxe = simplexml_load_file($this->file
= "$path/package.xml");
45 throw new Exception("Missing package.xml in $path");
48 $sxe->registerXPathNamespace("pecl", $sxe->getDocNamespaces()[""]);
51 $this->path
= realpath($path);
56 * @see \pharext\SourceDir::getBaseDir()
58 public function getBaseDir() {
63 * Retrieve gathered package info
66 public function getPackageInfo() {
67 if (($name = $this->sxe
->xpath("/pecl:package/pecl:name"))) {
68 yield
"name" => (string) $name[0];
70 if (($release = $this->sxe
->xpath("/pecl:package/pecl:version/pecl:release"))) {
71 yield
"release" => (string) $release[0];
73 if ($this->sxe
->xpath("/pecl:package/pecl:zendextsrcrelease")) {
82 public function getLicense() {
83 if (($license = $this->sxe
->xpath("/pecl:package/pecl:license"))) {
84 if (($file = $this->findLicense($this->getBaseDir(), $license[0]["filesource"]))) {
85 return $this->readLicense($file);
88 if (($file = $this->findLicense($this->getBaseDir()))) {
89 return $this->readLicense($file);
92 return $license[0] ." ". $license[0]["uri"];
99 * @see \pharext\SourceDir::getArgs()
101 public function getArgs() {
102 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
103 foreach ($configure as $cfg) {
104 yield
[null, $cfg["name"], ucfirst($cfg["prompt"]), Args
::OPTARG
,
105 strlen($cfg["default"]) ?
$cfg["default"] : null];
107 $configure = $this->sxe
->xpath("/pecl:package/pecl:zendextsrcrelease/pecl:configureoption");
108 foreach ($configure as $cfg) {
109 yield
[null, $cfg["name"], ucfirst($cfg["prompt"]), Args
::OPTARG
,
110 strlen($cfg["default"]) ?
$cfg["default"] : null];
116 * @see \pharext\SourceDir::setArgs()
118 public function setArgs(Args
$args) {
119 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
120 foreach ($configure as $cfg) {
121 if (isset($args[$cfg["name"]])) {
122 $args->configure
= "--{$cfg["name
"]}={$args[$cfg["name
"]]}";
125 $configure = $this->sxe
->xpath("/pecl:package/pecl:zendextsrcrelease/pecl:configureoption");
126 foreach ($configure as $cfg) {
127 if (isset($args[$cfg["name"]])) {
128 $args->configure
= "--{$cfg["name
"]}={$args[$cfg["name
"]]}";
134 * Compute the path of a file by parent dir nodes
135 * @param \SimpleXMLElement $ele
138 private function dirOf($ele) {
140 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
141 $path = trim($ele["name"], "/") ."/". $path ;
143 return trim($path, "/");
147 * Generate a list of files from the package.xml
150 private function generateFiles() {
153 fprintf($temp, "<?php\nreturn new %s(__DIR__);\n", get_class($this));
155 yield
"pharext_package.php" => $temp;
158 $dependencies = $this->sxe
->xpath("/pecl:package/pecl:dependencies/pecl:required/pecl:package");
159 foreach ($dependencies as $key => $dep) {
160 if (($glob = glob("{$this->path}/{$dep->name}-*.ext.phar*"))) {
161 usort($glob, function($a, $b) {
162 return version_compare(
163 substr($a, strpos(".ext.phar", $a)),
164 substr($b, strpos(".ext.phar", $b))
172 yield
realpath($this->file
);
173 foreach ($this->sxe
->xpath("//pecl:file") as $file) {
174 yield
realpath($this->path
."/". $this->dirOf($file) ."/". $file["name"]);
179 * Implements IteratorAggregate
180 * @see IteratorAggregate::getIterator()
182 public function getIterator() {
183 return $this->generateFiles();