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 bootstrapping function into a dedicated class #1263

Merged
merged 1 commit into from
Dec 11, 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
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
Loading