1e6d86ec7c8360d568136025e48eb0ba96725f4f
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
36 * @see \pharext\SourceDir::__construct()
38 public function __construct(Command
$cmd, $path) {
39 if (!realpath("$path/package.xml")) {
40 throw new \
Exception("Missing package.xml in $path");
42 $sxe = simplexml_load_file("$path/package.xml");
43 $sxe->registerXPathNamespace("pecl", $sxe->getDocNamespaces()[""]);
45 $args = $cmd->getArgs();
46 if (!isset($args->name
)) {
47 $name = (string) $sxe->xpath("/pecl:package/pecl:name")[0];
48 foreach ($args->parse(2, ["--name", $name]) as $error) {
49 $cmd->error("%s\n", $error);
53 if (!isset($args->release
)) {
54 $release = (string) $sxe->xpath("/pecl:package/pecl:version/pecl:release")[0];
55 foreach ($args->parse(2, ["--release", $release]) as $error) {
56 $cmd->error("%s\n", $error);
67 * @see \pharext\SourceDir::getBaseDir()
69 public function getBaseDir() {
74 * Compute the path of a file by parent dir nodes
75 * @param \SimpleXMLElement $ele
78 private function dirOf($ele) {
80 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
81 $path = trim($ele["name"], "/") ."/". $path ;
83 return trim($path, "/");
87 * Render installer hook
88 * @param array $configure
91 private static function loadHook($configure, $dependencies) {
92 return include __DIR__
."/../pharext_install.tpl.php";
96 * Create installer hook
99 private function generateHooks() {
100 $dependencies = $this->sxe
->xpath("/pecl:package/pecl:dependencies/pecl:required/pecl:package");
101 foreach ($dependencies as $key => $dep) {
102 if (($glob = glob("{$this->path}/{$dep->name}-*.ext.phar*"))) {
103 usort($glob, function($a, $b) {
104 return version_compare(
105 substr($a, strpos(".ext.phar", $a)),
106 substr($b, strpos(".ext.phar", $b))
109 yield
realpath($this->path
."/".end($glob));
111 unset($dependencies[$key]);
114 $configure = $this->sxe
->xpath("/pecl:package/pecl:extsrcrelease/pecl:configureoption");
117 ob_start(function($s) use($fd){
121 self
::loadHook($configure, $dependencies);
124 yield
"pharext_install.php" => $fd;
129 * Generate a list of files from the package.xml
132 private function generateFiles() {
133 foreach ($this->generateHooks() as $file => $hook) {
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();