basic source dirs && general tarball fixup
[pharext/pharext] / src / pharext / Task / PeclFixup.php
1 <?php
2
3 namespace pharext\Task;
4
5 use pharext\Exception;
6 use pharext\Task;
7
8 /**
9 * Fixup package.xml files in an extracted PECL dir
10 */
11 class PeclFixup implements Task
12 {
13 /**
14 * @var string
15 */
16 private $source;
17
18 /**
19 * @param string $source source directory
20 */
21 public function __construct($source) {
22 $this->source = $source;
23 }
24
25 /**
26 * @param bool $verbose
27 * @return string sanitized source location
28 * @throws \pahrext\Exception
29 */
30 public function run($verbose = false) {
31 if ($verbose !== false) {
32 printf("Sanitizing PECL dir ...\n");
33 }
34 $dirs = glob("{$this->source}/*", GLOB_ONLYDIR);
35 $files = array_diff(glob("{$this->source}/*"), $dirs);
36 $check = array_reduce($files, function($r, $v) {
37 return $v && fnmatch("package*.xml", basename($v));
38 }, true);
39
40 if (count($dirs) !== 1 || !$check) {
41 throw new Exception("Does not look like an extracted PECL dir: {$this->source}");
42 }
43
44 $dest = current($dirs);
45
46 foreach ($files as $file) {
47 if ($verbose) {
48 printf("Moving %s into %s ...\n", basename($file), basename($dest));
49 }
50 if (!rename($file, "$dest/" . basename($file))) {
51 throw new Exception;
52 }
53 }
54
55 return $dest;
56 }
57 }