Skip to content

Commit 6da14d7

Browse files
committed
Initial commit
0 parents  commit 6da14d7

16 files changed

+482
-0
lines changed

.editorconfig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.yml]
15+
indent_style = space
16+
indent_size = 4

.gitattributes

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
* text=auto
2+
3+
/.github export-ignore
4+
/tests export-ignore
5+
6+
.editorconfig export-ignore
7+
.gitattributes export-ignore
8+
.gitignore export-ignore
9+
.styleci.yml export-ignore
10+
.travis.yml export-ignore
11+
12+
monorepo-builder.php export-ignore
13+
phpunit.xml export-ignore

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.idea/
2+
vendor/
3+
4+
# Local testing
5+
include/
6+
test.php
7+
8+
composer.lock
9+
.phpunit.result.cache

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright © Kirill Nesmeyanov
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# FFI Proxy
2+
3+
A set of classes for creating FFI proxies.
4+
5+
## Requirements
6+
7+
- PHP >= 7.4
8+
- ext-ffi
9+
10+
## Installation
11+
12+
Library is available as composer repository and can be installed using the
13+
following command in a root of your project.
14+
15+
```sh
16+
$ composer require ffi/proxy
17+
```
18+
19+
## Usage
20+
21+
```php
22+
class Example extends FFI\Proxy\Proxy
23+
{
24+
public function __construct()
25+
{
26+
$this->ffi = \FFI::cdef('...');
27+
}
28+
}
29+
```

composer.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "ffi/proxy",
3+
"description": "PHP FFI Proxy",
4+
"license": "MIT",
5+
"keywords": ["ffi", "proxy"],
6+
"require": {
7+
"php": ">=7.4",
8+
"ext-ffi": "*"
9+
},
10+
"autoload": {
11+
"psr-4": {
12+
"FFI\\Proxy\\": "src"
13+
}
14+
},
15+
"require-dev": {
16+
"vimeo/psalm": "^4.9",
17+
"jetbrains/phpstorm-attributes": "^1.0",
18+
"phpunit/phpunit": "^9.0"
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"FFI\\Proxy\\Tests\\": "tests"
23+
}
24+
},
25+
"config": {
26+
"sort-packages": true
27+
},
28+
"extra": {
29+
"branch-alias": {
30+
"dev-master": "1.0.x-dev"
31+
}
32+
},
33+
"minimum-stability": "dev",
34+
"prefer-stable": true
35+
}

phpunit.xml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
4+
colors="true"
5+
backupGlobals="true"
6+
stopOnFailure="false"
7+
processIsolation="false"
8+
backupStaticAttributes="false"
9+
bootstrap="vendor/autoload.php"
10+
convertErrorsToExceptions="true"
11+
convertNoticesToExceptions="true"
12+
convertWarningsToExceptions="true"
13+
>
14+
<coverage>
15+
<include>
16+
<directory suffix=".php">src</directory>
17+
</include>
18+
</coverage>
19+
<testsuites>
20+
<testsuite name="Test Suite">
21+
<directory suffix="TestCase.php">tests</directory>
22+
</testsuite>
23+
</testsuites>
24+
<php>
25+
<ini name="error_reporting" value="-1"/>
26+
<ini name="memory_limit" value="-1"/>
27+
</php>
28+
</phpunit>

psalm.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<psalm
3+
errorLevel="1"
4+
resolveFromConfigFile="true"
5+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
6+
xmlns="https://getpsalm.org/schema/config"
7+
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
8+
xmlns:xi="http://www.w3.org/2001/XInclude"
9+
>
10+
<projectFiles>
11+
<directory name="src" />
12+
<ignoreFiles>
13+
<directory name="vendor" />
14+
</ignoreFiles>
15+
</projectFiles>
16+
</psalm>

src/ApiAwareTrait.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/**
4+
* This file is part of FFI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace FFI\Proxy;
13+
14+
use FFI\CData;
15+
use FFI\CType;
16+
use FFI\ParserException;
17+
18+
/**
19+
* @property-read \FFI $ffi
20+
* @mixin ApiInterface
21+
* @psalm-require-implements ApiInterface
22+
*/
23+
trait ApiAwareTrait
24+
{
25+
/**
26+
* @see ApiInterface::new()
27+
* @throws ParserException
28+
*
29+
* @psalm-param string|CType $type
30+
* @psalm-param bool $owned
31+
* @psalm-param bool $persistent
32+
* @psalm-return CData|null
33+
*/
34+
public function new($type, bool $owned = true, bool $persistent = false): ?CData
35+
{
36+
return $this->ffi->new($type, $owned, $persistent);
37+
}
38+
39+
/**
40+
* @see ApiInterface::cast()
41+
*
42+
* @psalm-param CType|string $type
43+
* @psalm-param CData|int|float|bool|null $ptr
44+
* @psalm-return CData|null
45+
*/
46+
public function cast($type, $ptr): ?CData
47+
{
48+
return $this->ffi->cast($type, $ptr);
49+
}
50+
51+
/**
52+
* @see ApiInterface::type()
53+
*
54+
* @psalm-param string $type
55+
* @psalm-return CType|null
56+
*/
57+
public function type(string $type): ?CType
58+
{
59+
return $this->ffi->type($type);
60+
}
61+
}

src/ApiInterface.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
/**
4+
* This file is part of FFI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace FFI\Proxy;
13+
14+
use FFI\CData;
15+
use FFI\CType;
16+
use FFI\ParserException;
17+
18+
interface ApiInterface
19+
{
20+
/**
21+
* @see \FFI::new()
22+
*
23+
* @param string|CType $type
24+
* @param bool $owned
25+
* @param bool $persistent
26+
* @return CData|null
27+
* @throws ParserException
28+
*/
29+
public function new($type, bool $owned = true, bool $persistent = false): ?CData;
30+
31+
/**
32+
* @see \FFI::cast()
33+
*
34+
* @param CType|string $type
35+
* @param CData|int|float|bool|null $ptr
36+
* @return CData|null
37+
*/
38+
public function cast($type, $ptr): ?CData;
39+
40+
/**
41+
* @see \FFI::type()
42+
*
43+
* @param string $type
44+
* @return CType|null
45+
*/
46+
public function type(string $type): ?CType;
47+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* This file is part of FFI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace FFI\Proxy\Exception;
13+
14+
class NotAvailableException extends ProxyException
15+
{
16+
}

src/Exception/ProxyException.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* This file is part of FFI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace FFI\Proxy\Exception;
13+
14+
class ProxyException extends \Exception
15+
{
16+
}

src/Proxy.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/**
4+
* This file is part of FFI package.
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace FFI\Proxy;
13+
14+
use FFI\Proxy\Exception\NotAvailableException;
15+
16+
abstract class Proxy implements ProxyInterface, ApiInterface
17+
{
18+
use ProxyAwareTrait;
19+
use ApiAwareTrait;
20+
21+
/**
22+
* @psalm-allow-private-mutation
23+
* @psalm-readonly
24+
* @var \FFI
25+
*/
26+
public \FFI $ffi;
27+
28+
/**
29+
* Proxy should not be serializable.
30+
*/
31+
public function __sleep(): array
32+
{
33+
throw new NotAvailableException('Cannot serialize proxy class');
34+
}
35+
36+
/**
37+
* Proxy should not be restorable from strings.
38+
*/
39+
public function __wakeup()
40+
{
41+
throw new NotAvailableException('Cannot unserialize proxy class');
42+
}
43+
44+
/**
45+
* Proxy should not be serializable.
46+
*/
47+
public function __serialize(): array
48+
{
49+
throw new NotAvailableException('Cannot serialize proxy class');
50+
}
51+
52+
/**
53+
* Proxy should not be restorable from strings.
54+
*/
55+
public function __unserialize(array $data): void
56+
{
57+
throw new NotAvailableException('Cannot unserialize proxy class');
58+
}
59+
60+
/**
61+
* Proxy should not be cloneable.
62+
*/
63+
public function __clone()
64+
{
65+
throw new NotAvailableException('Cannot clone proxy class');
66+
}
67+
}

0 commit comments

Comments
 (0)