Siam博客

brew安装swoole及指定旧版本

2025-07-24

添加扩展库安装swoole


brew tap shivammathur/extensions

# 安装swoole 到8.2php
brew install swoole@8.2

问题

安装swoole时后面带的标签是指定安装的相关 php 版本号,无法指定 swoole 版本号,作者相关说明 https://github.com/shivammathur/homebrew-extensions/issues/4225

There is no specific support to install a specific version for an extension in this tap. Please use PECL directly for this purpose.

安装的版本是跟随shivammathur/extensions库的更新 指定 swoole 版本号,文件在Formula/swoole@8.2.rb

解决方法

我们可以更改自己电脑上的扩展库文件,来达到安装旧版swoole 的目的

首先 查看扩展这个文件的 commit 历史,找到对应的想要的版本号文件 如 swoole@8.2: update 5.1.6 bottle. 这个 commit 相关文件内容覆盖自己电脑上的文件

自己电脑上文件的路径参考:/opt/homebrew/Library/Taps/shivammathur/homebrew-extensions/Formula/swoole@8.2.rb

cp /opt/homebrew/Library/Taps/shivammathur/homebrew-extensions/Formula/swoole@8.2.rb /opt/homebrew/Library/Taps/shivammathur/homebrew-extensions/Formula/swoole@8.2.rb.bak

rm /opt/homebrew/Library/Taps/shivammathur/homebrew-extensions/Formula/swoole@8.2.rb

vim /opt/homebrew/Library/Taps/shivammathur/homebrew-extensions/Formula/swoole@8.2.rb

为此写了一个小脚本

#!/usr/bin/env php
<?php

declare(strict_types=1);

$config = [
    '8.1' => [
        '5.1.6' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/54f07aec8e5df40d75786509b7b0cf119dc2eaf7/Formula/swoole%408.1.rb',
        '6.0.2' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/f0778ff3ef1b32681ef3f9f794dadfad606149e6/Formula/swoole%408.1.rb',
    ],
    '8.2' => [
        '5.1.6' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/54f07aec8e5df40d75786509b7b0cf119dc2eaf7/Formula/swoole%408.2.rb',
        '6.0.2' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/8379c51caf5e4dbde7a3c3da2a0fd3caf9099d8b/Formula/swoole%408.2.rb',
    ],
    '8.3' => [
        '5.1.6' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/7f2bb367c11e0060f74dffe626f2e479dbe80ead/Formula/swoole%408.3.rb',
        '6.0.2' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/3d0c0e7170b040d5e4ca2f83516e5e049b6a9dc7/Formula/swoole%408.3.rb',
    ],
    '8.4' => [
        '5.1.3' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/8622c3e3f4de5a1961d620613bf467aff37cad96/Formula/swoole%408.4.rb',
        '6.0.2' => 'https://raw.githubusercontent.com/shivammathur/homebrew-extensions/b06321ebe6057548f485b70248d88ad3da9b713a/Formula/swoole%408.4.rb',
    ],
];

/**
 * CLI工具:切换Homebrew安装的PHP Swoole版本
 * 用法:php switch-brew-swoole-rb.php [homebrew路径] <php版本> <swoole版本>
 * 示例:php switch-brew-swoole-rb.php 8.1 4.8.12
 */

// 检查参数数量
if ($argc < 3 || $argc > 4) {
    echo "用法: php switch-brew-swoole-rb.php [homebrew扩展Formula路径] <php版本> <swoole版本>\n";
    echo "示例: php switch-brew-swoole-rb.php 8.1 5.1.6\n";
    echo "=======\n";
    // 输出支持的 php 版本和 swoole 版本
    echo "支持的版本:\n";
    foreach ($config as $phpVersion => $swooleVersions) {
        echo "PHP:{$phpVersion}\n";
        foreach ($swooleVersions as $swooleVersion => $formulaUrl) {
            echo "- {$swooleVersion}\n";
        }
    }
    exit(1);
}

// 解析参数
if ($argc == 3) {
    // 如果只有两个参数,则使用默认Homebrew路径
    $brewPath = '/opt/homebrew/Library/Taps/shivammathur/homebrew-extensions/Formula/';
    $phpVersion = $argv[1];
    $swooleVersion = $argv[2];
} else {
    // 如果有三个参数,第一个是Homebrew路径
    $brewPath = $argv[1];
    $phpVersion = $argv[2];
    $swooleVersion = $argv[3];
}

// 验证参数
if (! is_dir($brewPath)) {
    echo "错误: Homebrew路径 '{$brewPath}' 不存在或不是一个目录\n";
    exit(1);
}

// 验证PHP版本格式
if (! preg_match('/^\d+(\.\d+)?$/', $phpVersion)) {
    echo "错误: PHP版本格式不正确,应为 'x.y' \n";
    exit(1);
}

// 验证Swoole版本格式
if (! preg_match('/^\d+\.\d+\.\d+$/', $swooleVersion)) {
    echo "错误: Swoole版本格式不正确,应为 'x.y.z'\n";
    exit(1);
}

// 文件路径
$formulaFile = $brewPath . "swoole@{$phpVersion}.rb";
// 不存在 则停止
if (! file_exists($formulaFile)) {
    echo "错误: 扩展库中无该版本配置文件 {$formulaFile}\n";
    exit(1);
}

// 下载对应文件
$formulaUrl = $config[$phpVersion][$swooleVersion] ?? null;
// 配置不存在
if (! $formulaUrl) {
    echo "错误: 不支持版本,请查看该脚本支持的版本列表\n";
    exit(1);
}

// 备份文件
$backupFile = $formulaFile . '.bak';
// 日志 正在将 xx 备份至 xxx
echo "正在将 {$formulaFile} 备份至 {$backupFile}\n";

// 备份
copy($formulaFile, $backupFile);

// 下载
file_put_contents($formulaFile, file_get_contents($formulaUrl));

echo "切换配置完成,请手动执行 brew reinstall swoole@{$phpVersion}\n";
本文链接:
版权声明: 本文由 Siam原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
Tags: SWOOLE

扫描二维码,分享此文章