Skip to content

Commit 501e2eb

Browse files
authored
Merge pull request #1 from Dropelikeit/feature/add-phpflex
Add installation and uninstallation script for PHPFlex
2 parents 5476cf8 + fdb7659 commit 501e2eb

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed

README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# PHPFlex
2+
3+
PHPFlex is a powerful command-line tool that allows developers to seamlessly switch between multiple PHP versions on their local environment. It provides a simple way to specify the desired PHP version for running commands or scripts, making it ideal for testing, development, and debugging across various PHP environments.
4+
5+
# _Features_:
6+
7+
- Version Switching: Instantly switch to any installed PHP version (e.g., 8.1, 8.2, 8.3) without changing system settings.
8+
- Command Execution: Run any PHP command or script with the specified version directly from the terminal.
9+
- User-friendly: Single function for all versions, simplifying multi-version management.
10+
- Error Handling: Clear error messages if the specified PHP version is not found.
11+
12+
```bash
13+
# Check the PHP version for 8.1
14+
phpFlex 8.1 -v
15+
16+
# Run a PHP script with version 8.2
17+
phpFlex 8.2 myscript.php
18+
19+
# Execute an inline PHP command with version 8.3
20+
phpFlex 8.3 -r 'echo "Hello, PHP 8.3!";'
21+
```
22+
23+
# Installation
24+
25+
---
26+
To start using PHPFlex, execute the script `install.sh`. It checks whether you are using bash or zsh and installs PHPFlex to your configuration file.
27+
28+
*Hint:* It is important that you reload your configuration after installation to use PHPFlex.
29+
30+
Sometimes it is necessary to set the correct permission to run the script
31+
```bash
32+
chmod -x install.sh && \
33+
./install.sh \
34+
source ~/<.zshrc | .bashrc>
35+
```
36+
37+
# Uninstall
38+
39+
---
40+
41+
To stop using PHPFlex, run the script `uninstall.sh`. It create a backup file of your configuration before PHPFlex is removed.
42+
43+
*Hint:* It is important to reload your configuration to remove PHPFlex from your current commandline session.
44+
45+
Sometimes it is necessary to set the correct permission to run the script
46+
```bash
47+
chmod -x uninstall.sh && \
48+
./uninstall.sh \
49+
source ~/<.zshrc | .bashrc>
50+
```
51+
52+
# Contributions
53+
54+
---
55+
56+
Help us to improve this project by reporting bugs, enhancement requests or other suggestions via the issue tracker.
57+
58+
After creating the issue tracker, it is also possible to create a PR with reference to the issue.
59+
60+
Thank you very much for your support!

install.sh

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
# declaration
4+
GREEN="\033[32m"
5+
RED="\033[31m"
6+
7+
# PHPFlex function
8+
php_flex_function='
9+
phpFlex() {
10+
local version=$1
11+
if [[ -z $version ]]; then
12+
echo "Please specify a PHP version, e.g., phpFlex 8.1 -v"
13+
return 1
14+
fi
15+
16+
# ignore parameter one because it is not necessary for the php command
17+
shift
18+
19+
local php_path=$(find /usr/local/Cellar/php@${version} -name "php" 2>/dev/null | head -n 1)
20+
if [[ -z $php_path ]]; then
21+
php_path=$(find /usr/local/Cellar/php/${version} -name "php" 2>/dev/null | head -n 1)
22+
fi
23+
if [[ -n $php_path ]]; then
24+
"$php_path" "$@"
25+
else
26+
echo "PHP $version not found."
27+
return 1
28+
fi
29+
}
30+
'
31+
32+
# Check, which shell is currently being used to find the correct configuration file
33+
if [[ $SHELL == *"zsh"* ]]; then
34+
shell_config="$HOME/.zshrc"
35+
elif [[ $SHELL == *"bash"* ]]; then
36+
shell_config="$HOME/.bashrc"
37+
else
38+
echo "Unsupported shell. Only zsh and bash are supported."
39+
exit 1
40+
fi
41+
42+
# Add the function if it does not yet exist.
43+
if grep -q "phpFlex()" "$shell_config"; then
44+
echo -e "${RED}The phpFlex function is already defined in $shell_config."
45+
else
46+
echo "$php_flex_function" >> "$shell_config"
47+
echo "phpFlex function added to $shell_config."
48+
echo "Please run 'source $shell_config' or restart your terminal to apply the changes. You can use \"source "
49+
echo -e "${GREEN}You can use following command to reload your configuration file:"
50+
51+
COMMAND=source $shell_config
52+
LINK_TEXT="source $shell_config"
53+
54+
CLICKABLE_LINK="\033]8;;${COMMAND}\033\\${LINK_TEXT}\033]8;;\033\\"
55+
56+
echo -e "${GREEN}${CLICKABLE_LINK}"
57+
fi

uninstall.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# declaration
4+
GREEN="\033[32m"
5+
6+
if [[ $SHELL == *"zsh"* ]]; then
7+
shell_config="$HOME/.zshrc"
8+
elif [[ $SHELL == *"bash"* ]]; then
9+
shell_config="$HOME/.bashrc"
10+
else
11+
echo "Unsupported shell. Only zsh and bash are supported."
12+
exit 1
13+
fi
14+
15+
echo "A backup of your original configuration file is saved as ${shell_config}.bak."
16+
cp "$shell_config" "${shell_config}.bak"
17+
18+
sed -i.bak '/phpFlex()/,/^}/d' "$shell_config"
19+
echo "phpFlex function has been removed from $shell_config."
20+
21+
echo "Please run 'source $shell_config' or restart your terminal to apply the changes."
22+
echo -e "${GREEN}You can use following command to reload your configuration file:"
23+
24+
COMMAND=source $shell_config
25+
LINK_TEXT="source $shell_config"
26+
27+
CLICKABLE_LINK="\033]8;;${COMMAND}\033\\${LINK_TEXT}\033]8;;\033\\"
28+
29+
echo -e "${GREEN}${CLICKABLE_LINK}${RESET}"

0 commit comments

Comments
 (0)