Skip to content

Commit

Permalink
refactor: Extract checking that the PHAR is readonly in a dedicated m…
Browse files Browse the repository at this point in the history
…ethod (#929)
  • Loading branch information
theofidry authored Mar 16, 2023
1 parent efc69a4 commit 2b559cf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
5 changes: 3 additions & 2 deletions src/Console/Php/PhpSettingsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace KevinGH\Box\Console\Php;

use Composer\XdebugHandler\XdebugHandler;
use KevinGH\Box\Phar\PharPhpSettings;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;
use function getenv;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __construct(LoggerInterface $logger)
$this->setLogger($logger);
$this->logger = $logger;

$this->pharReadonly = '1' === ini_get('phar.readonly');
$this->pharReadonly = PharPhpSettings::isReadonly();
}

public function check(): void
Expand Down Expand Up @@ -78,7 +79,7 @@ protected function restart(array $command): void

private function disablePharReadonly(): void
{
if (ini_get('phar.readonly')) {
if (PharPhpSettings::isReadonly()) {
Assert::notNull($this->tmpIni);

append_to_file($this->tmpIni, 'phar.readonly=0'.PHP_EOL);
Expand Down
29 changes: 29 additions & 0 deletions src/Phar/PharPhpSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

/*
* This file is part of the box project.
*
* (c) Kevin Herrera <kevin@herrera.io>
* Théo Fidry <theo.fidry@gmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace KevinGH\Box\Phar;

use function ini_get;

final class PharPhpSettings
{
public static function isReadonly(): bool
{
return '1' === ini_get('phar.readonly');
}

private function __construct()
{
}
}
4 changes: 2 additions & 2 deletions src/Pharaoh/Pharaoh.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@
namespace KevinGH\Box\Pharaoh;

use Error;
use KevinGH\Box\Phar\PharPhpSettings;
use KevinGH\Box\PharInfo\PharInfo;
use ParagonIE\ConstantTime\Hex;
use Phar;
use function copy;
use function file_put_contents;
use function ini_get;
use function is_dir;
use function is_readable;
use function is_string;
Expand Down Expand Up @@ -79,7 +79,7 @@ public function __construct(string $file, ?string $alias = null)
throw new PharError($file.' cannot be read');
}

if ('1' == ini_get('phar.readonly')) {
if (PharPhpSettings::isReadonly()) {
// TODO: the value may be something else than '1'
throw new PharError("Pharaoh cannot be used if phar.readonly is enabled in php.ini\n");
}
Expand Down
4 changes: 2 additions & 2 deletions src/Test/RequiresPharReadonlyOff.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace KevinGH\Box\Test;

use function ini_get;
use KevinGH\Box\Phar\PharPhpSettings;

/**
* @private
Expand All @@ -23,7 +23,7 @@ trait RequiresPharReadonlyOff
{
private function markAsSkippedIfPharReadonlyIsOn(): void
{
if (true === (bool) ini_get('phar.readonly')) {
if (PharPhpSettings::isReadonly()) {
$this->markTestSkipped(
'Requires phar.readonly to be set to 0. Either update your php.ini file or run this test with '
.'php -d phar.readonly=0.',
Expand Down

0 comments on commit 2b559cf

Please sign in to comment.