add bin/pharext.update
[pharext/pharext] / src / pharext / Task / PharStub.php
1 <?php
2
3 namespace pharext\Task;
4
5 use Phar;
6 use pharext\Exception;
7 use pharext\Task;
8
9 /**
10 * Set the phar's stub
11 */
12 class PharStub implements Task
13 {
14 /**
15 * @var \Phar
16 */
17 private $phar;
18
19 /**
20 * @var string
21 */
22 private $stub;
23
24 /**
25 * @param \Phar $phar
26 * @param string $stub file path to the stub
27 * @throws \pharext\Exception
28 */
29 function __construct(Phar $phar, $stub) {
30 $this->phar = $phar;
31 if (!file_exists($this->stub = $stub)) {
32 throw new Exception("File '$stub' does not exist");
33 }
34 }
35
36 /**
37 * @param bool $verbose
38 */
39 function run($verbose = false) {
40 if ($verbose) {
41 printf("Using stub '%s'...\n", basename($this->stub));
42 }
43 $stub = preg_replace_callback('/^#include <([^>]+)>/m', function($includes) {
44 return file_get_contents($includes[1], true, null, 5);
45 }, file_get_contents($this->stub));
46 if ($this->phar->isCompressed() && substr($stub, 0, 2) === "#!") {
47 $stub = substr($stub, strpos($stub, "\n")+1);
48 }
49 $this->phar->setStub($stub);
50 }
51 }