forked from phpDocumentor/phpDocumentor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPharCompiler.php
More file actions
234 lines (213 loc) · 6.27 KB
/
Copy pathPharCompiler.php
File metadata and controls
234 lines (213 loc) · 6.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* phpDocumentor
*
* PHP Version 5.3
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Gordon Franke <info@nevalon.de>
* @author Mike van Riel <mike.vanriel@naenius.com>
* @copyright 2010-2012 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/
namespace phpDocumentor;
use Symfony\Component\Finder\Finder;
/**
* The Compiler class compiles the phpDocumentor utility.
*
* It is inspired by Goutte's Phar Compiler
* https://github.com/fabpot/Goutte/blob/master/src/Goutte/Compiler.php
*/
class PharCompiler
{
/**
* Compiles the source code into a PHAR file with the given name,
*
* @param string $pharFile
*
* @see getFiles() for the file selection process.
*
* @return void
*/
public function compile($pharFile = 'phpDocumentor.phar')
{
$this->removeExistingPharArchive($pharFile);
$phar = $this->initializePharArchive($pharFile);
$phar->startBuffering();
$this->addFilesToPharArchive($this->getFiles(), $phar);
$this->addStubsToPharArchive($phar);
$phar->stopBuffering();
echo '>> Finished creating phar archive' . PHP_EOL;
unset($phar);
}
/**
* Detects whether the given PHAR file alrready exists and removes it if so.
*
* @param string $pharFile
*
* @return void
*/
protected function removeExistingPharArchive($pharFile)
{
echo '>> Checking whether a phar file has been left from a previous run'
. PHP_EOL;
if (file_exists($pharFile)) {
echo '>> Removing previous phar' . PHP_EOL;
unlink($pharFile);
}
}
/**
* Initializes a new PHAR archive with the given name.
*
* The PHAR archive's Signature's algorithm is set to SHA1.
*
* @param string $pharFile
*
* @return \Phar
*/
protected function initializePharArchive($pharFile)
{
echo '>> Initializing new phar archive' . PHP_EOL;
$phar = new \Phar($pharFile, 0, 'phpDocumentor');
$phar->setSignatureAlgorithm(\Phar::SHA1);
return $phar;
}
/**
* Retrieves an array containing all filenames that are to be included.
*
* The following files are added:
*
* - LICENCE
* - README.md
* - bin/*
* - data/* (excluding the output folder)
* - src/*
* - vendor/*
*
* @return string[]
*/
protected function getFiles()
{
$files = array('LICENSE', 'README.md');
$finder = new Finder();
$iterator = $finder->files()
->in(array('bin', 'data', 'src', 'vendor'))
->notName('*.rst')
->notName('*.md')
->exclude(
array(
'output',
'behat',
'cilex/cilex/tests',
'dflydev/markdown/tests',
'nikic/php-parser/doc',
'nikic/php-parser/test',
'nikic/php-parser/test_old',
'phpdocumentor/fileset/tests',
'phpdocumentor/graphviz/tests',
'phpdocumentor/reflection-docblock/tests',
'pimple/pimple/tests',
'twig/twig/test',
)
);
return array_merge($files, iterator_to_array($iterator));
}
/**
* Processes the given array of filenames to be added to the PHAR archive.
*
* @param string[] $files
* @param \Phar $phar
*
* @return void
*/
protected function addFilesToPharArchive(array $files, \Phar $phar)
{
echo '>> Found ' . count($files) . ' files to add to archive' . PHP_EOL;
$counter = 0;
foreach ($files as $file) {
echo '.';
$counter++;
if ($counter % 70 == 0) {
echo ' [' . $counter . '/' . count($files) . ']' . PHP_EOL;
}
$this->addFileToPharArchive($file, $phar);
}
echo PHP_EOL;
echo '>> Finished adding files to archive' . PHP_EOL;
}
/**
* Adds the given file to the PHAR archive,
*
* Before adding files to the archive they are bein converted to
* relative paths.
*
* @param string $file
* @param \Phar $phar
*
* @return void
*/
protected function addFileToPharArchive($file, \Phar $phar)
{
$path = str_replace(__DIR__ . '/', '', $file);
$file_contents = file_get_contents($file);
$file_contents = str_replace('#!/usr/bin/env php', '', $file_contents);
$phar->addFromString($path, $file_contents);
}
/**
* Adds the stubs for the CLI and Web interaction to the PHAR archive.
*
* @param \Phar $phar
*
* @see getCliStub() for the Stub for Command Line Interaction
* @see getWebStub() for the Stub for Web Interaction
*
* @return void
*/
protected function addStubsToPharArchive($phar)
{
$phar['_cli_stub.php'] = $this->getCliStub();
$phar['_web_stub.php'] = $this->getWebStub();
$phar->setDefaultStub('_cli_stub.php', '_web_stub.php');
}
/**
* Adds a stub file that initiates the command line interaction.
*
* @return string
*/
protected function getCliStub()
{
return "<?php " . $this->getLicense()
. " require_once __DIR__.'/bin/phpdoc.php'; __HALT_COMPILER();";
}
/**
* Adds a stub file that initiates the web interaction.
*
* @return string
*/
protected function getWebStub()
{
return "<?php " . $this->getLicense()
. "throw new \LogicException('This PHAR file can only be used from "
."the CLI.'); __HALT_COMPILER();";
}
/**
* Returns a license file-level DocBlock for use in the Stubs.
*
* @return string
*/
protected function getLicense()
{
return '
/**
* phpDocumentor
*
* PHP Version 5.3
*
* @author Mike van Riel <mike.vanriel@naenius.com>
* @copyright 2010-2012 Mike van Riel / Naenius (http://www.naenius.com)
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://phpdoc.org
*/';
}
}