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 $sxe = simplexml_load_file("$path/package.xml", null, 0, "http://pear.php.net/dtd/package-2.0");
34 $sxe->registerXPathNamespace("pecl", "http://pear.php.net/dtd/package-2.0");
36 $args = $cmd->getArgs();
37 if (!isset($args->name
)) {
38 $name = (string) $sxe->xpath("/pecl:package/pecl:name")[0];
39 foreach ($args->parse(2, ["--name", $name]) as $error) {
40 $cmd->error("%s\n", $error);
44 if (!isset($args->release
)) {
45 $release = (string) $sxe->xpath("/pecl:package/pecl:version/pecl:release")[0];
46 foreach ($args->parse(2, ["--release", $release]) as $error) {
47 $cmd->error("%s\n", $error);
58 * @see \pharext\SourceDir::getBaseDir()
60 public function getBaseDir() {
65 * Compute the path of a file by parent dir nodes
66 * @param \SimpleXMLElement $ele
69 private function dirOf($ele) {
71 while (($ele = current($ele->xpath(".."))) && $ele->getName() == "dir") {
72 $path = trim($ele["name"], "/") ."/". $path ;
74 return trim($path, "/");
78 * Generate a list of files from the package.xml
81 private function generateFiles() {
82 foreach ($this->sxe
->xpath("//pecl:file") as $file) {
83 $path = $this->path
."/". $this->dirOf($file) ."/". $file["name"];
84 if ($this->cmd
->getArgs()->verbose
) {
85 $this->cmd
->info("Packaging %s\n", $path);
87 if (!($realpath = realpath($path))) {
88 $this->cmd
->error("File %s does not exist", $path);
95 * Implements IteratorAggregate
96 * @see IteratorAggregate::getIterator()
98 public function getIterator() {
99 return $this->generateFiles();