Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ acinclude.m4
config.guess
config.sub
/composer.lock

/*.command
/py-vendor
/requirements.txt
/phpy.lock
13 changes: 2 additions & 11 deletions bin/phpy
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

use Symfony\Component\Console\Application;
use PhpyTool\Phpy\Application;

require_once ($GLOBALS['_composer_autoload_path'] ?? dirname(__DIR__) . '/vendor/autoload.php');

$application = new Application();

$application->addCommands([
new \PhpyTool\Commands\PipModuleInstall(),
new \PhpyTool\Commands\PhpyInstall(),
new \PhpyTool\Commands\PythonInstall(),
new \PhpyTool\Commands\PipMirrorConfig(),
new \PhpyTool\Commands\ScanImport(),
]);
try {
$application->run();
(new Application())->run();
} catch (Throwable $e) {
exit($e->getMessage() . PHP_EOL);
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"require-dev": {
"phpunit/phpunit": "^10.4",
"friendsofphp/php-cs-fixer": "^3.40"
"friendsofphp/php-cs-fixer": "^3.40",
"symfony/var-dumper": "^6.0 | ^7.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions docs/cn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ PHP 扩展
* [IDE 提示](php/composer.md)
* [Socket API](php/socket.md)
* [继承 Python 类](php/inherit.md)
* [PHPy 工具](php/phpy.md)

Python 模块
---
Expand Down
145 changes: 145 additions & 0 deletions docs/cn/php/phpy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# PHPy 管理工具

PHPy提供类似`composer`的包管理工具,可以安装、更新、卸载 `Python`环境、`swoole/phpy`拓展及`Python-module`,
对其他`composer`引入的`Python`相关信息和模块进行依赖管理。

## 使用

通过`composer`安装`swoole/phpy`
```shell
composer require swoole/phpy
```

## 文档

### 1. 初始化`phpy.json`配置

#### 命令:
```shell
./vendor/bin/phpy init-config
```

**与`composer`类似,`PHPy`也有自己的配置管理文件`phpy.json`;使用`init-config`命令可以在当前目录下创建`phpy.json`文件。**

#### 文件内容:
```php
{
// 全局配置
"config": {
// 缓存目录
"cache-dir": "~/.cache/phpy",
// 扫描路径
"scan-dirs": [
],
// pip源
"pip-index-url": ""
},
// python 配置
"python": {
// 源码路径
"source-url": "https://github.com/python/cpython.git",
// 安装路径
"install-dir": "/usr",
// 安装版本,不支持latest构建
"install-version": "v3.13.2",
// 编译参数(建议不要改动)
"install-configure": [
"--enable-shared",
"--with-system-expat",
"--with-system-ffi",
"--enable-ipv6",
"--enable-loadable-sqlite-extensions",
"--with-computed-gotos",
"--with-ensurepip=install"
]
},
// phpy 配置
"phpy": {
// 源码路径
"source-url": "https://github.com/swoole/phpy.git",
// 安装路径,支持latest使用master分支进行构建
"install-version": "latest",
// 编译参数
"install-configure": [],
// ini文件路径,空字符串为不自动引入php.ini
"ini-path": "/usr/local/etc/php/conf.d/xx-php-ext-phpy.ini"
},
// 模块配置
"modules": {
// 例子
"pandas": "^2.0"
}
}
```

### 2. 依赖安装

#### 命令:
```shell
./vendor/bin/phpy install
```
- `install`命令会根据当前项目及`vendor`中引入的所有`composer`包的`phpy.json`配置信息进行安装,安装内容如下:
- 编译构建依赖,详见[BuildToolsInstaller.php](../../../tools/src/Phpy/Installer/BuildToolsInstaller.php)
- 编译安装`Python`环境,详见[PythonInstaller.php](../../../tools/src/Phpy/Installer/PythonInstaller.php)
- 编译安装`phpy`拓展,详见[PhpyInstaller.php](../../../tools/src/Phpy/Installer/PhpyInstaller.php)
- 安装`Python`模块,详见[ModuleInstaller.php](../../../tools/src/Phpy/Installer/ModuleInstaller.php)
- `install`命令会在项目路径下创建`phpy.lock`文件,用于记录安装信息,下次安装时,如果`phpy.lock`文件存在,则不会重复安装。
- `install`命令默认使用`Python-venv`环境,会在项目路径下创建`py-vendor`目录,用于存储`Python`环境及模块。
- `install`命令会在项目路径下创建如下文件,**以下文件建议加入项目`.gitignore`文件**:
- `pip.command`:用于提供`executePip`方法标准化执行`pip`命令
- `python.command`:用于提供`executePython`方法标准化执行`python`命令
- `phpy.command`:用于提供`executePhpy`方法标准化执行`phpy`命令
- `phpy.lock`:用于记录安装信息,下次安装时,如果`phpy.lock`文件存在,则不会重复安装
- `requirements.txt`:用于记录安装的`Python`模块信息,使用标准化`executePip`安装
- 更多查看`--help`

### 3. 依赖更新

#### 命令:
```shell
./vendor/bin/phpy update
```

`update`命令会根据`phpy.json`配置信息进行更新,更多查看`--help`

### 4. 环境检查

#### 命令:
```shell
./vendor/bin/phpy show
```

`show`命令会展示当前`Python`环境信息及引入的`Python`模块信息,更多查看`--help`

```shell
/var/www/test-project # ./vendor/bin/phpy show
[>] Python-env:
[>] Python 3.13.2
[>] pip 25.0.1 from /var/www/phpy/py-vendor/lib/python3.13/site-packages/pip (python 3.13)
[>] Python-includes:
[>] -I/var/www/phpy/py-vendor/include/python3.13 -I/var/www/phpy/py-vendor/include/python3.13
[>] Python-modules:
[>] Package Version
[>] ------- -------
[>] pip 25.0.1
[>] pyorc 0.10.0
```

### 5. 扫描引入

#### 命令:
```shell
./vendor/bin/phpy scan
```

`scan`命令会根据`phpy.json`的`config.scan-dirs`扫描所有php文件并检查依赖的`Python-module`,
引入并安装,更多查看`--help`

### 6. 缓存清除

#### 命令:
```shell
./vendor/bin/phpy clear-cache
```

`clear-cache`命令会根据`phpy.json`的`config.cache-dir`清除相关缓存,更多查看`--help`
29 changes: 29 additions & 0 deletions phpy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"config": {
"cache-dir": "~/.cache/phpy",
"scan-dirs": [
],
"pip-index-url": ""
},
"python": {
"source-url": "https://github.com/python/cpython.git",
"install-dir": "/usr",
"install-version": "v3.13.2",
"install-configure": [
"--enable-shared",
"--with-system-expat",
"--with-system-ffi",
"--enable-ipv6",
"--enable-loadable-sqlite-extensions",
"--with-computed-gotos",
"--with-ensurepip=install"
]
},
"phpy": {
"source-url": "https://github.com/swoole/phpy.git",
"install-version": "latest",
"install-configure": [],
"ini-path": "/usr/local/etc/php/conf.d/xx-php-ext-phpy.ini"
},
"modules": {}
}
Loading