|
| 1 | +# PhpPy - PHP Python 🚀🐍 |
| 2 | +Seamlessly enabling secure and efficient execution of Python scripts within PHP applications. |
| 3 | + |
| 4 | +## 📌 Table of Contents |
| 5 | + |
| 6 | +- [📖 Overview](#-overview) |
| 7 | +- [✨ Features Summary](#-features-summary) |
| 8 | +- [⚙️ Configuration](#-configuration) |
| 9 | + - [🔧 ConfigManager](#-configmanager) |
| 10 | + - [📜 PhpPy](#-phppy) |
| 11 | + - [💻 CommandExecutor](#-commandexecutor) |
| 12 | +- [🚀 Quick Start](#-quick-start) |
| 13 | +- [🔑 Core Components](#-core-components) |
| 14 | +- [🛠 Framework Integration](#-framework-integration) |
| 15 | +- [📋 Changelog](#-changelog) |
| 16 | +- [🧪 Testing](#-testing) |
| 17 | +- [🔒 Security](#-security) |
| 18 | +- [🤝 Contributors](#-contributors) |
| 19 | +- [📄 License](#-license) |
| 20 | + |
| 21 | +--- |
| 22 | + |
| 23 | +## 📖 Overview |
| 24 | + |
| 25 | +The `PhpPy` package provides seamless integration between PHP and Python without API, enabling secure and efficient execution of Python scripts within PHP applications. It ensures structured script execution while managing configurations, arguments, environment variables, and error handling. |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## ✨ Features Summary |
| 30 | + |
| 31 | +### 🔐 Secure Execution |
| 32 | +- **Path Validation** ✅ Ensures scripts are within allowed directories. |
| 33 | +- **Argument & Environment Validation** 🔍 Restricts unauthorized input. |
| 34 | +- **Timeout Control** ⏳ Prevents long-running scripts. |
| 35 | +- **Uses `proc_close` as an alternative to `shell_exec`**. |
| 36 | + |
| 37 | +### 🔧 Flexible Configuration |
| 38 | +- Centralized settings via `ConfigManager`. |
| 39 | +- Customizable execution parameters. |
| 40 | + |
| 41 | +### 📤 Output Handling |
| 42 | +- Supports JSON parsing. |
| 43 | +- Captures and reports script errors. |
| 44 | + |
| 45 | +### 🚨 Error Management |
| 46 | +- Detailed exception handling for debugging. |
| 47 | +- Standardized error reporting. |
| 48 | + |
| 49 | +### 🔌 Extensibility |
| 50 | +- Modular execution through `CommandExecutor`. |
| 51 | +- Customizable for advanced use cases. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +## 🚀 Quick Start |
| 56 | + |
| 57 | +1. 📂 Create a folder for scripts, e.g., `phpPyScripts` in your project root directory. |
| 58 | +2. 📝 Create a Python script file (`.py` extension) and write Python code. [See this script example](https://github.com/omaralalwi/php-py/example-scripts/sum_calculator.py). |
| 59 | +3. 🔧 make script file executable, `chmod +x script_file_path` . |
| 60 | + |
| 61 | +### ⚡ Easy Usage |
| 62 | + |
| 63 | +```php |
| 64 | +<?php |
| 65 | +require_once 'vendor/autoload.php'; |
| 66 | + |
| 67 | +use Omaralalwi\PhpPy\PhpPy; |
| 68 | +use Omaralalwi\PhpPy\Managers\ConfigManager; |
| 69 | + |
| 70 | +$configManager = new ConfigManager([ |
| 71 | + 'scripts_directory' => 'phpPyScripts', |
| 72 | + 'python_executable' => '/usr/bin/python3', |
| 73 | + 'max_timeout' => 120, |
| 74 | +]); |
| 75 | + |
| 76 | +try { |
| 77 | + $result = PhpPy::build() |
| 78 | + ->setConfig($configManager) |
| 79 | + ->loadScript('sum_calculator.py') |
| 80 | + ->withArguments([10, 20, 30]) |
| 81 | + ->run(); |
| 82 | + |
| 83 | + print_r($result); // 60.0 |
| 84 | +} catch (Exception $e) { |
| 85 | + echo "Error: " . $e->getMessage(); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### 🔥 Advanced Usage |
| 90 | + |
| 91 | +```php |
| 92 | +<?php |
| 93 | +require_once 'vendor/autoload.php'; |
| 94 | + |
| 95 | +use Omaralalwi\PhpPy\PhpPy; |
| 96 | +use Omaralalwi\PhpPy\Managers\ConfigManager; |
| 97 | + |
| 98 | +$configManager = new ConfigManager([ |
| 99 | + 'scripts_directory' => 'phpPyScripts', |
| 100 | + 'python_executable' => '/usr/bin/python3', |
| 101 | + 'max_timeout' => 120, |
| 102 | +]); |
| 103 | + |
| 104 | +$result = PhpPy::build() |
| 105 | + ->setConfig($configManager) |
| 106 | + ->loadScript('advanced_example.py') |
| 107 | + ->withArguments([10, 20, 30]) |
| 108 | + ->withEnvironment(['FIRST_ENV_VAR' => 'some value', 'SECOND_ENV_VAR' => 'some value']) |
| 109 | + ->timeout(30) |
| 110 | + ->asJson() |
| 111 | + ->run(); |
| 112 | + |
| 113 | +try { |
| 114 | + print_r(json_decode($result)); |
| 115 | +} catch (Exception $e) { |
| 116 | + echo "Error: " . $e->getMessage(); |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +### 🌍 Real-World Example |
| 121 | + |
| 122 | +Example: Running **DeepSeek AI** on your server while handling API requests using PHP. |
| 123 | + |
| 124 | +```php |
| 125 | +<?php |
| 126 | +require_once 'vendor/autoload.php'; |
| 127 | + |
| 128 | +use Omaralalwi\PhpPy\PhpPy; |
| 129 | +use Omaralalwi\PhpPy\Managers\ConfigManager; |
| 130 | + |
| 131 | +$configManager = new ConfigManager([ |
| 132 | + 'scripts_directory' => 'deepSeekScripts', |
| 133 | + 'python_executable' => '/usr/bin/python3', |
| 134 | + 'max_timeout' => 120, |
| 135 | +]); |
| 136 | + |
| 137 | +header('Content-Type: application/json'); |
| 138 | +$valid_tokens = ['USER1' => 'abcd1234', 'USER2' => 'efgh5678']; |
| 139 | +$token = $_POST['token'] ?? ''; |
| 140 | +if (!isset($valid_tokens[$token])) { |
| 141 | + echo json_encode(['error' => 'Invalid token']); |
| 142 | + exit; |
| 143 | +} |
| 144 | +$prompt = $_POST['prompt'] ?? ''; |
| 145 | +if (!empty($prompt)) { |
| 146 | + $clean_prompt = escapeshellarg($prompt); |
| 147 | + $response = PhpPy::build() |
| 148 | + ->setConfig($configManager) |
| 149 | + ->loadScript('model_worker.py') |
| 150 | + ->withArguments($clean_prompt) |
| 151 | + ->timeout(30) |
| 152 | + ->asJson() |
| 153 | + ->run(); |
| 154 | + echo json_encode(['response' => trim($response)]); |
| 155 | +} else { |
| 156 | + echo json_encode(['error' => 'No prompt provided']); |
| 157 | +} |
| 158 | +``` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## 🛠 Framework Integration |
| 163 | + |
| 164 | +### [Laravel php-py](https://github.com/omaralalwi/laravel-php-py) |
| 165 | + |
| 166 | +--- |
| 167 | + |
| 168 | +## 📋 Changelog |
| 169 | + |
| 170 | +See detailed release notes in [CHANGELOG.md](CHANGELOG.md) 📜 |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## 🧪 Testing |
| 175 | + |
| 176 | +```bash |
| 177 | +composer test |
| 178 | +OR |
| 179 | +./vendor/bin/pest |
| 180 | +``` |
| 181 | + |
| 182 | +Test coverage is coming in future versions. 🚀 |
| 183 | + |
| 184 | +--- |
| 185 | + |
| 186 | +## 🔒 Security |
| 187 | + |
| 188 | +**Report Vulnerabilities**: Contact [omaralwi2010@gmail.com](mailto:omaralwi2010@gmail.com) 📩 |
| 189 | + |
| 190 | +--- |
| 191 | + |
| 192 | +## 🤝 Contributors |
| 193 | + |
| 194 | +A huge thank you to these amazing people who have contributed to this project! 🎉💖 |
| 195 | + |
| 196 | +<table> |
| 197 | + <tr> |
| 198 | + <td align="center"> |
| 199 | + <a href="https://github.com/omaralalwi"> |
| 200 | + <img src="https://avatars.githubusercontent.com/u/25439498?v=4" width="60px;" style="border-radius:50%;" alt="Omar AlAlwi"/> |
| 201 | + <br /> |
| 202 | + <b>Omar AlAlwi</b> |
| 203 | + </a> |
| 204 | + <br /> |
| 205 | + 🏆 Creator |
| 206 | + </td> |
| 207 | + </tr> |
| 208 | +</table> |
| 209 | + |
| 210 | +**Want to contribute?** Check out the [contributing guidelines](./CONTRIBUTING.md) and submit a pull request! 🚀 |
| 211 | + |
| 212 | +--- |
| 213 | + |
| 214 | +## 📄 License |
| 215 | + |
| 216 | +This package is open-source software licensed under the [MIT License](LICENSE.md). 📜 |
| 217 | + |
0 commit comments