A service for parsing PHP code and extracting structured information about classes, methods, and imports.
PHP Parser is a service that uses the nikic/php-parser library to analyze PHP code and generate a structured representation of its contents. This includes information about:
- Classes and their methods
- Standalone functions
- Import/use statements
- Documentation and annotations
The service exposes REST API endpoints that accept PHP code and return structured JSON data.
- PHP 7.4 or higher
- Composer
- Docker (optional, for containerized deployment)
- Docker Compose (optional, for local development)
git clone https://github.com/your-username/php-parser.git
cd php-parsercomposer installYou can run the application directly using PHP's built-in web server:
php -S localhost:5684 index.phpThe service will be available at http://localhost:5684.
For a containerized environment, use Docker Compose:
docker-compose upThe service will be available at http://localhost:5684.
Alternatively, you can build and run the Docker container manually:
# Build the Docker image
docker build -t php-parser .
# Run the container
docker run -p 5684:3010 -e PORT=3010 php-parserThe service will be available at http://localhost:5684.
GET /api/health
Example response:
{
"status": "hello from Php!",
"timestamp": 1650000000
}POST /api/parse
Request body:
{
"code": "<?php\n\nclass MyClass {\n public function hello() {\n return 'world';\n }\n}",
"fileType": "php"
}Example response:
{
"classes": {
"MyClass": {
"methods": {
"hello": {
"content": " public function hello() {\n return 'world';\n }",
"calledFunctions": [],
"docstring": "",
"fun_start_line": 4,
"fun_end_line": 6,
"doc_start_line": -1,
"doc_end_line": -1,
"annotations": [],
"code_line_start": 4,
"code_line_end": 6
}
},
"content": "class MyClass {\n public function hello() {\n return 'world';\n }\n}",
"docstring": "",
"class_start_line": 3,
"class_end_line": 7,
"doc_start_line": -1,
"doc_end_line": -1,
"annotations": []
}
},
"methods": {},
"imports": []
}The project includes integration tests to verify the functionality of the parser.
# Run the server first
docker-compose up -d
# Run the integration test
cd integration_test
python test.pyThe test output will be saved to integration_test/samples/sample1.json.
The project structure is organized as follows:
src/- Source code for the PHP parserCodeParser.php- Main parser classCodeVisitor.php- AST visitor for code analysisRouter.php- HTTP request router
index.php- Application entry pointintegration_test/- Integration teststest.py- Test runner scriptsamples/- Test samples and outputs
For production deployment, the project includes a workflow for building and pushing the Docker image to Azure Container Registry.
-
Build the production Docker image:
docker build -t php-parser:latest . -
Tag and push to your container registry:
docker tag php-parser:latest your-registry.azurecr.io/php-parser:latest docker push your-registry.azurecr.io/php-parser:latest
This project can be deployed to various Azure services:
-
Create an App Service Plan:
az appservice plan create --name php-parser-plan --resource-group your-resource-group --sku B1 --is-linux
-
Create a Web App with container configuration:
az webapp create --resource-group your-resource-group --plan php-parser-plan --name your-app-name --deployment-container-image-name your-registry.azurecr.io/php-parser:latest
-
Configure environment variables:
az webapp config appsettings set --resource-group your-resource-group --name your-app-name --settings PORT=3010 PHP_ENV=production
Deploy as a container instance:
az container create --resource-group your-resource-group --name php-parser --image your-registry.azurecr.io/php-parser:latest --dns-name-label php-parser --ports 3010 --environment-variables PORT=3010 PHP_ENV=production