Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Move finding the PHP or Box bin into a dedicated class #1185

Merged
merged 4 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/ExecutableFinder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <[email protected]>
* Théo Fidry <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box;

use RuntimeException;
use Symfony\Component\Process\PhpExecutableFinder as SymfonyPhpExecutableFinder;

final class ExecutableFinder
{
private static string $boxExecutable;
private static string $phpExecutable;

public static function findBoxExecutable(): string
{
if (isset(self::$boxExecutable)) {
return self::$boxExecutable;
}

self::$boxExecutable = getenv(Constants::BIN) ?: $_SERVER['SCRIPT_NAME'];

return self::$boxExecutable;
}

public static function findPhpExecutable(): string
{
if (isset(self::$phpExecutable)) {
return self::$phpExecutable;
}

$phpExecutable = (new SymfonyPhpExecutableFinder())->find();

if (false === $phpExecutable) {
throw new RuntimeException('Could not find a PHP executable.');
}

self::$phpExecutable = $phpExecutable;

return self::$phpExecutable;
}
}
31 changes: 3 additions & 28 deletions src/Phar/PharInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@

use Fidry\FileSystem\FS;
use KevinGH\Box\Console\Command\Extract;
use KevinGH\Box\Constants;
use KevinGH\Box\ExecutableFinder;
use OutOfBoundsException;
use Phar;
use RuntimeException;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\PhpExecutableFinder;
use Symfony\Component\Process\Process;
use function bin2hex;
use function file_exists;
use function getenv;
use function is_readable;
use function iter\mapKeys;
use function iter\toArrayWithKeys;
Expand Down Expand Up @@ -287,9 +284,9 @@

private static function dumpPhar(string $file, string $tmp): void
{
$extractPharProcess = new Process([

Check warning on line 287 in src/Phar/PharInfo.php

View workflow job for this annotation

GitHub Actions / Infection (PHP 8.2)

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ } private static function dumpPhar(string $file, string $tmp) : void { - $extractPharProcess = new Process([ExecutableFinder::findPhpExecutable(), ExecutableFinder::findBoxExecutable(), 'extract', $file, $tmp, '--no-interaction', '--internal']); + $extractPharProcess = new Process([ExecutableFinder::findBoxExecutable(), 'extract', $file, $tmp, '--no-interaction', '--internal']); $extractPharProcess->run(); if (false === $extractPharProcess->isSuccessful()) { throw new InvalidPhar($extractPharProcess->getErrorOutput(), $extractPharProcess->getExitCode(), new ProcessFailedException($extractPharProcess));
self::getPhpExecutable(),
self::getBoxBin(),
ExecutableFinder::findPhpExecutable(),
ExecutableFinder::findBoxExecutable(),
'extract',
$file,
$tmp,
Expand Down Expand Up @@ -329,28 +326,6 @@
return [$meta, $dumpedFiles];
}

private static function getPhpExecutable(): string
{
if (isset(self::$phpExecutable)) {
return self::$phpExecutable;
}

$phpExecutable = (new PhpExecutableFinder())->find();

if (false === $phpExecutable) {
throw new RuntimeException('Could not find a PHP executable.');
}

self::$phpExecutable = $phpExecutable;

return self::$phpExecutable;
}

private static function getBoxBin(): string
{
return getenv(Constants::BIN) ?: $_SERVER['SCRIPT_NAME'];
}

/**
* @param array<string, array{'compression': CompressionAlgorithm, compressedSize: int}> $filesMeta
*/
Expand Down
Loading