Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
sorinsarca committed Dec 27, 2024
1 parent 3c9e749 commit 9fbec94
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 29 deletions.
42 changes: 38 additions & 4 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Opis\Closure;

use Opis\Closure\Security\SecurityProviderInterface;

/**
* @param Security\SecurityProviderInterface|string|null $security The security provider used to sign data
* @param bool $v3Compatible True if you need v3 compatibility at deserialization
Expand All @@ -29,7 +27,7 @@ function set_security_provider(Security\SecurityProviderInterface|string|null $s

/**
* Get current security provider
* @return SecurityProviderInterface|null
* @return Security\SecurityProviderInterface|null
*/
function get_security_provider(): ?Security\SecurityProviderInterface
{
Expand Down Expand Up @@ -91,7 +89,7 @@ function serialize(
* Unserialize data
* @param string $data Data to be deserialized
* @param Security\SecurityProviderInterface|array|null $security The security provider used to check the signature
* @param array|null $options Options for unserialization
* @param array|null $options Options for \unserialize()
* @return mixed The deserialized data
* @see https://www.php.net/manual/en/function.unserialize.php
*/
Expand All @@ -108,3 +106,39 @@ function unserialize(
return Serializer::unserialize($data, $security, $options);
}

/**
* Unserialize data from v3
* @param string $data The data to deserialize
* @param Security\SecurityProviderInterface|null $security Security provider used to check the signature
* @param array|null $options Options for \unserialize()
* @return mixed The deserialized data
*/
function v3_unserialize(
string $data,
?Security\SecurityProviderInterface $security = null,
?array $options = null
): mixed
{
return Serializer::v3_unserialize($data, $security, $options);
}

/**
* Helper method used to replace the deprecated create_function() from PHP
* @param string $args Comma separated list of function arguments
* @param string $body Function code
* @return \Closure
* @see https://www.php.net/manual/en/function.create-function.php
*/
function create_closure(string $args, string $body): \Closure
{
Serializer::init();

$header = "/* created with " . __FUNCTION__ . "() */";
$body = "static function (" . $args . ") {\n" . $body . "\n}";

if (!($info = ClosureInfo::resolve(ClosureInfo::createKey($header, $body)))) {
$info = new ClosureInfo($header, $body, null, ClosureInfo::FLAG_IS_STATIC);
}

return ($info->getFactory(null))();
}
28 changes: 3 additions & 25 deletions src/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Opis\Closure;

use Closure, UnitEnum;
use UnitEnum;
use Opis\Closure\Security\{
DefaultSecurityProvider,
SecurityProviderInterface,
Expand Down Expand Up @@ -165,12 +165,11 @@ public static function unserialize(string $data, ?SecurityProviderInterface $sec
}

/**
* Unserialize data from v3.x using a security provider (optional)
* Unserialize data from v3 using a security provider (optional)
* DO NOT use this to unserialize data from v4
* This method was created in order to help with migration from v3 to v4
* @throws SecurityException
*/
public static function unserialize_v3(string $data, ?SecurityProviderInterface $security = null, ?array $options = null): mixed
public static function v3_unserialize(string $data, ?SecurityProviderInterface $security = null, ?array $options = null): mixed
{
self::$init || self::init();

Expand Down Expand Up @@ -272,27 +271,6 @@ public static function register(string $class, ?callable $serialize, ?callable $
$data->unserialize = $unserialize;
}

/**
* Helper method used to replace the deprecated create_function() from PHP
* @param string $args
* @param string $body
* @return Closure
*/
public static function createClosure(string $args, string $body): Closure
{
// make sure we are registered
self::$init || self::init();

$header = "/* created with " . __METHOD__ . "() */";
$body = "static function (" . $args . ") {\n" . $body . "\n}";

if (!($info = ClosureInfo::resolve(ClosureInfo::createKey($header, $body)))) {
$info = new ClosureInfo($header, $body, null, ClosureInfo::FLAG_IS_STATIC);
}

return ($info->getFactory(null))();
}

/**
* Set current security provider
*/
Expand Down

0 comments on commit 9fbec94

Please sign in to comment.