Skip to content

Commit 0b2101e

Browse files
committed
Add --shebang-less switch
1 parent 89d5b6e commit 0b2101e

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ Name / Shorthand | Type | Description
5151
`--file`, `-f` | multi | Adds a single file to the archive |n
5252
`--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
5353
`--meta`, `-m` | multi | Adds a metadata to the archive<br/>_Metadata must be specified in the `$key:$value` format_ |n
54+
`--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

5556

5657
### Examples

src/Command/Compile.php

+7-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function configure()
4141
->addOption('meta', 'm', Option::MULTI, 'Add a metadata property (eg: "-m $key:$value")')
4242
->addOption('output', 'o', Option::VALUE, 'Set the compiled archive output name')
4343
->addOption('banner', 'b', Option::VALUE, 'Load legal notice from the given banner file')
44+
->addOption('shebang-less', '', Option::FLAG, 'Produce a stub deprived of the shebang directive')
4445
;
4546
}
4647

@@ -58,13 +59,15 @@ public function execute()
5859
$main = $this->require('main');
5960
$output = $this->require('output');
6061

62+
$shebang = (!$this->getOption('shebang-less'));
63+
6164
$this
6265
->initBuilder($main)
6366
->addFiles($files)
6467
->addDirectories($dirs)
6568
->setNotice($banner)
6669
->addMetadata($meta)
67-
->publish($output)
70+
->publish($output, $shebang)
6871
->info('Build complete.')
6972
;
7073
}
@@ -202,14 +205,15 @@ protected function setNotice(string $banner = null): self
202205
* Compile the Phar archive and write contents to disk
203206
*
204207
* @param string $output Path to the phar archive output file
208+
* @param bool $shebang Whether to include the shebang line
205209
* @param string $compression Compression type - "GZ" or "BZ2"
206210
*
207211
* @return self
208212
*/
209-
protected function publish(string $output, string $compression = 'GZ'): self
213+
protected function publish(string $output, bool $shebang, string $compression = 'GZ'): self
210214
{
211215
$this->info("Writing Phar archive to <strong>$output</strong>...");
212-
$this->builder->compile($output, $compression);
216+
$this->builder->compile($output, $shebang, $compression);
213217

214218
return $this;
215219
}

src/PharBuilder.php

+12-9
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,10 @@ public function setBanner(string $banner): self
122122
* Compress files, generate the stub and save PHAR to the output file
123123
*
124124
* @param string $output Path to the final output file
125+
* @param bool $shebang Include a shebang directive line in the stub ?
125126
* @param string $compression Compression type - "GZ" or "BZ2" (defaults to "GZ")
126127
*/
127-
public function compile(string $output, string $compression = 'GZ')
128+
public function compile(string $output, bool $shebang, string $compression = 'GZ')
128129
{
129130
// Check that entrypoint script contents has been added to the archive before proceeding
130131
if (!$this->archive->has($this->main)) {
@@ -137,7 +138,7 @@ public function compile(string $output, string $compression = 'GZ')
137138
$this->archive->compressFiles($c);
138139

139140
// NB: It's important to set the stub AFTER the files compression step so it is kept as plain text
140-
$this->archive->setStub($this->stub($this->main, $this->banner));
141+
$this->archive->setStub($this->stub($this->main, $shebang, $this->banner));
141142
// Create file on the disk
142143
$this->archive->stopBuffering();
143144
// Make file executable
@@ -159,17 +160,19 @@ public function list(): array
159160
/**
160161
* Generate the PHAR stub definition contents
161162
*
162-
* @param string $main The main entrypoint script
163-
* @param ?string $banner Optional legal notice text
163+
* @param string $main The main entrypoint script
164+
* @param bool $shebang Whether to include the shebang line
165+
* @param ?string $banner Optional legal notice text
164166
*
165167
* @return string
166168
*/
167-
protected function stub(string $main, string $banner = null): string
169+
protected function stub(string $main, bool $shebang = true, string $banner = null): string
168170
{
169-
$lines = [
170-
'#!/usr/bin/env php',
171-
'<?php',
172-
];
171+
$lines = [];
172+
if ($shebang) {
173+
$lines[] = '#!/usr/bin/env php';
174+
}
175+
$lines[] = '<?php';
173176
if ($banner) {
174177
$lines[] = $banner;
175178
}

0 commit comments

Comments
 (0)