2021-02-26 16:16:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Safe;
|
|
|
|
|
|
|
|
use Safe\Exceptions\SplException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns an array with the names of the interfaces that the
|
2022-07-12 19:26:21 +00:00
|
|
|
* given object_or_class and its parents implement.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param object|string $object_or_class An object (class instance) or a string (class or interface name).
|
|
|
|
* @param bool $autoload Whether to call __autoload by default.
|
|
|
|
* @return array An array on success, or FALSE when the given class doesn't exist.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws SplException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function class_implements($object_or_class, bool $autoload = true): array
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \class_implements($object_or_class, $autoload);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw SplException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns an array with the name of the parent classes of
|
2022-07-12 19:26:21 +00:00
|
|
|
* the given object_or_class.
|
2021-02-26 16:16:17 +00:00
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param object|string $object_or_class An object (class instance) or a string (class name).
|
|
|
|
* @param bool $autoload Whether to call __autoload by default.
|
|
|
|
* @return array An array on success, or FALSE when the given class doesn't exist.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws SplException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function class_parents($object_or_class, bool $autoload = true): array
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \class_parents($object_or_class, $autoload);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw SplException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This function returns an array with the names of the traits that the
|
2022-07-12 19:26:21 +00:00
|
|
|
* given object_or_class uses. This does however not include
|
2021-02-26 16:16:17 +00:00
|
|
|
* any traits used by a parent class.
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param object|string $object_or_class An object (class instance) or a string (class name).
|
|
|
|
* @param bool $autoload Whether to call __autoload by default.
|
|
|
|
* @return array An array on success, or FALSE when the given class doesn't exist.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws SplException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function class_uses($object_or_class, bool $autoload = true): array
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \class_uses($object_or_class, $autoload);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw SplException::createFromPhpError();
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a function with the spl provided __autoload queue. If the queue
|
|
|
|
* is not yet activated it will be activated.
|
|
|
|
*
|
|
|
|
* If your code has an existing __autoload function then
|
|
|
|
* this function must be explicitly registered on the __autoload queue. This
|
|
|
|
* is because spl_autoload_register will effectively
|
|
|
|
* replace the engine cache for the __autoload function
|
|
|
|
* by either spl_autoload or
|
|
|
|
* spl_autoload_call.
|
|
|
|
*
|
|
|
|
* If there must be multiple autoload functions, spl_autoload_register
|
|
|
|
* allows for this. It effectively creates a queue of autoload functions, and
|
|
|
|
* runs through each of them in the order they are defined. By contrast,
|
|
|
|
* __autoload may only be defined once.
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param callable(string):void $callback The autoload function being registered.
|
|
|
|
* If NULL, then the default implementation of
|
2021-02-26 16:16:17 +00:00
|
|
|
* spl_autoload will be registered.
|
|
|
|
* @param bool $throw This parameter specifies whether
|
|
|
|
* spl_autoload_register should throw
|
2022-07-12 19:26:21 +00:00
|
|
|
* exceptions when the callback
|
2021-02-26 16:16:17 +00:00
|
|
|
* cannot be registered.
|
|
|
|
* @param bool $prepend If true, spl_autoload_register will prepend
|
|
|
|
* the autoloader on the autoload queue instead of appending it.
|
|
|
|
* @throws SplException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function spl_autoload_register(callable $callback = null, bool $throw = true, bool $prepend = false): void
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
|
|
|
if ($prepend !== false) {
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \spl_autoload_register($callback, $throw, $prepend);
|
2021-02-26 16:16:17 +00:00
|
|
|
} elseif ($throw !== true) {
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \spl_autoload_register($callback, $throw);
|
|
|
|
} elseif ($callback !== null) {
|
|
|
|
$result = \spl_autoload_register($callback);
|
2021-02-26 16:16:17 +00:00
|
|
|
} else {
|
|
|
|
$result = \spl_autoload_register();
|
|
|
|
}
|
|
|
|
if ($result === false) {
|
|
|
|
throw SplException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a function from the autoload queue. If the queue
|
|
|
|
* is activated and empty after removing the given function then it will
|
|
|
|
* be deactivated.
|
|
|
|
*
|
|
|
|
* When this function results in the queue being deactivated, any
|
|
|
|
* __autoload function that previously existed will not be reactivated.
|
|
|
|
*
|
2022-07-12 19:26:21 +00:00
|
|
|
* @param mixed $callback The autoload function being unregistered.
|
2021-02-26 16:16:17 +00:00
|
|
|
* @throws SplException
|
|
|
|
*
|
|
|
|
*/
|
2022-07-12 19:26:21 +00:00
|
|
|
function spl_autoload_unregister($callback): void
|
2021-02-26 16:16:17 +00:00
|
|
|
{
|
|
|
|
error_clear_last();
|
2022-07-12 19:26:21 +00:00
|
|
|
$result = \spl_autoload_unregister($callback);
|
2021-02-26 16:16:17 +00:00
|
|
|
if ($result === false) {
|
|
|
|
throw SplException::createFromPhpError();
|
|
|
|
}
|
|
|
|
}
|