Skip to content

Commit

Permalink
refactor: Move the constants into a dedicated class (#1184)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Nov 21, 2023
1 parent f306ce1 commit 003a0e0
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 20 deletions.
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
"KevinGH\\Box\\": "src"
},
"files": [
"src/consts.php",
"src/functions.php"
],
"exclude-from-classmap": [
Expand Down
4 changes: 2 additions & 2 deletions src/Composer/ComposerProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

use Closure;
use Fidry\Console\IO;
use KevinGH\Box\Constants;
use RuntimeException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
use const KevinGH\Box\BOX_ALLOW_XDEBUG;

/**
* @final
Expand Down Expand Up @@ -143,7 +143,7 @@ private static function getDefaultEnvVars(): array
{
$vars = ['COMPOSER_ORIGINAL_INIS' => ''];

if ('1' === (string) getenv(BOX_ALLOW_XDEBUG)) {
if ('1' === (string) getenv(Constants::ALLOW_XDEBUG)) {
$vars['COMPOSER_ALLOW_XDEBUG'] = '1';
}

Expand Down
6 changes: 3 additions & 3 deletions src/Console/Php/PhpSettingsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

use Composer\XdebugHandler\XdebugHandler;
use Fidry\FileSystem\FS;
use KevinGH\Box\Constants;
use KevinGH\Box\Phar\PharPhpSettings;
use Psr\Log\LoggerInterface;
use Webmozart\Assert\Assert;
Expand All @@ -26,7 +27,6 @@
use function KevinGH\Box\memory_to_bytes;
use function sprintf;
use function trim;
use const KevinGH\Box\BOX_MEMORY_LIMIT;
use const PHP_EOL;

/**
Expand Down Expand Up @@ -128,7 +128,7 @@ private function bumpMemoryLimit(): void
sprintf(
'Changed the memory limit from "%s" to %s="%s"',
format_size($memoryLimitInBytes, 0),
BOX_MEMORY_LIMIT,
Constants::MEMORY_LIMIT,
format_size($userDefinedMemoryLimit, 0),
),
);
Expand All @@ -144,7 +144,7 @@ private function bumpMemoryLimit(): void

private static function getUserDefinedMemoryLimit(): ?int
{
$memoryLimit = getenv(BOX_MEMORY_LIMIT);
$memoryLimit = getenv(Constants::MEMORY_LIMIT);

if (false === $memoryLimit) {
$memoryLimitInBytes = null;
Expand Down
19 changes: 10 additions & 9 deletions src/consts.php → src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@

namespace KevinGH\Box;

// Public constants pointing to environment variable names
const BOX_MEMORY_LIMIT = 'BOX_MEMORY_LIMIT';
const BOX_ALLOW_XDEBUG = 'BOX_ALLOW_XDEBUG';
final class Constants
{
use NotInstantiable;

/**
* @internal
*
* @private
*/
const _NO_PARALLEL_PROCESSING = '_BOX_NO_PARALLEL_PROCESSING';
public const MEMORY_LIMIT = 'BOX_MEMORY_LIMIT';
public const ALLOW_XDEBUG = 'BOX_ALLOW_XDEBUG';
public const BIN = 'BOX_BIN';

/** @internal */
public const NO_PARALLEL_PROCESSING = '_BOX_NO_PARALLEL_PROCESSING';
}
4 changes: 2 additions & 2 deletions src/Phar/PharInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

use Fidry\FileSystem\FS;
use KevinGH\Box\Console\Command\Extract;
use KevinGH\Box\Constants;
use OutOfBoundsException;
use Phar;
use RuntimeException;
Expand Down Expand Up @@ -347,8 +348,7 @@ private static function getPhpExecutable(): string

private static function getBoxBin(): string
{
// TODO: move the constraint strings declaration in one place
return getenv('BOX_BIN') ?: $_SERVER['SCRIPT_NAME'];
return getenv(Constants::BIN) ?: $_SERVER['SCRIPT_NAME'];
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ class_alias(\Symfony\Component\Finder\Finder::class, \Isolated\Symfony\Component
*/
function disable_parallel_processing(): void
{
if (false === defined(_NO_PARALLEL_PROCESSING)) {
define(_NO_PARALLEL_PROCESSING, true);
if (false === defined(Constants::NO_PARALLEL_PROCESSING)) {
define(Constants::NO_PARALLEL_PROCESSING, true);
}
}

Expand All @@ -193,7 +193,7 @@ function disable_parallel_processing(): void
*/
function is_parallel_processing_enabled(): bool
{
return false === defined(_NO_PARALLEL_PROCESSING) || false === constant(_NO_PARALLEL_PROCESSING);
return false === defined(Constants::NO_PARALLEL_PROCESSING) || false === constant(Constants::NO_PARALLEL_PROCESSING);
}

/**
Expand Down

0 comments on commit 003a0e0

Please sign in to comment.