|
| 1 | +# FFI IDE Helper Generator |
| 2 | + |
| 3 | +<p align="center"> |
| 4 | + <a href="https://packagist.org/packages/ffi/ide-helper-generator"><img src="https://poser.pugx.org/ffi/ide-helper-generator/require/php?style=for-the-badge" alt="PHP 8.1+"></a> |
| 5 | + <a href="https://packagist.org/packages/ffi/ide-helper-generator"><img src="https://poser.pugx.org/ffi/ide-helper-generator/version?style=for-the-badge" alt="Latest Stable Version"></a> |
| 6 | + <a href="https://packagist.org/packages/ffi/ide-helper-generator"><img src="https://poser.pugx.org/ffi/ide-helper-generator/v/unstable?style=for-the-badge" alt="Latest Unstable Version"></a> |
| 7 | + <a href="https://packagist.org/packages/ffi/ide-helper-generator"><img src="https://poser.pugx.org/ffi/ide-helper-generator/downloads?style=for-the-badge" alt="Total Downloads"></a> |
| 8 | + <a href="https://raw.githubusercontent.com/php-ffi/ide-helper-generator/master/LICENSE.md"><img src="https://poser.pugx.org/ffi/ide-helper-generator/license?style=for-the-badge" alt="License MIT"></a> |
| 9 | +</p> |
| 10 | +<p align="center"> |
| 11 | + <a href="https://github.com/php-ffi/ide-helper-generator/actions"><img src="https://github.com/php-ffi/ide-helper-generator/workflows/build/badge.svg"></a> |
| 12 | +</p> |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +- PHP ^8.1 |
| 17 | +- [castxml](https://github.com/CastXML/CastXML) ([binaries](https://github.com/CastXML/CastXMLSuperbuild/releases)) |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Library is available as composer repository and can be installed using the |
| 22 | +following command in a root of your project as dev-dependency. |
| 23 | + |
| 24 | +```sh |
| 25 | +$ composer require ffi/ide-helper-generator --dev |
| 26 | +``` |
| 27 | + |
| 28 | +## Usage |
| 29 | + |
| 30 | +### Generate Metadata |
| 31 | + |
| 32 | +Before generating the helper, the headers must be parsed to build the metadata |
| 33 | +data. To do this, `castxml` will be used, which in turn uses the original |
| 34 | +compiler (like `clang`) to build the AST. |
| 35 | + |
| 36 | +```php |
| 37 | +use FFI\Generator\Metadata\CastXMLGenerator; |
| 38 | + |
| 39 | +(new CastXMLGenerator( |
| 40 | + binary: 'castxml', // path to binary (optional) |
| 41 | + temp: 'storage', // path to temp directory (optional) |
| 42 | +)) |
| 43 | + ->generate('/path/to/headers.h') |
| 44 | + ->save('/path/to/metadata.xml') |
| 45 | +; |
| 46 | +``` |
| 47 | + |
| 48 | +You can also to optimize this step by adding a file existence check: |
| 49 | + |
| 50 | +```php |
| 51 | +if (!is_file('/path/to/metadata.xml')) { |
| 52 | + // Generate metadata: (new CastXMLGenerator())->... |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +### Analyze Metadata |
| 57 | + |
| 58 | +After the metadata is generated, it should be parsed and an abstract syntax tree |
| 59 | +built in memory. |
| 60 | + |
| 61 | +```php |
| 62 | +use FFI\Generator\Metadata\CastXMLParser; |
| 63 | + |
| 64 | +$ast = (new CastXMLParser()) |
| 65 | + ->parse('/path/to/metadata.xml') |
| 66 | +; |
| 67 | +``` |
| 68 | + |
| 69 | +### Building IDE Helper |
| 70 | + |
| 71 | +```php |
| 72 | +use FFI\Generator\PhpStormMetadataGenerator; |
| 73 | +use FFI\Generator\SimpleNamingStrategy; |
| 74 | + |
| 75 | +$generator = new PhpStormMetadataGenerator( |
| 76 | + argumentSetPrefix: 'ffi_', // Optional prefix for all argument sets to be registered |
| 77 | + // in metadata files. |
| 78 | + |
| 79 | + ignoreDirectories: ['/usr'], // Optional list of directories with headers whose types |
| 80 | + // should be excluded from the generated code. |
| 81 | + |
| 82 | + naming: new SimpleNamingStrategy( |
| 83 | + entrypoint: 'FFI\\Generated\\EntrypointInterface', // The name of the main FFI class |
| 84 | + // for which methods for autocomplete |
| 85 | + // will be generated. |
| 86 | + |
| 87 | + externalNamespace: 'FFI\\Generated', // Namespace for all public types (e.g. enums) that |
| 88 | + // can be used in PHP code. |
| 89 | + |
| 90 | + internalNamespace: 'PHPSTORM_META', // Namespace for all generated types which should not |
| 91 | + // be included in the PHP code and will only be used |
| 92 | + // for autocomplete. |
| 93 | + ), |
| 94 | +); |
| 95 | + |
| 96 | +// Pass AST into generator |
| 97 | +$result = $generator->generate($ast); |
| 98 | + |
| 99 | +// Write result code into stdout |
| 100 | +echo $result; |
| 101 | + |
| 102 | +file_put_contents(__DIR__ . '/.phpstorm.meta.php', (string)$result); |
| 103 | +``` |
| 104 | + |
| 105 | +You can also override some naming methods: |
| 106 | + |
| 107 | +```php |
| 108 | +use FFI\Generator\PhpStormMetadataGenerator; |
| 109 | +use FFI\Generator\SimpleNamingStrategy; |
| 110 | + |
| 111 | +$generator = new PhpStormMetadataGenerator( |
| 112 | + naming: new class extends SimpleNamingStrategy |
| 113 | + { |
| 114 | + // Each enum value will be converted to CamelCase |
| 115 | + // instead of UPPER_SNAKE_CASE (by default) |
| 116 | + protected function getEnumValueName(string $name): string |
| 117 | + { |
| 118 | + return $this->toCamelCase($name); |
| 119 | + } |
| 120 | + } |
| 121 | +); |
| 122 | +``` |
| 123 | + |
| 124 | +## Example |
| 125 | + |
| 126 | +Below is the simplest complex code example: |
| 127 | + |
| 128 | +```php |
| 129 | +use FFI\Generator\Metadata\CastXMLGenerator; |
| 130 | +use FFI\Generator\Metadata\CastXMLParser; |
| 131 | +use FFI\Generator\PhpStormMetadataGenerator; |
| 132 | +use FFI\Generator\SimpleNamingStrategy; |
| 133 | + |
| 134 | +const INPUT_HEADERS = __DIR__ . '/path/to/headers.h'; |
| 135 | +const OUTPUT_FILE = __DIR__ . '/path/to/.phpstorm.meta.php'; |
| 136 | + |
| 137 | +fwrite(STDOUT, " - [1/3] Generating metadata files\n"); |
| 138 | +if (!is_file(INPUT_HEADERS . '.xml')) { |
| 139 | + (new CastXMLGenerator()) |
| 140 | + ->generate(INPUT_HEADERS) |
| 141 | + ->save(INPUT_HEADERS . '.xml') |
| 142 | + ; |
| 143 | +} |
| 144 | + |
| 145 | +fwrite(STDOUT, " - [2/3] Building AST\n"); |
| 146 | +$ast = (new CastXMLParser()) |
| 147 | + ->parse(INPUT_HEADERS . '.xml') |
| 148 | +; |
| 149 | + |
| 150 | +fwrite(STDOUT, " - [3/3] Generating IDE helper\n"); |
| 151 | +$result = (new PhpStormMetadataGenerator()) |
| 152 | + ->generate($ast) |
| 153 | +; |
| 154 | + |
| 155 | +fwrite(STDOUT, " - DONE!\n"); |
| 156 | +file_put_contents(OUTPUT_FILE, (string)$result); |
| 157 | +``` |
0 commit comments