Skip to content

Commit 889aa83

Browse files
committed
Runtime: Add --no-minify flag
1 parent 0e4f2cd commit 889aa83

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ Name / Shorthand | Type | Description
5050
`--file`, `-f` | multi | Adds a single file to the archive |n
5151
`--dir`, `-d` | multi | Adds a sources directory to the archive<br/>_Possible dir spec formats:<br/>- `$dir` => include all files in directory<br/>- `$dir:$extension` => filter files on a specific extension_ |n
5252
`--meta`, `-m` | multi | Adds a metadata to the archive<br/>_Metadata must be specified in the `$key:$value` format_ |n
53+
`--no-minify`, `-n` | flag | Don't minify PHP source files<br/>_Useful for debugging the compiled executable in case of runtime errors_ |n
5354
`--shebang-less` | flag | Produce a stub deprived of the shebang directive<br/>_Useful when the phar is meant to be included instead of being executed directly_ |n
5455
`--quiet`, `-q` | flag | Reduce output messages amount: set verbosity level to `INFO` instead of default `DEBUG` |n
5556

src/Command/Compile.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function configure()
4646
->addOption('output', 'o', Option::VALUE, 'Set the compiled archive output name')
4747
->addOption('banner', 'b', Option::VALUE, 'Load legal notice from the given banner file')
4848
->addOption('shebang-less', '', Option::FLAG, 'Produce a stub deprived of the shebang directive')
49+
->addOption('no-minify', 'n', Option::FLAG, "Don't minify php source files")
4950
// Global options
5051
->addOption('quiet', 'q', Option::FLAG, 'Set output verbosity level to INFO instead of DEBUG')
5152
;
@@ -66,14 +67,15 @@ public function execute()
6667
$output = $this->require('output');
6768

6869
$shebang = (!$this->getOption('shebang-less'));
70+
$min = (!$this->getOption('no-minify'));
6971

7072
$quiet = $this->getOption('quiet');
7173
$this->setVerbosity($quiet ? Verbosity::INFO : Verbosity::DEBUG);
7274

7375
$this
74-
->initBuilder($main)
75-
->addFiles($files)
76-
->addDirectories($dirs)
76+
->initBuilder($main, $min)
77+
->addFiles($files, $min)
78+
->addDirectories($dirs, $min)
7779
->setNotice($banner)
7880
->addMetadata($meta)
7981
->publish($output, $shebang)
@@ -85,15 +87,16 @@ public function execute()
8587
* Creates & store the Phar builder instance
8688
*
8789
* @param string $main The main entrypoint script
90+
* @param bool $min Toggle PHP files minifying
8891
*
8992
* @return self
9093
*/
91-
protected function initBuilder(string $main): self
94+
protected function initBuilder(string $main, bool $min): self
9295
{
9396
$this->info('Initializing Phar builder...');
9497
$this->builder = PharBuilder::create($main);
9598
$this->info('Adding stub entrypoint script contents...');
96-
$this->addFile($main);
99+
$this->addFile($main, $min);
97100

98101
return $this;
99102
}
@@ -103,15 +106,16 @@ protected function initBuilder(string $main): self
103106
*
104107
* @param string $directory The directory to scan for contents
105108
* @param ?string $extensions Filter on extension, may be "php" or "(php|phtml)"
109+
* @param bool $min Toggle PHP files minifying (defaults: true)
106110
*
107111
* @return int The number of added files
108112
*/
109-
protected function addDirectory(string $directory, string $extensions = null): int
113+
protected function addDirectory(string $directory, string $extensions = null, bool $min = true): int
110114
{
111115
$filter = ($extensions) ? sprintf('/\.%s$/', $extensions) : '';
112116
$files = Directory::find($directory, $filter);
113117

114-
array_walk($files, function ($file) { $this->addFile($file); });
118+
array_walk($files, function ($file) use ($min) { $this->addFile($file, $min); });
115119

116120
return count($files);
117121
}
@@ -120,17 +124,18 @@ protected function addDirectory(string $directory, string $extensions = null): i
120124
* Add a single file to the archive builder
121125
*
122126
* @param string $file A relative or absolute file path
127+
* @param bool $min Toggle PHP files minifying
123128
*
124129
*/
125-
protected function addFile(string $file)
130+
protected function addFile(string $file, bool $min)
126131
{
127132
$fullpath = $this->fullpath($file);
128133

129134
$this->debug('+ ' . $file, 'grey');
130135

131136
// Only minify pure PHP source files, other files such as
132137
// code templates for instance, should be left as-is
133-
$minify = (pathinfo($file, PATHINFO_EXTENSION) === 'php');
138+
$minify = (pathinfo($file, PATHINFO_EXTENSION) === 'php') && $min;
134139

135140
$this->builder->addFile($fullpath, $file, $minify);
136141
}
@@ -139,18 +144,19 @@ protected function addFile(string $file)
139144
* Add a list of directory specifications to the archive builder
140145
*
141146
* @param string[] $dirs A list of specs in the form "$dir" or "$dir:$extension"
147+
* @param bool $min Toggle PHP files minifying
142148
*
143149
* @return self
144150
*/
145-
protected function addDirectories(array $dirs): self
151+
protected function addDirectories(array $dirs, bool $min): self
146152
{
147153
foreach ($dirs as $spec) {
148154
list($directory, $extensions) = explode(':', $spec);
149155

150156
$wildcard = $extensions ? "*.$extensions" : 'all';
151157
$this->info("Scanning directory <strong>$directory</strong> for <strong>$wildcard</strong> files...");
152158

153-
$count = $this->addDirectory($directory, $extensions);
159+
$count = $this->addDirectory($directory, $extensions, $min);
154160

155161
$this->info("Added {$count} files.", 'grey');
156162
}
@@ -162,14 +168,15 @@ protected function addDirectories(array $dirs): self
162168
* Add a list of single files to the archive builder
163169
*
164170
* @param string[] $files A list of relative or absolute file paths
171+
* @param bool $min Toggle PHP files minifying
165172
*
166173
* @return self
167174
*/
168-
protected function addFiles(array $files): self
175+
protected function addFiles(array $files, bool $min): self
169176
{
170177
foreach ($files as $file) {
171178
$this->info("Adding single file <strong>$file</strong>...");
172-
$this->addFile($file);
179+
$this->addFile($file, $min);
173180
}
174181

175182
return $this;

0 commit comments

Comments
 (0)