Move php8.php to /app/libraries/polyfill/

This commit is contained in:
the-djmaze 2024-02-25 20:13:39 +01:00
parent 5262e59fdd
commit c6aad25581
3 changed files with 77 additions and 38 deletions

View file

@ -0,0 +1,76 @@
<?php
if (\PHP_VERSION_ID < 80000)
{
interface Stringable
{
public function __toString() : string;
}
if (!class_exists('ValueError')) {
class ValueError extends Error
{
}
}
if (!function_exists('str_contains')) {
function str_contains(string $haystack, string $needle) : bool
{
return false !== strpos($haystack, $needle);
}
}
if (!function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle) : bool
{
return 0 === strncmp($haystack, $needle, strlen($needle));
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle) : bool
{
$length = strlen($needle);
return $length ? substr($haystack, -$length) === $needle : true;
}
}
if (!function_exists('get_debug_type')) {
function get_debug_type($value): string
{
$type = gettype($value);
switch ($type) {
case 'NULL': return 'null';
case 'boolean': return 'bool';
case 'double': return 'float';
case 'integer': return 'int';
case 'object':
// if ($value instanceof __PHP_Incomplete_Class) return '__PHP_Incomplete_Class';
$class = get_class($value);
if (false === strpos($class, '@')) {
return $class;
}
$parent = get_parent_class($class);
if ($parent) {
return $parent . '@anonymous';
}
$implements = class_implements($class);
if ($implements) {
return key($implements) . '@anonymous';
}
return 'class@anonymous';
case 'resource':
$type = get_resource_type($value);
if (null === $type) {
return 'unknown';
}
if ('Unknown' === $type) {
$type = 'closed';
}
return "resource ($type)";
default:
return $type;
}
}
}
}

View file

@ -5,7 +5,7 @@ if (defined('APP_VERSION_ROOT_PATH')) {
// PHP 8
if (\PHP_VERSION_ID < 80000) {
require __DIR__ . '/php8.php';
require __DIR__ . '/app/libraries/polyfill/php8.php';
}
if (!defined('APP_VERSION')) {

View file

@ -1,37 +0,0 @@
<?php
if (\PHP_VERSION_ID < 80000)
{
interface Stringable
{
public function __toString() : string;
}
if (!class_exists('ValueError')) {
class ValueError extends Error
{
}
}
if (!function_exists('str_contains')) {
function str_contains(string $haystack, string $needle) : bool
{
return false !== strpos($haystack, $needle);
}
}
if (!function_exists('str_starts_with')) {
function str_starts_with(string $haystack, string $needle) : bool
{
return 0 === strncmp($haystack, $needle, strlen($needle));
}
}
if (!function_exists('str_ends_with')) {
function str_ends_with(string $haystack, string $needle) : bool
{
$length = strlen($needle);
return $length ? substr($haystack, -$length) === $needle : true;
}
}
}