fix normal/verbose/quiet output
[pharext/pharext] / src / pharext / Task / PharCompress.php
1 <?php
2
3 namespace pharext\Task;
4
5 use pharext\Task;
6
7 use Phar;
8
9 /**
10 * Clone a compressed copy of a phar
11 */
12 class PharCompress implements Task
13 {
14 /**
15 * @var string
16 */
17 private $file;
18
19 /**
20 * @var Phar
21 */
22 private $package;
23
24 /**
25 * @var int
26 */
27 private $encoding;
28
29 /**
30 * @var string
31 */
32 private $extension;
33
34 /**
35 * @param string $file path to the original phar
36 * @param int $encoding Phar::GZ or Phar::BZ2
37 */
38 public function __construct($file, $encoding) {
39 $this->file = $file;
40 $this->package = new Phar($file);
41 $this->encoding = $encoding;
42
43 switch ($encoding) {
44 case Phar::GZ:
45 $this->extension = ".gz";
46 break;
47 case Phar::BZ2:
48 $this->extension = ".bz2";
49 break;
50 }
51 }
52
53 /**
54 * @param bool $verbose
55 * @return string
56 */
57 public function run($verbose = false) {
58 if ($verbose !== false) {
59 printf("Compressing %s ...\n", basename($this->package->getPath()));
60 }
61 /* stop shebang */
62 $stub = $this->package->getStub();
63 $phar = $this->package->compress($this->encoding);
64 $phar->setStub(substr($stub, strpos($stub, "\n")+1));
65 return $this->file . $this->extension;
66 }
67 }