Skip to content

Halleck45/go-php-parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-PHP-Parser

Go-PHP-Parser

CI GitHub Release

Go library plug-and-play to parse PHP code through the embed SAPI + ext-ast, returning the AST as JSON.
No PHP installation required: prebuilt binaries are automatically fetched on first use.

Sponsor me!





Installation

go get github.com/Halleck45/go-php-parser

Go 1.20+ recommended. CGO_ENABLED=1 must be active (default on macOS/Linux).

Supported platforms

  • Linux: linux_amd64
  • macOS: darwin_amd64, darwin_arm64

Usage

package main

import (
    "fmt"
    "github.com/Halleck45/go-php-parser"
)

func main() {
    defer PhpParser.Shutdown()

    // Inline code
    json, ok := PhpParser.ParseCode("<?php function foo(int $a){return $a+1;}", "inline.php", 0, 0)
    if !ok {
        panic("parse failed")
    }
    fmt.Println(json)

    // File
    json2, ok := PhpParser.ParseFile("example.php", 0, 0)
    if !ok {
        panic("parse file failed")
    }
    fmt.Println(json2)
}

Performance

The parser processes 4,000–8,000 PHP files per second on a standard development machine (multi-core CPU, NVMe SSD).

Parameters

For advanced usage, you can pass the following parameters:

  • astVersion: 0current version of ext-ast (recommended).
  • flags: 0 (default).

The lib returns JSON with at least:

  • root: AST array (via ast\dump(..., AST_DUMP_ARRAY))
  • version: metadata (PHP, ext-ast, ast_version, engine)
  • file (if provided)

License

MIT (see LICENSE).

About

A C library (and standalone binary) for parsing AST of PHP code

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published