b237771055c51cf344d9397caa26591c0cf5ea56
[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
37 if (count($dirs) !== 1 || !count($files)) {
38 throw new Exception("Does not look like an extracted PECL dir: {$this->source}");
39 }
40
41 $dest = current($dirs);
42
43 foreach ($files as $file) {
44 if ($verbose) {
45 printf("Moving %s into %s ...\n", basename($file), basename($dest));
46 }
47 if (!rename($file, "$dest/" . basename($file))) {
48 throw new Exception;
49 }
50 }
51
52 return $dest;
53 }
54 }