Skip to content

Commit

Permalink
feat(runtime): introduce runtime component
Browse files Browse the repository at this point in the history
Signed-off-by: azjezz <azjezz@protonmail.com>
  • Loading branch information
azjezz committed Nov 7, 2021
1 parent 0435b4d commit d873819
Show file tree
Hide file tree
Showing 17 changed files with 355 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Psl/Internal/Loader.php
Expand Up @@ -460,6 +460,16 @@ final class Loader
'Psl\File\open_write_only',
'Psl\File\open_read_write',
'Psl\File\temporary',
'Psl\Runtime\get_extensions',
'Psl\Runtime\get_sapi',
'Psl\Runtime\get_version',
'Psl\Runtime\get_version_id',
'Psl\Runtime\get_version_details',
'Psl\Runtime\get_zend_version',
'Psl\Runtime\get_zend_extensions',
'Psl\Runtime\has_extension',
'Psl\Runtime\is_debug',
'Psl\Runtime\is_thread_safe',
];

public const INTERFACES = [
Expand Down
19 changes: 19 additions & 0 deletions src/Psl/Runtime/get_extensions.php
@@ -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();
}
20 changes: 20 additions & 0 deletions src/Psl/Runtime/get_sapi.php
@@ -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;
}
19 changes: 19 additions & 0 deletions src/Psl/Runtime/get_version.php
@@ -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;
}
28 changes: 28 additions & 0 deletions src/Psl/Runtime/get_version_details.php
@@ -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,
];
}
20 changes: 20 additions & 0 deletions src/Psl/Runtime/get_version_id.php
@@ -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;
}
20 changes: 20 additions & 0 deletions src/Psl/Runtime/get_zend_extensions.php
@@ -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);
}
24 changes: 24 additions & 0 deletions src/Psl/Runtime/get_zend_version.php
@@ -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();
}
19 changes: 19 additions & 0 deletions src/Psl/Runtime/has_extension.php
@@ -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);
}
17 changes: 17 additions & 0 deletions src/Psl/Runtime/is_debug.php
@@ -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;
}
17 changes: 17 additions & 0 deletions src/Psl/Runtime/is_thread_safe.php
@@ -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;
}
18 changes: 18 additions & 0 deletions tests/unit/Runtime/DebugTest.php
@@ -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());
}
}
24 changes: 24 additions & 0 deletions tests/unit/Runtime/ExtensionsTest.php
@@ -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'));
}
}
18 changes: 18 additions & 0 deletions tests/unit/Runtime/SapiTest.php
@@ -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());
}
}
18 changes: 18 additions & 0 deletions tests/unit/Runtime/ThreadSafetyTest.php
@@ -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());
}
}
46 changes: 46 additions & 0 deletions tests/unit/Runtime/VersionTest.php
@@ -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());
}
}
18 changes: 18 additions & 0 deletions tests/unit/Runtime/ZendTest.php
@@ -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());
}
}

0 comments on commit d873819

Please sign in to comment.