!!! This package is deprecated, please use https://github.com/php-ffi/ide-helper-generator instead
CastXML is a C-family abstract syntax tree XML output tool.
This project is maintained by Kitware in support of ITK, the Insight Segmentation and Registration Toolkit.
Library can be installed into any PHP application using Composer dependency manager.
composer require serafim/castxml
In order to access library make sure to include vendor/autoload.php
in your file.
<?php
require __DIR__ . '/vendor/autoload.php';
Don't forget to install the binary dependency. If you are looking for pre-built binaries, or a compact way to build this project, please see CastXMLSuperbuild.
Also, some operating systems support installation using dependency managers, for example:
$ sudo apt update
$ apt install castxml
To create a new CastXML, you can use one of the possible options.
$castxml = new \Serafim\CastXml\CastXml();
// OR
$binary = '/path/to/binary/castxml';
$castxml = new \Serafim\CastXml\CastXml($binary);
Windows OS is also supported, just write the path to CastXML in PATH variable or add the path to
castxml.exe
to the class constructor.
For information on whether everything is OK, simply use the accessibility
isAvailable()
method.
$castxml = new \Serafim\CastXml\CastXml();
$available = $castxml->isAvailable();
// - bool(true) - CastXML is available
// - bool(false) - CastXML is not available
For version information use methods getVersion()
and getClangVersion()
.
$castxml = new \Serafim\CastXml\CastXml();
echo $castxml->getVersion();
// For example: "0.4.2"
echo $castxml->getClangVersion();
// For example: "11.0.0"
To parse the original header, just use the parse()
method. The method takes
one required $pathname
and one optional $cwd
string arguments.
$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');
// OR
$result = $castxml->parse('/path/to/header.h', '/path/to/working_directory');
As a result, you will get a Serafim\CastXml\Result
object.
$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');
echo $result->getContents(); // XML output
// OR
echo $result; // Same XML output
$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');
// Save to file
$result->saveAs('/path/to/filename.xml');
// Save into directory
$result->saveIn('/path/to/directory');
$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');
// Using SimpleXML
// - ext-simplexml required
$simplexml = $result->toXml();
// Using XML Reader
// - ext-xmlreader required
$reader = $result->toXmlReader();
// Using DOMDocument
// - ext-dom required
$ast = $result->toDomDocument();
$castxml = new \Serafim\CastXml\CastXml();
$result = $castxml->parse('/path/to/header.h');
foreach ($result as $type) {
var_dump($type);
}