-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #365 from reliforp/improve-speedscope-converter
Add a configuration option to the speedscope converter for the handling of utf8 errors
- Loading branch information
Showing
6 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/Converter/Speedscope/Settings/SpeedscopeConverterSettings.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Converter\Speedscope\Settings; | ||
|
||
final class SpeedscopeConverterSettings | ||
{ | ||
public function __construct( | ||
public Utf8ErrorHandlingType $utf8_error_handling_type, | ||
) { | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Converter/Speedscope/Settings/SpeedscopeConverterSettingsException.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Converter\Speedscope\Settings; | ||
|
||
use Reli\Inspector\Settings\InspectorSettingsException; | ||
|
||
final class SpeedscopeConverterSettingsException extends InspectorSettingsException | ||
{ | ||
public const UNSUPPORTED_UTF8_ERROR_HANDLING = 1; | ||
|
||
protected const ERRORS = [ | ||
self::UNSUPPORTED_UTF8_ERROR_HANDLING => 'unsupported utf8 error handling type is specified', | ||
]; | ||
} |
57 changes: 57 additions & 0 deletions
57
src/Converter/Speedscope/Settings/SpeedscopeConverterSettingsFromConsoleInput.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Converter\Speedscope\Settings; | ||
|
||
use PhpCast\Cast; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
final class SpeedscopeConverterSettingsFromConsoleInput | ||
{ | ||
/** @codeCoverageIgnore */ | ||
public function setOptions(Command $command): void | ||
{ | ||
$command | ||
->addOption( | ||
'utf8-errors', | ||
null, | ||
InputOption::VALUE_REQUIRED, | ||
'utf8 error handling type (ignore|substitute|fail)', | ||
'ignore' | ||
); | ||
; | ||
} | ||
|
||
public function createSettings(InputInterface $input): SpeedscopeConverterSettings | ||
{ | ||
return new SpeedscopeConverterSettings( | ||
$this->utf8ErrorHandlingTypeFromString( | ||
Cast::toString($input->getOption('utf8-errors')) | ||
), | ||
); | ||
} | ||
|
||
private function utf8ErrorHandlingTypeFromString(string $input): Utf8ErrorHandlingType | ||
{ | ||
return match ($input) { | ||
'substitute' => Utf8ErrorHandlingType::Substitute, | ||
'ignore' => Utf8ErrorHandlingType::Ignore, | ||
'fail' => Utf8ErrorHandlingType::Fail, | ||
default => throw SpeedscopeConverterSettingsException::create( | ||
SpeedscopeConverterSettingsException::UNSUPPORTED_UTF8_ERROR_HANDLING | ||
), | ||
}; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/Converter/Speedscope/Settings/Utf8ErrorHandlingType.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of the reliforp/reli-prof package. | ||
* | ||
* (c) sji <sji@sj-i.dev> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Reli\Converter\Speedscope\Settings; | ||
|
||
enum Utf8ErrorHandlingType | ||
{ | ||
case Ignore; | ||
case Substitute; | ||
case Fail; | ||
|
||
public function toFlag(): int | ||
{ | ||
return match ($this) { | ||
self::Ignore => \JSON_INVALID_UTF8_IGNORE, | ||
self::Substitute => \JSON_INVALID_UTF8_SUBSTITUTE, | ||
self::Fail => 0, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters