Skip to content

Commit

Permalink
refactor: phpcs and composer related updates (#21)
Browse files Browse the repository at this point in the history
Hey there,

I know this looks huge, but as far as I can see, there are no standards
in the PHP code. I thought I might add some standards to make the PHP
SDK more collaborative for everyone. If you think these changes are too
much, I wouldn't mind creating multiple PRs for a smoother transition.

---------

Co-authored-by: Muhammad Azeez <[email protected]>
  • Loading branch information
yigitcukuren and mhmd-azeez authored Jul 7, 2024
1 parent ac61e14 commit 443a762
Show file tree
Hide file tree
Showing 24 changed files with 387 additions and 308 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
src/ExtismLib.php
example/php_errors.log
php_errors.log
.phpunit.cache
.phpunit.result.cache
.php-cs-fixer.cache
.vscode
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,9 @@ prepare:

test: prepare
php vendor/bin/phpunit ./tests

cscheck:
vendor/bin/phpcs .

csfix:
vendor/bin/php-cs-fixer fix .
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ First you should add a using statement for Extism:
```php
use Extism\Plugin;
use Extism\Manifest;
use Extism\UrlWasmSource;
use Extism\Manifest\UrlWasmSource;
```

## Creating A Plug-in
Expand Down
16 changes: 9 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
"psr-4": {
"Extism\\": "src/"
},
"files": [
"src/Manifest.php",
"src/Plugin.php",
"src/CurrentPlugin.php"
]
"psr-0": {
"LibExtism": "src"
}
},
"autoload-dev": {
"psr-4": {}
"psr-4": {
"Extism\\Tests\\": "tests/"
}
},
"config": {
"sort-packages": true
Expand All @@ -49,6 +49,8 @@
"scripts": {},
"scripts-descriptions": {},
"require-dev": {
"phpunit/phpunit": "^9"
"friendsofphp/php-cs-fixer": "^3.59",
"phpunit/phpunit": "^9",
"squizlabs/php_codesniffer": "^3.10"
}
}
9 changes: 5 additions & 4 deletions example/index.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php
use Extism\UrlWasmSource;
use Extism\Manifest;

use Extism\Plugin;
use Extism\Manifest;
use Extism\Manifest\UrlWasmSource;

require_once __DIR__ . "/../src/Plugin.php";
require "../vendor/autoload.php";

$wasm = new UrlWasmSource("https://github.com/extism/plugins/releases/latest/download/count_vowels.wasm");
$manifest = new Manifest($wasm);
Expand All @@ -17,4 +18,4 @@

$plugin = new Plugin($manifest, true);
$output = $plugin->call("count_vowels", "Yellow, World!");
var_dump($output);
var_dump($output);
11 changes: 5 additions & 6 deletions example/memory_test.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
<?php
use Extism\PathWasmSource;
use Extism\UrlWasmSource;

use Extism\Manifest;
use Extism\Manifest\PathWasmSource;
use Extism\Plugin;
use Extism\HostFunction;
use Extism\ExtismValType;
use Extism\CurrentPlugin;

require_once __DIR__ . "/../src/Plugin.php";
require_once __DIR__ . "/../src/HostFunction.php";
require "../vendor/autoload.php";

$wasm = new PathWasmSource(__DIR__ . "/../wasm/count_vowels_kvstore.wasm");
$manifest = new Manifest($wasm);

for ($i = 0; $i < 10_000; $i++){
for ($i = 0; $i < 10_000; $i++) {
$kvstore = [];

$kvRead = new HostFunction("kv_read", [ExtismValType::I64], [ExtismValType::I64], function (CurrentPlugin $p, string $key) use (&$kvstore) {
Expand All @@ -32,4 +31,4 @@
}
}

readline();
readline();
8 changes: 8 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<ruleset name="extism-php-sdk">
<rule ref="PSR12">
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps"/>
<exclude name="Generic.Files.LineLength"/>
</rule>
<exclude-pattern>*/vendor/*</exclude-pattern>
</ruleset>
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd"
colors="true"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="unit">
<directory>tests/</directory>
</testsuite>
</testsuites>
</phpunit>
34 changes: 17 additions & 17 deletions src/CurrentPlugin.php
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
<?php

declare(strict_types=1);
namespace Extism;

require_once __DIR__ . "/LibExtism.php";
namespace Extism;

/**
* Represents a plugin that is calling the currently running host function.
*/
class CurrentPlugin
{
private \FFI\CData $handle;
private \LibExtism $lib;
private \Extism\Internal\LibExtism $lib;

/**
* constructor.
*
* @param \LibExtism $lib
*
* @param \Extism\Internal\LibExtism $lib
* @param \FFI\CData $handle
*/
function __construct($lib, \FFI\CData $handle)
public function __construct($lib, \FFI\CData $handle)
{
$this->handle = $handle;
$this->lib = $lib;
}

/**
* Reads a string from the plugin's memory at the given offset.
*
*
* @param int $offset Offset of the block to read.
*/
function read_block(int $offset) : string
public function read_block(int $offset): string
{
$ptr = $this->lib->extism_current_plugin_memory($this->handle);
$ptr = $this->lib->ffi->cast("char *", $ptr);
Expand All @@ -42,20 +42,20 @@ function read_block(int $offset) : string

/**
* Allocates a block of memory in the plugin's memory and returns the offset.
*
*
* @param int $size Size of the block to allocate in bytes.
*/
function allocate_block(int $size) : int
private function allocate_block(int $size): int
{
return $this->lib->extism_current_plugin_memory_alloc($this->handle, $size);
}

/**
* Writes a string to the plugin's memory, returning the offset of the block.
*
*
* @param string $data Buffer to write to the plugin's memory.
*/
function write_block(string $data) : int
public function write_block(string $data): int
{
$offset = $this->allocate_block(strlen($data));
$this->fill_block($offset, $data);
Expand All @@ -64,11 +64,11 @@ function write_block(string $data) : int

/**
* Fills a block of memory in the plugin's memory.
*
*
* @param int $offset Offset of the block to fill.
* @param string $data Buffer to fill the block with.
*/
function fill_block(int $offset, string $data) : void
private function fill_block(int $offset, string $data): void
{
$ptr = $this->lib->extism_current_plugin_memory($this->handle);
$ptr = $this->lib->ffi->cast("char *", $ptr);
Expand All @@ -79,11 +79,11 @@ function fill_block(int $offset, string $data) : void

/**
* Frees a block of memory in the plugin's memory.
*
*
* @param int $offset Offset of the block to free.
*/
function free_block(int $offset) : void
private function free_block(int $offset): void
{
$this->lib->extism_current_plugin_memory_free($this->handle, $offset);
}
}
}
15 changes: 15 additions & 0 deletions src/ExtismValType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Extism;

class ExtismValType
{
public const I32 = 0;
public const I64 = 1;
public const PTR = I64;
public const F32 = 2;
public const F64 = 3;
public const V128 = 4;
public const FUNC_REF = 5;
public const EXTERN_REF = 6;
}
35 changes: 16 additions & 19 deletions src/HostFunction.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
declare(strict_types=1);
namespace Extism;

require_once __DIR__ . "/LibExtism.php";
require_once __DIR__ . "/CurrentPlugin.php";
declare(strict_types=1);

namespace Extism;
class ExtismValType
{
public const I32 = 0;
Expand All @@ -29,23 +27,23 @@ class ExtismValType

class HostFunction
{
private \LibExtism $lib;
private \Extism\Internal\LibExtism $lib;
private $callback;

public \FFI\CData $handle;

/**
* Constructor
*
*
* @param string $name Name of the function
* @param array $inputTypes Array of input types. @see ExtismValType
* @param array $outputTypes Array of output types
* @param callable $callback Callback to invoke when the function is called
*
*
* @example ../tests/PluginTest.php 82 84 Simple Example
* @example ../tests/PluginTest.php 100 104 Manually read memory using CurrentPlugin
*/
function __construct(string $name, array $inputTypes, array $outputTypes, callable $callback)
public function __construct(string $name, array $inputTypes, array $outputTypes, callable $callback)
{
$reflection = new \ReflectionFunction($callback);
$arguments = $reflection->getParameters();
Expand All @@ -54,7 +52,7 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
global $lib;

if ($lib == null) {
$lib = new \LibExtism();
$lib = new \Extism\Internal\LibExtism();
}

$this->lib = $lib;
Expand All @@ -80,9 +78,9 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab

$r = $callback(...$params);

if ($r == NULL) {
if ($r == null) {
$r = 0;
} else if (gettype($r) == "string") {
} elseif (gettype($r) == "string") {
$r = $currentPlugin->write_block($r);
}

Expand All @@ -106,7 +104,6 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
throw new \Exception("Unsupported type for output: " . $output->t);
}
}

// Throwing an exception in FFI callback is not supported and
// causes a fatal error without a stack trace.
// So we catch it and print the exception manually
Expand All @@ -123,12 +120,12 @@ function __construct(string $name, array $inputTypes, array $outputTypes, callab
$this->set_namespace("extism:host/user");
}

function __destruct()
public function __destruct()
{
$this->lib->extism_function_free($this->handle);
}

function set_namespace(string $namespace)
public function set_namespace(string $namespace)
{
$this->lib->extism_function_set_namespace($this->handle, $namespace);
}
Expand Down Expand Up @@ -156,12 +153,12 @@ private static function get_type_name(\ReflectionParameter $param)
}

private static function get_parameters(
CurrentPlugin $currentPlugin,
CurrentPlugin $currentPlugin,
\FFI\CData $inputs,
int $n_inputs,
array $arguments,
int $offset) : array
{
array $arguments,
int $offset
): array {
$params = [];

if ($offset == 1) {
Expand Down Expand Up @@ -219,7 +216,7 @@ private static function validate_arguments(array $arguments, array $inputTypes)

if ($argType == null) {
continue;
} else if ($argType == "string") {
} elseif ($argType == "string") {
// string is represented as a pointer to a block of memory
$argType = "int";
}
Expand Down
Loading

0 comments on commit 443a762

Please sign in to comment.