Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(runtime): introduce runtime component
Signed-off-by: azjezz <azjezz@protonmail.com>
- Loading branch information
Showing
17 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
/** | ||
* Returns an list with the names of all extensions compiled and loaded. | ||
* | ||
* @return non-empty-list<non-empty-string> | ||
*/ | ||
function get_extensions(): array | ||
{ | ||
// we know that this cannot be empty, since some extensions cannot be disabled ( e.g: Core ) | ||
/** | ||
* @var non-empty-list<non-empty-string> | ||
*/ | ||
return get_loaded_extensions(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use const PHP_SAPI; | ||
|
||
/** | ||
* Return the Server API for this build of PHP. | ||
* | ||
* @return non-empty-string | ||
* | ||
* @pure | ||
*/ | ||
function get_sapi(): string | ||
{ | ||
/** @var non-empty-string */ | ||
return PHP_SAPI; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use const PHP_VERSION; | ||
|
||
/** | ||
* Return the current PHP version as a string in "major.minor.release[extra]" notation. | ||
* | ||
* @return non-empty-string | ||
* | ||
* @pure | ||
*/ | ||
function get_version(): string | ||
{ | ||
return PHP_VERSION; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use const PHP_EXTRA_VERSION; | ||
use const PHP_MAJOR_VERSION; | ||
use const PHP_MINOR_VERSION; | ||
use const PHP_RELEASE_VERSION; | ||
|
||
/** | ||
* Return the current PHP version as an array. | ||
* | ||
* @return array{major: non-empty-string, minor: non-empty-string, release: non-empty-string, extra: non-empty-string|null} | ||
* | ||
* @pure | ||
*/ | ||
function get_version_details(): array | ||
{ | ||
/** @var array{major: non-empty-string, minor: non-empty-string, release: non-empty-string, extra: non-empty-string|null} */ | ||
return [ | ||
'major' => PHP_MAJOR_VERSION, | ||
'minor' => PHP_MINOR_VERSION, | ||
'release' => PHP_RELEASE_VERSION, | ||
'extra' => PHP_EXTRA_VERSION ?: null, | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* Return the current PHP version as an integer, useful for version comparisons (e.g., int(80100) from version "8.1.0-RC5"). | ||
* | ||
* @return positive-int | ||
* | ||
* @pure | ||
*/ | ||
function get_version_id(): int | ||
{ | ||
/** @var positive-int */ | ||
return PHP_VERSION_ID; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
/** | ||
* Returns an list with the names of all Zend extensions compiled and loaded. | ||
* | ||
* @return list<non-empty-string> | ||
* | ||
* @pure | ||
*/ | ||
function get_zend_extensions(): array | ||
{ | ||
/** | ||
* @var list<non-empty-string> | ||
*/ | ||
return get_loaded_extensions(true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use function zend_version; | ||
|
||
/** | ||
* Return the version of the current Zend engine. | ||
* | ||
* @return non-empty-string | ||
* | ||
* @pure | ||
*/ | ||
function get_zend_version(): string | ||
{ | ||
/** | ||
* @psalm-suppress ImpureFunctionCall | ||
* | ||
* @var non-empty-string | ||
*/ | ||
return zend_version(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use function extension_loaded; | ||
|
||
/** | ||
* Find out whether an $extension is loaded. | ||
* | ||
* @param non-empty-string $extension | ||
* | ||
* @pure | ||
*/ | ||
function has_extension(string $extension): bool | ||
{ | ||
return extension_loaded($extension); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use const PHP_DEBUG; | ||
|
||
/** | ||
* Return true if PHP was built with debugging enabled. | ||
* | ||
* @pure | ||
*/ | ||
function is_debug(): bool | ||
{ | ||
return 1 === PHP_DEBUG; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Runtime; | ||
|
||
use const PHP_ZTS; | ||
|
||
/** | ||
* Return true if PHP was built with ZTS enabled. | ||
* | ||
* @pure | ||
*/ | ||
function is_thread_safe(): bool | ||
{ | ||
return 1 === PHP_ZTS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Runtime; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Runtime; | ||
|
||
use const PHP_DEBUG; | ||
|
||
final class DebugTest extends TestCase | ||
{ | ||
public function testIsDebug(): void | ||
{ | ||
static::assertSame(PHP_DEBUG === 1, Runtime\is_debug()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Runtime; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Runtime; | ||
|
||
final class ExtensionsTest extends TestCase | ||
{ | ||
public function testExtensions(): void | ||
{ | ||
foreach (Runtime\get_extensions() as $extension) { | ||
static::assertTrue(Runtime\has_extension($extension)); | ||
} | ||
|
||
foreach (Runtime\get_zend_extensions() as $zend_extension) { | ||
static::assertTrue(Runtime\has_extension($zend_extension)); | ||
} | ||
|
||
static::assertFalse(Runtime\has_extension('php-standard-library')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Runtime; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Runtime; | ||
|
||
use const PHP_SAPI; | ||
|
||
final class SapiTest extends TestCase | ||
{ | ||
public function testSapi(): void | ||
{ | ||
static::assertSame(PHP_SAPI, Runtime\get_sapi()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Runtime; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Runtime; | ||
|
||
use const PHP_ZTS; | ||
|
||
final class ThreadSafetyTest extends TestCase | ||
{ | ||
public function testIsThreadSafe(): void | ||
{ | ||
static::assertSame(PHP_ZTS === 1, Runtime\is_thread_safe()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Runtime; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Runtime; | ||
|
||
use const PHP_EXTRA_VERSION; | ||
use const PHP_MAJOR_VERSION; | ||
use const PHP_MINOR_VERSION; | ||
use const PHP_RELEASE_VERSION; | ||
use const PHP_VERSION; | ||
use const PHP_VERSION_ID; | ||
|
||
final class VersionTest extends TestCase | ||
{ | ||
public function testGetVersionDetails(): void | ||
{ | ||
$version_details = Runtime\get_version_details(); | ||
|
||
static::assertCount(4, $version_details); | ||
static::assertArrayHasKey('major', $version_details); | ||
static::assertArrayHasKey('minor', $version_details); | ||
static::assertArrayHasKey('release', $version_details); | ||
static::assertArrayHasKey('extra', $version_details); | ||
|
||
static::assertSame([ | ||
'major' => PHP_MAJOR_VERSION, | ||
'minor' => PHP_MINOR_VERSION, | ||
'release' => PHP_RELEASE_VERSION, | ||
'extra' => PHP_EXTRA_VERSION ?: null, | ||
], $version_details); | ||
} | ||
|
||
public function testGetVersionId(): void | ||
{ | ||
static::assertSame(PHP_VERSION_ID, Runtime\get_version_id()); | ||
} | ||
|
||
public function testGetVersion(): void | ||
{ | ||
static::assertSame(PHP_VERSION, Runtime\get_version()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Psl\Tests\Unit\Runtime; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psl\Runtime; | ||
|
||
use function zend_version; | ||
|
||
final class ZendTest extends TestCase | ||
{ | ||
public function testGetZendVersion(): void | ||
{ | ||
static::assertSame(zend_version(), Runtime\get_zend_version()); | ||
} | ||
} |