3 namespace pharext\SourceDir
;
10 * A PECL extension source directory containing a v2 package.xml
12 class Pecl
implements \IteratorAggregate
, SourceDir
15 * The Packager command
16 * @var pharext\Packager
22 * @var SimpleXmlElement
34 * @see \pharext\SourceDir::__construct()
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");
42 throw new Exception("Missing package.xml in $path");
44 $sxe->registerXPathNamespace("pecl", $sxe->getDocNamespaces()[""]);
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);
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);
60 if (!isset($args->zend
)) {
61 if ($sxe->xpath("/pecl:package/pecl:zendextsrcrelease")) {
62 foreach ($args->parse(1, ["--zend"]) as $error) {
63 $cmd->warn("%s\n", $error);
75 * @see \pharext\SourceDir::getBaseDir()
77 public function getBaseDir() {
82 * Compute the path of a file by parent dir nodes
83 * @param \SimpleXMLElement $ele
86 private function dirOf($ele) {
88 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
89 $path = trim($ele["name"], "/") ."/". $path ;
91 return trim($path, "/");
95 * Render installer hook
96 * @param array $configure
99 private static function loadHook($configure, $dependencies) {
100 require_once "pharext/Version.php";
101 return include __DIR__
."/../../pharext_install.tpl.php";
105 * Create installer hook
108 private function generateHooks() {
109 $dependencies = $this->sxe
->xpath("/pecl:package/pecl:dependencies/pecl:required/pecl:package");
110 foreach ($dependencies as $key => $dep) {
111 if (($glob = glob("{$this->path}/{$dep->name}-*.ext.phar*"))) {
112 usort($glob, function($a, $b) {
113 return version_compare(
114 substr($a, strpos(".ext.phar", $a)),
115 substr($b, strpos(".ext.phar", $b))
120 unset($dependencies[$key]);
123 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
126 ob_start(function($s) use($fd){
130 self
::loadHook($configure, $dependencies);
133 yield
"pharext_install.php" => $fd;
138 * Generate a list of files from the package.xml
141 private function generateFiles() {
142 foreach ($this->generateHooks() as $file => $hook) {
143 if ($this->cmd
->getArgs()->verbose
) {
144 $this->cmd
->info("Packaging %s\n", is_scalar($hook) ?
$hook : $file);
146 yield
$file => $hook;
148 foreach ($this->sxe
->xpath("//pecl:file") as $file) {
149 $path = $this->path
."/". $this->dirOf($file) ."/". $file["name"];
150 if ($this->cmd
->getArgs()->verbose
) {
151 $this->cmd
->info("Packaging %s\n", substr($path, strlen($this->path
)));
153 if (!($realpath = realpath($path))) {
154 $this->cmd
->warn("File %s does not exist", $path);
161 * Implements IteratorAggregate
162 * @see IteratorAggregate::getIterator()
164 public function getIterator() {
165 return $this->generateFiles();