Skip to content

Penify-dev/php-parser

Repository files navigation

PHP Parser

A service for parsing PHP code and extracting structured information about classes, methods, and imports.

Overview

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.

Prerequisites

Installation

Clone the repository

git clone https://github.com/your-username/php-parser.git
cd php-parser

Install dependencies

composer install

Running Locally

Using PHP's built-in server

You can run the application directly using PHP's built-in web server:

php -S localhost:5684 index.php

The service will be available at http://localhost:5684.

Using Docker

For a containerized environment, use Docker Compose:

docker-compose up

The service will be available at http://localhost:5684.

Using Docker manually

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-parser

The service will be available at http://localhost:5684.

API Usage

Health Check

GET /api/health

Example response:

{
  "status": "hello from Php!",
  "timestamp": 1650000000
}

Parse PHP Code

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": []
}

Running Tests

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.py

The test output will be saved to integration_test/samples/sample1.json.

Development

The project structure is organized as follows:

  • src/ - Source code for the PHP parser
    • CodeParser.php - Main parser class
    • CodeVisitor.php - AST visitor for code analysis
    • Router.php - HTTP request router
  • index.php - Application entry point
  • integration_test/ - Integration tests
    • test.py - Test runner script
    • samples/ - Test samples and outputs

Deployment

Production Docker Deployment

For production deployment, the project includes a workflow for building and pushing the Docker image to Azure Container Registry.

  1. Build the production Docker image:

    docker build -t php-parser:latest .
  2. 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

Azure Deployment

This project can be deployed to various Azure services:

Azure App Service

  1. Create an App Service Plan:

    az appservice plan create --name php-parser-plan --resource-group your-resource-group --sku B1 --is-linux
  2. 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
  3. Configure environment variables:

    az webapp config appsettings set --resource-group your-resource-group --name your-app-name --settings PORT=3010 PHP_ENV=production

Azure Container Instances

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

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published