|
| 1 | +# PHP Parser |
| 2 | + |
| 3 | +A service for parsing PHP code and extracting structured information about classes, methods, and imports. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +PHP Parser is a service that uses the [nikic/php-parser](https://github.com/nikic/PHP-Parser) library to analyze PHP code and generate a structured representation of its contents. This includes information about: |
| 8 | + |
| 9 | +- Classes and their methods |
| 10 | +- Standalone functions |
| 11 | +- Import/use statements |
| 12 | +- Documentation and annotations |
| 13 | + |
| 14 | +The service exposes REST API endpoints that accept PHP code and return structured JSON data. |
| 15 | + |
| 16 | +## Prerequisites |
| 17 | + |
| 18 | +- PHP 7.4 or higher |
| 19 | +- [Composer](https://getcomposer.org/download/) |
| 20 | +- [Docker](https://www.docker.com/products/docker-desktop/) (optional, for containerized deployment) |
| 21 | +- [Docker Compose](https://docs.docker.com/compose/install/) (optional, for local development) |
| 22 | + |
| 23 | +## Installation |
| 24 | + |
| 25 | +### Clone the repository |
| 26 | + |
| 27 | +```bash |
| 28 | +git clone https://github.com/your-username/php-parser.git |
| 29 | +cd php-parser |
| 30 | +``` |
| 31 | + |
| 32 | +### Install dependencies |
| 33 | + |
| 34 | +```bash |
| 35 | +composer install |
| 36 | +``` |
| 37 | + |
| 38 | +## Running Locally |
| 39 | + |
| 40 | +### Using PHP's built-in server |
| 41 | + |
| 42 | +You can run the application directly using PHP's built-in web server: |
| 43 | + |
| 44 | +```bash |
| 45 | +php -S localhost:5684 index.php |
| 46 | +``` |
| 47 | + |
| 48 | +The service will be available at http://localhost:5684. |
| 49 | + |
| 50 | +### Using Docker |
| 51 | + |
| 52 | +For a containerized environment, use Docker Compose: |
| 53 | + |
| 54 | +```bash |
| 55 | +docker-compose up |
| 56 | +``` |
| 57 | + |
| 58 | +The service will be available at http://localhost:5684. |
| 59 | + |
| 60 | +### Using Docker manually |
| 61 | + |
| 62 | +Alternatively, you can build and run the Docker container manually: |
| 63 | + |
| 64 | +```bash |
| 65 | +# Build the Docker image |
| 66 | +docker build -t php-parser . |
| 67 | + |
| 68 | +# Run the container |
| 69 | +docker run -p 5684:3010 -e PORT=3010 php-parser |
| 70 | +``` |
| 71 | + |
| 72 | +The service will be available at http://localhost:5684. |
| 73 | + |
| 74 | +## API Usage |
| 75 | + |
| 76 | +### Health Check |
| 77 | + |
| 78 | +``` |
| 79 | +GET /api/health |
| 80 | +``` |
| 81 | + |
| 82 | +Example response: |
| 83 | +```json |
| 84 | +{ |
| 85 | + "status": "hello from Php!", |
| 86 | + "timestamp": 1650000000 |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +### Parse PHP Code |
| 91 | + |
| 92 | +``` |
| 93 | +POST /api/parse |
| 94 | +``` |
| 95 | + |
| 96 | +Request body: |
| 97 | +```json |
| 98 | +{ |
| 99 | + "code": "<?php\n\nclass MyClass {\n public function hello() {\n return 'world';\n }\n}", |
| 100 | + "fileType": "php" |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +Example response: |
| 105 | +```json |
| 106 | +{ |
| 107 | + "classes": { |
| 108 | + "MyClass": { |
| 109 | + "methods": { |
| 110 | + "hello": { |
| 111 | + "content": " public function hello() {\n return 'world';\n }", |
| 112 | + "calledFunctions": [], |
| 113 | + "docstring": "", |
| 114 | + "fun_start_line": 4, |
| 115 | + "fun_end_line": 6, |
| 116 | + "doc_start_line": -1, |
| 117 | + "doc_end_line": -1, |
| 118 | + "annotations": [], |
| 119 | + "code_line_start": 4, |
| 120 | + "code_line_end": 6 |
| 121 | + } |
| 122 | + }, |
| 123 | + "content": "class MyClass {\n public function hello() {\n return 'world';\n }\n}", |
| 124 | + "docstring": "", |
| 125 | + "class_start_line": 3, |
| 126 | + "class_end_line": 7, |
| 127 | + "doc_start_line": -1, |
| 128 | + "doc_end_line": -1, |
| 129 | + "annotations": [] |
| 130 | + } |
| 131 | + }, |
| 132 | + "methods": {}, |
| 133 | + "imports": [] |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +## Running Tests |
| 138 | + |
| 139 | +The project includes integration tests to verify the functionality of the parser. |
| 140 | + |
| 141 | +```bash |
| 142 | +# Run the server first |
| 143 | +docker-compose up -d |
| 144 | + |
| 145 | +# Run the integration test |
| 146 | +cd integration_test |
| 147 | +python test.py |
| 148 | +``` |
| 149 | + |
| 150 | +The test output will be saved to `integration_test/samples/sample1.json`. |
| 151 | + |
| 152 | +## Development |
| 153 | + |
| 154 | +The project structure is organized as follows: |
| 155 | + |
| 156 | +- `src/` - Source code for the PHP parser |
| 157 | + - `CodeParser.php` - Main parser class |
| 158 | + - `CodeVisitor.php` - AST visitor for code analysis |
| 159 | + - `Router.php` - HTTP request router |
| 160 | +- `index.php` - Application entry point |
| 161 | +- `integration_test/` - Integration tests |
| 162 | + - `test.py` - Test runner script |
| 163 | + - `samples/` - Test samples and outputs |
| 164 | + |
| 165 | +## Deployment |
| 166 | + |
| 167 | +### Production Docker Deployment |
| 168 | + |
| 169 | +For production deployment, the project includes a workflow for building and pushing the Docker image to Azure Container Registry. |
| 170 | + |
| 171 | +1. Build the production Docker image: |
| 172 | + ```bash |
| 173 | + docker build -t php-parser:latest . |
| 174 | + ``` |
| 175 | + |
| 176 | +2. Tag and push to your container registry: |
| 177 | + ```bash |
| 178 | + docker tag php-parser:latest your-registry.azurecr.io/php-parser:latest |
| 179 | + docker push your-registry.azurecr.io/php-parser:latest |
| 180 | + ``` |
| 181 | + |
| 182 | +### Azure Deployment |
| 183 | + |
| 184 | +This project can be deployed to various Azure services: |
| 185 | + |
| 186 | +#### Azure App Service |
| 187 | + |
| 188 | +1. Create an App Service Plan: |
| 189 | + ```bash |
| 190 | + az appservice plan create --name php-parser-plan --resource-group your-resource-group --sku B1 --is-linux |
| 191 | + ``` |
| 192 | + |
| 193 | +2. Create a Web App with container configuration: |
| 194 | + ```bash |
| 195 | + 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 |
| 196 | + ``` |
| 197 | + |
| 198 | +3. Configure environment variables: |
| 199 | + ```bash |
| 200 | + az webapp config appsettings set --resource-group your-resource-group --name your-app-name --settings PORT=3010 PHP_ENV=production |
| 201 | + ``` |
| 202 | + |
| 203 | +#### Azure Container Instances |
| 204 | + |
| 205 | +Deploy as a container instance: |
| 206 | + |
| 207 | +```bash |
| 208 | +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 |
| 209 | +``` |
| 210 | + |
| 211 | +## License |
| 212 | + |
| 213 | +[MIT](LICENSE) |
0 commit comments