dd19171a55e307b9007aa7dc9256d50c249c2b29
6 * A PECL extension source directory containing a v2 package.xml
8 class PeclSourceDir
implements \IteratorAggregate
, SourceDir
11 * The Packager command
12 * @var pharext\Packager
18 * @var SimpleXmlElement
30 * @see \pharext\SourceDir::__construct()
32 public function __construct(Command
$cmd, $path) {
33 if (!realpath("$path/package.xml")) {
34 throw new \
Exception("Missing package.xml in $path");
36 $sxe = simplexml_load_file("$path/package.xml");
37 $sxe->registerXPathNamespace("pecl", $sxe->getDocNamespaces()[""]);
39 $args = $cmd->getArgs();
40 if (!isset($args->name
)) {
41 $name = (string) $sxe->xpath("/pecl:package/pecl:name")[0];
42 foreach ($args->parse(2, ["--name", $name]) as $error) {
43 $cmd->error("%s\n", $error);
47 if (!isset($args->release
)) {
48 $release = (string) $sxe->xpath("/pecl:package/pecl:version/pecl:release")[0];
49 foreach ($args->parse(2, ["--release", $release]) as $error) {
50 $cmd->error("%s\n", $error);
61 * @see \pharext\SourceDir::getBaseDir()
63 public function getBaseDir() {
68 * Compute the path of a file by parent dir nodes
69 * @param \SimpleXMLElement $ele
72 private function dirOf($ele) {
74 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
75 $path = trim($ele["name"], "/") ."/". $path ;
77 return trim($path, "/");
81 * Render installer hook
82 * @param array $configure
85 private static function loadHook($configure, $dependencies) {
86 return include __DIR__
."/../pharext_install.tpl.php";
90 * Create installer hook
93 private function generateHooks() {
94 $dependencies = $this->sxe
->xpath("/pecl:package/pecl:dependencies/pecl:required/pecl:package");
95 foreach ($dependencies as $key => $dep) {
96 if (($glob = glob("{$this->path}/{$dep->name}-*.ext.phar*"))) {
97 usort($glob, function($a, $b) {
98 return version_compare(
99 substr($a, strpos(".ext.phar", $a)),
100 substr($b, strpos(".ext.phar", $b))
103 yield
realpath($this->path
."/".end($glob));
105 unset($dependencies[$key]);
108 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
111 ob_start(function($s) use($fd){
115 self
::loadHook($configure, $dependencies);
118 yield
"pharext_install.php" => $fd;
123 * Generate a list of files from the package.xml
126 private function generateFiles() {
127 foreach ($this->generateHooks() as $file => $hook) {
128 if ($this->cmd
->getArgs()->verbose
) {
129 $this->cmd
->info("Packaging %s\n", is_string($hook) ?
$hook : $file);
131 yield
$file => $hook;
133 foreach ($this->sxe
->xpath("//pecl:file") as $file) {
134 $path = $this->path
."/". $this->dirOf($file) ."/". $file["name"];
135 if ($this->cmd
->getArgs()->verbose
) {
136 $this->cmd
->info("Packaging %s\n", $path);
138 if (!($realpath = realpath($path))) {
139 $this->cmd
->error("File %s does not exist", $path);
146 * Implements IteratorAggregate
147 * @see IteratorAggregate::getIterator()
149 public function getIterator() {
150 return $this->generateFiles();