Skip to content

Commit 582308f

Browse files
committed
Refactor: Phar stub entrypoint initialization
- Inclusion of the entrypoint script contents is now handled by the compile command
1 parent 3b55961 commit 582308f

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

src/Command/Compile.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ protected function initBuilder(string $main): self
7272
{
7373
$this->info('Initializing Phar builder...');
7474
$this->builder = PharBuilder::create($main);
75+
$this->info('Adding stub entrypoint script contents...');
76+
$this->addFile($main);
7577

7678
return $this;
7779
}

src/Phar.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,16 @@ public function addFileContents(string $filename, string $localName = null, bool
3737
$contents = $minify ? php_strip_whitespace($filename) : file_get_contents($filename);
3838
$this[$key] = $contents;
3939
}
40+
41+
/**
42+
* Check whether the given file exists in archive
43+
*
44+
* @param string $file
45+
*
46+
* @return bool
47+
*/
48+
public function has(string $file): bool
49+
{
50+
return $this->offsetExists($file);
51+
}
4052
}

src/PharBuilder.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace Yannoff\PhpCodeCompiler;
1717

18+
use LogicException;
19+
1820
/**
1921
* Phar archive builder class
2022
*/
@@ -91,8 +93,6 @@ public function init(): self
9193
$this->archive = new Phar($this->pharname);
9294
$this->archive->startBuffering();
9395

94-
$this->archive->addFileContents($this->main);
95-
9696
return $this;
9797
}
9898

@@ -118,6 +118,11 @@ public function setBanner(string $banner): self
118118
*/
119119
public function compile(string $output, string $compression = 'GZ')
120120
{
121+
// Check that entrypoint script contents has been added to the archive before proceeding
122+
if (!$this->archive->has($this->main)) {
123+
throw new LogicException("Main script {$this->main} contents must be added to the archive");
124+
}
125+
121126
$c = constant('Phar::' . $compression);
122127
$this->archive->compressFiles($c);
123128

0 commit comments

Comments
 (0)