major refactoring under the hood
[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 $dirs = glob("{$this->source}/*", GLOB_ONLYDIR);
32 $files = array_diff(glob("{$this->source}/*"), $dirs);
33
34 if (count($dirs) !== 1 || !count($files)) {
35 throw new Exception("Does not look like an extracted PECL dir: {$this->source}");
36 }
37
38 $dest = current($dirs);
39
40 foreach ($files as $file) {
41 if (!rename($file, "$dest/" . basename($file))) {
42 throw new Exception;
43 }
44 }
45
46 return $dest;
47 }
48 }