Skip to content

Commit ebc6b56

Browse files
Improve Random polyfill a bit
1 parent 930fa6c commit ebc6b56

File tree

12 files changed

+128
-11
lines changed

12 files changed

+128
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# 1.27.0
22

33
* Add PHP 8.3 polyfill for `json_validate()`
4+
* Add polyfill for PHP 8.2's `Random` interfaces, exceptions and `Secure` engine
45
* Fix `IntlDateFormatter::formatObject()` signature
56

67
# 1.26.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Polyfills are provided for:
6767
- the `SensitiveParameterValue` class introduced in PHP 8.2;
6868
- the `Random\Engine` interface introduced in PHP 8.2;
6969
- the `Random\CryptoSafeEngine` interface introduced in PHP 8.2;
70-
- the `Random\Engine\Secure` class introduced in PHP 8.2;
70+
- the `Random\Engine\Secure` class introduced in PHP 8.2 (check [arokettu/random-polyfill](https://packagist.org/packages/arokettu/random-polyfill) for more engines);
7171
- the `json_validate` function introduced in PHP 8.3;
7272

7373
It is strongly recommended to upgrade your PHP version and/or install the missing

src/Php82/NoDynamicProperties.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Polyfill\Php82;
13+
14+
/**
15+
* @internal
16+
*/
17+
trait NoDynamicProperties
18+
{
19+
public function __set(string $name, $value): void
20+
{
21+
throw new \Error('Cannot create dynamic property '.self::class.'::$'.$name);
22+
}
23+
}

src/Php82/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ This component provides features added to PHP 8.2 core:
88
- [`SensitiveParameterValue`](https://wiki.php.net/rfc/redact_parameters_in_back_traces)
99
- [`Random\Engine`](https://wiki.php.net/rfc/rng_extension)
1010
- [`Random\Engine\CryptoSafeEngine`](https://wiki.php.net/rfc/rng_extension)
11-
- [`Random\Engine\Secure`](https://wiki.php.net/rfc/rng_extension)
11+
- [`Random\Engine\Secure`](https://wiki.php.net/rfc/rng_extension) (check [arokettu/random-polyfill](https://packagist.org/packages/arokettu/random-polyfill) for more engines)
1212

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

src/Php82/Random/Engine/Secure.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,40 @@
1111

1212
namespace Symfony\Polyfill\Php82\Random\Engine;
1313

14+
use Random\RandomException;
15+
use Symfony\Polyfill\Php82\NoDynamicProperties;
16+
1417
/**
1518
* @author Tim Düsterhus <[email protected]>
19+
* @author Anton Smirnov <[email protected]>
1620
*
1721
* @internal
1822
*/
1923
class Secure
2024
{
21-
public function __construct()
22-
{
23-
}
25+
use NoDynamicProperties;
2426

2527
public function generate(): string
2628
{
27-
return \random_bytes(\PHP_INT_SIZE);
29+
try {
30+
return random_bytes(\PHP_INT_SIZE);
31+
} catch (\Exception $e) {
32+
throw new RandomException($e->getMessage(), $e->getCode(), $e->getPrevious());
33+
}
2834
}
2935

3036
public function __sleep(): array
3137
{
32-
throw new \Exception("Serialization of 'Random\\Engine\\Secure' is not allowed");
38+
throw new \Exception("Serialization of 'Random\Engine\Secure' is not allowed");
3339
}
3440

3541
public function __wakeup(): void
3642
{
37-
throw new \Exception("Unserialization of 'Random\\Engine\\Secure' is not allowed");
43+
throw new \Exception("Unserialization of 'Random\Engine\Secure' is not allowed");
44+
}
45+
46+
public function __clone()
47+
{
48+
throw new \Error('Trying to clone an uncloneable object of class Random\Engine\Secure');
3849
}
3950
}

src/Php82/Resources/stubs/Random/BrokenRandomEngineError.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Random;
413

514
if (\PHP_VERSION_ID < 80200) {

src/Php82/Resources/stubs/Random/CryptoSafeEngine.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Random;
413

514
if (\PHP_VERSION_ID < 80200) {

src/Php82/Resources/stubs/Random/Engine.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Random;
413

514
if (\PHP_VERSION_ID < 80200) {

src/Php82/Resources/stubs/Random/Engine/Secure.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Random\Engine;
413

5-
use \Symfony\Polyfill\Php82 as p;
14+
use Symfony\Polyfill\Php82 as p;
615

716
if (\PHP_VERSION_ID < 80200) {
817
final class Secure extends p\Random\Engine\Secure implements \Random\CryptoSafeEngine
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
<?php
22

3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
312
namespace Random;
413

14+
use Symfony\Polyfill\Php82\NoDynamicProperties;
15+
516
if (\PHP_VERSION_ID < 80200) {
617
class RandomError extends \Error
718
{
19+
use NoDynamicProperties;
820
}
921
}

0 commit comments

Comments
 (0)