1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Conveyor package. |
5
|
|
|
* |
6
|
|
|
* (c) Jeroen Fiege <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webcreate\Conveyor\Task; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Finder\Finder; |
15
|
|
|
use Symfony\Component\Finder\SplFileInfo; |
16
|
|
|
use Webcreate\Conveyor\IO\IOInterface; |
17
|
|
|
use Webcreate\Conveyor\Repository\Version; |
18
|
|
|
use Webcreate\Conveyor\Task\Result\ExecuteResult; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @todo check requirement: phar.readonly = Off in php.ini |
22
|
|
|
* @author jeroen |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
class PharTask extends Task |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $buildDir; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var \Webcreate\Conveyor\IO\IOInterface |
34
|
|
|
*/ |
35
|
|
|
protected $io; |
|
|
|
|
36
|
|
|
protected $conveyorFile; |
37
|
|
|
protected $filename; |
38
|
|
|
protected $stub; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $buildDir |
44
|
|
|
* @param $conveyorFile |
45
|
|
|
* @param IOInterface $io |
46
|
|
|
*/ |
47
|
|
|
public function __construct($buildDir, $conveyorFile, IOInterface $io = null) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
$this->io = $io; |
50
|
|
|
$this->buildDir = $buildDir; |
51
|
|
|
$this->conveyorFile = $conveyorFile; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @todo implement an excluded file list, instead of hard-coding the filter for the conveyorFile |
56
|
|
|
* |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function execute($target, Version $version = null) |
60
|
|
|
{ |
61
|
|
|
$filename = $this->options['filename']; |
62
|
|
|
$conveyorFile = $this->conveyorFile; |
|
|
|
|
63
|
|
|
|
64
|
|
|
$this->output(sprintf('Creating Phar...')); |
65
|
|
|
|
66
|
|
|
$phar = new \Phar($this->buildDir . '/' . $filename, 0, $filename); |
67
|
|
|
$phar->setSignatureAlgorithm(\Phar::SHA1); |
68
|
|
|
|
69
|
|
|
$phar->startBuffering(); |
70
|
|
|
|
71
|
|
|
$finder = new Finder(); |
72
|
|
|
$finder |
73
|
|
|
->files() |
74
|
|
|
->ignoreVCS(true) |
75
|
|
|
->in($this->buildDir) |
76
|
|
|
; |
77
|
|
|
|
78
|
|
|
foreach ($finder as $file) { |
79
|
|
|
$this->addFile($phar, $file, false); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Stubs |
83
|
|
|
$phar->setStub($this->getStub()); |
84
|
|
|
|
85
|
|
|
$phar->stopBuffering(); |
86
|
|
|
|
87
|
|
|
unset($phar); |
88
|
|
|
|
89
|
|
|
return new ExecuteResult(array($filename)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param \Phar $phar |
94
|
|
|
* @param SplFileInfo $file |
95
|
|
|
*/ |
96
|
|
|
private function addFile(\Phar $phar, SplFileInfo $file) |
97
|
|
|
{ |
98
|
|
|
$path = $file->getRelativePathname(); |
99
|
|
|
|
100
|
|
|
$content = file_get_contents($file); |
101
|
|
|
|
102
|
|
|
$content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content); |
103
|
|
|
|
104
|
|
|
$phar->addFromString($path, $content); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
private function getStub() |
111
|
|
|
{ |
112
|
|
|
$stub = <<<'EOF' |
113
|
|
|
#!/usr/bin/env php |
114
|
|
|
<?php |
115
|
|
|
|
116
|
|
|
Phar::mapPhar('@@filename@@'); |
117
|
|
|
|
118
|
|
|
require 'phar://@@filename@@/@@stub@@'; |
119
|
|
|
|
120
|
|
|
__HALT_COMPILER(); |
121
|
|
|
|
122
|
|
|
EOF; |
123
|
|
|
|
124
|
|
|
$stub = str_replace('@@filename@@', $this->options['filename'], $stub); |
125
|
|
|
$stub = str_replace('@@stub@@', $this->options['stub'], $stub); |
126
|
|
|
|
127
|
|
|
return $stub; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.