定制 rawdreeg/phpswitcher 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rawdreeg/phpswitcher

最新稳定版本:v0.1.1

Composer 安装命令:

composer create-project rawdreeg/phpswitcher

包简介

A simple CLI tool to manage multiple PHP versions on macOS using Homebrew. Linux and Windows support planned.

README 文档

README

CI

PHP Switcher

A simple CLI tool to manage multiple PHP versions on macOS and Linux.

Features

  • Install specific PHP versions (via Homebrew for macOS, or APT for Linux).
  • Switch the active PHP version.
  • List all installed PHP versions.
  • Auto-detect required version from .php-version or composer.json.
  • Automatic version switching when you change directories.

Prerequisites

  • macOS: Requires Homebrew for installing and managing PHP versions.
  • Linux (Debian/Ubuntu): Requires apt and the software-properties-common package. sudo is required for installing and switching versions.

Installation

Run the following command in your terminal to download and execute the installation script:

# Ensure you have curl installed
bash -c "$(curl -fsSL https://raw.githubusercontent.com/rawdreeg/phpswitcher/main/install.sh)"

This will install phpswitcher to $HOME/.phpswitcher and add it to your shell's profile file (e.g., .zshrc, .bashrc).

After installation, open a new terminal session or run source ~/.zshrc (or the equivalent for your shell) and verify with:

phpswitcher help

Usage

List installed PHP versions:

Shows all available PHP versions and highlights the one that is currently active.

phpswitcher list
# Example Output:
#
# Installed PHP Versions (via Homebrew):
#    7.4
#  * 8.1 (active)

Install a PHP version:

If you are in a directory containing a composer.json file with a PHP requirement, you can omit the <version> argument to automatically detect and install the required X.Y version.

phpswitcher install [<version>]
# Examples:
phpswitcher install 8.1
phpswitcher install 7.4

# Auto-detect from composer.json in current directory:
cd my-project-using-php8.0/
phpswitcher install

Switch active PHP version:

If you are in a directory containing a composer.json file with a PHP requirement (require.php or config.platform.php), you can omit the <version> argument, and phpswitcher will attempt to detect and use the appropriate X.Y version.

phpswitcher use [<version>]
# Examples:
phpswitcher use 8.1

# Auto-detect from composer.json in current directory:
cd my-project-using-php7.4/
phpswitcher use

Show Version:

phpswitcher version

Self-Update:

phpswitcher self-update

This will fetch and install the latest version of phpswitcher from GitHub.

Check active PHP version (after switching):

php --version

Automatic Version Switching

phpswitcher supports automatic version switching when you change directories. This is achieved by hooking into your shell's prompt.

How it Works

  1. When you cd into a new directory, phpswitcher looks for a .php-version file in the current directory or any parent directory.
  2. If a .php-version file is found, it reads the required version from it.
  3. If the required version is not the currently active version, phpswitcher automatically runs phpswitcher use to switch to the correct version.

Usage

To use this feature, simply create a file named .php-version in the root of your project and put the desired PHP version number in it.

# In your project's root directory
echo "8.1" > .php-version

Now, whenever you cd into this directory (or any subdirectory), phpswitcher will ensure that PHP 8.1 is activated automatically.

This feature is enabled by default during the installation process, which adds a sourcing line to your shell's profile file (.bashrc or .zshrc).

Development

  1. Clone the repository: git clone https://github.com/rawdreeg/phpswitcher.git
  2. Navigate into the project directory: cd phpswitcher.
  3. The main script is bin/phpswitcher. You can run it directly for testing:
    ./bin/phpswitcher install 8.2
  4. To create a release artifact, run the build script:
    ./build.sh
    This will create a phpswitcher.tar.gz in the root directory.

Contributing

Contributions are welcome! Please feel free to open issues or submit pull requests.

License

MIT License

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 18
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 17
  • Watchers: 1
  • Forks: 0
  • 开发语言: Shell

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-07