Skip to content

Commit

Permalink
Refactor: Phar stub entrypoint initialization
Browse files Browse the repository at this point in the history
- Inclusion of the entrypoint script contents is now handled by the compile command
  • Loading branch information
yannoff committed Mar 2, 2024
1 parent 3b55961 commit 5cae825
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/Command/Compile.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ protected function initBuilder(string $main): self
{
$this->info('Initializing Phar builder...');
$this->builder = PharBuilder::create($main);
$this->info('Adding stub entrypoint script contents...');
$this->addFile($main);

return $this;
}
Expand Down
12 changes: 12 additions & 0 deletions src/Phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ public function addFileContents(string $filename, string $localName = null, bool
$contents = $minify ? php_strip_whitespace($filename) : file_get_contents($filename);
$this[$key] = $contents;
}

/**
* Check whether the given file exists in archive
*
* @param string $file
*
* @return bool
*/
public function has(string $file): bool
{
return $this->offsetExists($file);
}
}
9 changes: 7 additions & 2 deletions src/PharBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

namespace Yannoff\PhpCodeCompiler;

use LogicException;

/**
* Phar archive builder class
*/
Expand Down Expand Up @@ -91,8 +93,6 @@ public function init(): self
$this->archive = new Phar($this->pharname);
$this->archive->startBuffering();

$this->archive->addFileContents($this->main);

return $this;
}

Expand All @@ -118,6 +118,11 @@ public function setBanner(string $banner): self
*/
public function compile(string $output, string $compression = 'GZ')
{
// Check that entrypoint script contents has been added to the archive before proceeding
if (!$this->archive->has($this->main)) {
throw new LogicException("Main script {$this->main} contents must be added to the archive");
}

$c = constant('Phar::' . $compression);
$this->archive->compressFiles($c);

Expand Down

0 comments on commit 5cae825

Please sign in to comment.