Skip to content

Commit

Permalink
refactor: Move bootstrapping function into a dedicated class (#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Dec 11, 2023
1 parent 6989f3f commit 937e0ee
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
4 changes: 2 additions & 2 deletions bin/box
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ if (false === in_array(PHP_SAPI, ['cli', 'phpdbg', 'embed', 'micro'], true)) {
throw new RuntimeException('Unable to find the Composer autoloader.');
})();

register_aliases();
register_error_handler();
Bootstrap::registerAliases();
Bootstrap::registerErrorHandler();

$io = IO::createDefault();
OutputFormatterConfigurator::configure($io);
Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
<directory>src/</directory>
</include>
<exclude>
<file>src/bootstrap.php</file>
<file>src/consts.php</file>
<file>src/Bootstrap.php</file>
<file>src/functions.php</file>
<file>src/Console/Command/Build.php</file>
<file>src/Console/Command/ChangeableWorkingDirectory.php</file>
Expand Down
50 changes: 50 additions & 0 deletions src/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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 ErrorException;
use Isolated\Symfony\Component\Finder\Finder as IsolatedFinder;
use Symfony\Component\Finder\Finder as SymfonyFinder;
use function class_alias;
use function class_exists;
use function error_reporting;
use function set_error_handler;

final class Bootstrap
{
use NotInstantiable;

/**
* @private
*/
public static function registerAliases(): void
{
// Exposes the finder used by PHP-Scoper PHAR to allow its usage in the configuration file.
if (false === class_exists(IsolatedFinder::class)) {
class_alias(SymfonyFinder::class, IsolatedFinder::class);
}
}

public static function registerErrorHandler(): void
{
set_error_handler(
static function (int $code, string $message, string $file = '', int $line = -1): void {
if (error_reporting() & $code) {
throw new ErrorException($message, 0, $code, $file, $line);
}
},
);
}
}
7 changes: 3 additions & 4 deletions src/Parallelization/ProcessFileTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@
use Amp\Sync\Channel;
use Fidry\FileSystem\FS;
use Humbug\PhpScoper\Symbol\SymbolsRegistry;
use KevinGH\Box\Bootstrap;
use KevinGH\Box\Compactor\Compactors;
use KevinGH\Box\MapFile;
use function array_map;
use function KevinGH\Box\register_aliases;
use function KevinGH\Box\register_error_handler;

/**
* @private
Expand All @@ -45,8 +44,8 @@ public function run(Channel $channel, Cancellation $cancellation): TaskResult
{
chdir($this->cwd);

register_aliases();
register_error_handler();
Bootstrap::registerAliases();
Bootstrap::registerErrorHandler();

$mapFile = $this->mapFile;
$compactors = $this->compactors;
Expand Down

0 comments on commit 937e0ee

Please sign in to comment.