alecsammon/php-raml-parser
PHP library for parsing RAML (RESTful API Modeling Language) files into usable PHP structures. Helps load and interpret RAML API definitions for tooling, validation, or documentation workflows.
Installation Add the package via Composer:
composer require alecsammon/php-raml-parser
(Note: Due to abandonment, prefer raml-org/raml-php-parser for new projects.)
Basic Usage
Parse a RAML file (e.g., api.raml):
use AlesSammon\RamlParser\Parser;
$parser = new Parser();
$api = $parser->parseFile(__DIR__ . '/api.raml');
First Use Case: Extract API Metadata Access parsed data (e.g., title, versions, resources):
echo $api->getTitle(); // "My API"
$resources = $api->getResources();
Parse & Cache RAML Store parsed RAML in a service container for reuse:
$this->api = $parser->parseFile($ramlPath);
$this->api->getResources(); // Reused across controllers
Generate API Specs Dynamically Use parsed RAML to auto-generate OpenAPI/Swagger:
$openApi = convertRamlToOpenApi($this->api);
Validation Layer Validate incoming requests against RAML definitions:
$resource = $this->api->getResource('/users/{id}');
if (!$resource->isValidRequest($request)) {
abort(400, 'Invalid request format');
}
$this->app->singleton(Parser::class, function () {
return new Parser();
});
$this->call('raml:parse', ['file' => 'api.raml']);
Abandoned Package
raml-org/raml-php-parser.Strict RAML Compliance
Memory Usage
$parser->setDebug(true);
Custom Type Handlers Override default type handling (e.g., for proprietary formats):
$parser->addTypeHandler('custom-type', function ($value) {
return json_decode($value);
});
Post-Parse Hooks Modify parsed data via events:
$parser->on('parse.complete', function ($api) {
$api->setCustomMetadata(['author' => 'Your Team']);
});
Laravel Integration
How can I help you explore Laravel packages today?