Skip to content

Add get_*_handler polyfills #521

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

Merged
merged 1 commit into from
Mar 31, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ jobs:
- php: '8.2'
- php: '8.3'
- php: '8.4'
- php: '8.5'
fail-fast: false

steps:
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Polyfills are provided for:
- the `Deprecated` attribute introduced in PHP 8.4;
- the `mb_trim`, `mb_ltrim` and `mb_rtrim` functions introduced in PHP 8.4;
- the `CURL_HTTP_VERSION_3` and `CURL_HTTP_VERSION_3ONLY` constants introduced in PHP 8.4;
- the `get_error_handler` and `get_exception_handler` functions introduced in PHP 8.5;

It is strongly recommended to upgrade your PHP version and/or install the missing
extensions whenever possible. This polyfill should be used only when there is no
Expand Down Expand Up @@ -105,6 +106,7 @@ should **not** `require` the `symfony/polyfill` package, but the standalone ones
- `symfony/polyfill-php82` for using the PHP 8.2 functions,
- `symfony/polyfill-php83` for using the PHP 8.3 functions,
- `symfony/polyfill-php84` for using the PHP 8.4 functions,
- `symfony/polyfill-php85` for using the PHP 8.5 functions,
- `symfony/polyfill-iconv` for using the iconv functions,
- `symfony/polyfill-intl-grapheme` for using the `grapheme_*` functions,
- `symfony/polyfill-intl-idn` for using the `idn_to_ascii` and `idn_to_utf8` functions,
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"symfony/polyfill-php82": "self.version",
"symfony/polyfill-php83": "self.version",
"symfony/polyfill-php84": "self.version",
"symfony/polyfill-php85": "self.version",
"symfony/polyfill-iconv": "self.version",
"symfony/polyfill-intl-grapheme": "self.version",
"symfony/polyfill-intl-icu": "self.version",
Expand Down
19 changes: 19 additions & 0 deletions src/Php85/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2025-present Fabien Potencier

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
36 changes: 36 additions & 0 deletions src/Php85/Php85.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Polyfill\Php85;

/**
* @author Pierre Ambroise <pierre27.ambroise@gmail.com>
*
* @internal
*/
final class Php85
{
public static function get_error_handler(): ?callable
{
$handler = set_error_handler(null);
restore_error_handler();

return $handler;
}

public static function get_exception_handler(): ?callable
{
$handler = set_exception_handler(null);
restore_exception_handler();

return $handler;
}
}
14 changes: 14 additions & 0 deletions src/Php85/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Symfony Polyfill / Php85
========================

This component provides features added to PHP 8.5 core:

- [`get_error_handler` and `get_exception_handler`](https://wiki.php.net/rfc/get-error-exception-handler)

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/main/README.md).

License
=======

This library is released under the [MIT license](LICENSE).
24 changes: 24 additions & 0 deletions src/Php85/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

use Symfony\Polyfill\Php85 as p;

if (\PHP_VERSION_ID >= 80500) {
return;
}

if (!function_exists('get_error_handler')) {
function get_error_handler(): ?callable { return p\Php85::get_error_handler(); }
}

if (!function_exists('get_exception_handler')) {
function get_exception_handler(): ?callable { return p\Php85::get_exception_handler(); }
}
32 changes: 32 additions & 0 deletions src/Php85/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "symfony/polyfill-php85",
"type": "library",
"description": "Symfony polyfill backporting some PHP 8.5+ features to lower PHP versions",
"keywords": ["polyfill", "shim", "compatibility", "portable"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"require": {
"php": ">=7.2"
},
"autoload": {
"psr-4": { "Symfony\\Polyfill\\Php85\\": "" },
"files": [ "bootstrap.php" ]
},
"minimum-stability": "dev",
"extra": {
"thanks": {
"name": "symfony/polyfill",
"url": "https://github.com/symfony/polyfill"
}
}
}
4 changes: 4 additions & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,7 @@
if (\PHP_VERSION_ID < 80400) {
require __DIR__.'/Php84/bootstrap.php';
}

if (\PHP_VERSION_ID < 80500) {
require __DIR__.'/Php85/bootstrap.php';
}
92 changes: 92 additions & 0 deletions tests/Php85/Php85Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Polyfill\Tests\Php85;

use PHPUnit\Framework\TestCase;

class Php85Test extends TestCase
{
/**
* @dataProvider provideHandler
*/
public function testGetErrorHandler($expected, $handler): void
{
set_error_handler($handler);
try {
$result = get_error_handler();
} finally {
restore_error_handler();
}

$this->assertSame($expected, $result);
}

public function testErrorStableReturnValue(): void
{
$this->assertSame(get_error_handler(), get_error_handler());
}

/**
* @dataProvider provideHandler
*/
public function testGetExceptionHandler($expected, $handler): void
{
set_exception_handler($handler);
try {
$result = get_exception_handler();
} finally {
restore_exception_handler();
}

$this->assertSame($expected, $result);
}

public function testExceptionStableReturnValue(): void
{
$this->assertSame(get_exception_handler(), get_exception_handler());

}

public static function provideHandler()
{
// String handler
yield ['var_dump', 'var_dump'];

// Null handler
yield [null, null];

// Static method array handler
yield [[TestHandler::class, 'handleStatic'], [TestHandler::class, 'handleStatic']];

// Static method string handler
yield ['Symfony\Polyfill\Tests\Php85\TestHandler::handleStatic', 'Symfony\Polyfill\Tests\Php85\TestHandler::handleStatic'];

// Instance method array
$handler = new TestHandler();
yield [[$handler, 'handle'], [$handler, 'handle']];

// Invokable object
$handler = new TestHandlerInvokable();
yield [$handler, $handler];
}
}

class TestHandler
{
public static function handleStatic() {}
public function handle() {}
}

class TestHandlerInvokable
{
public function __invoke() {}
}
Loading