tourze / php-packer-ast
AST管理和解析工具
0.0.1
2025-04-11 06:00 UTC
Requires
- php: ^8.1
- nikic/php-parser: ^5.3.0
- psr/log: ^3|^2|^1
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-04-25 17:38:29 UTC
README
Introduction
PHP Packer AST is a library for parsing, managing, and traversing PHP Abstract Syntax Trees (AST). It provides a simple and extensible API to parse PHP code, manage ASTs, and traverse nodes using the visitor pattern.
Features
- Parse PHP code and files into AST
- Efficient AST manager for multiple files
- Visitor pattern support for flexible AST traversal
- Simple error handling mechanism
- Compatible with PHP 8.1 and above
Installation
Require via Composer:
composer require tourze/php-packer-ast
Quick Start
Parse PHP Code
use PhpPacker\Ast\CodeParser; $parser = new CodeParser(); $ast = $parser->parseCode('<?php echo "Hello World"; ?>'); // Or parse from file $ast = $parser->parseFile('/path/to/your/file.php');
Use AST Manager
use PhpPacker\Ast\AstManager; use Psr\Log\NullLogger; $manager = new AstManager(new NullLogger()); // Add AST $manager->addAst('/path/to/file.php', $ast); // Get AST $ast = $manager->getAst('/path/to/file.php'); // Check existence if ($manager->hasAst('/path/to/file.php')) { // ... } // Get statistics $fileCount = $manager->getFileCount(); $nodeCount = $manager->getTotalNodeCount();
Use Visitor Pattern
use PhpPacker\Ast\Visitor\RenameDebugInfoVisitor; use PhpParser\NodeTraverser; $traverser = new NodeTraverser(); $traverser->addVisitor(new RenameDebugInfoVisitor()); $modifiedAst = $traverser->traverse($ast);
Documentation
- API documentation: See source code and tests
- Configuration: No additional configuration required
- Advanced: Implement custom visitors by extending
AbstractVisitor
Contribution Guide
- Fork and clone this repository
- Create a new branch for your feature or bugfix
- Write code and add tests
- Run tests with PHPUnit
- Submit a Pull Request
- Please follow PSR coding standards
- Ensure all tests pass before submitting
License
MIT License. See LICENSE for details.
Authors
- tourze Team
Changelog
See CHANGELOG for release notes and upgrade guide.