Skip to content

Commit

Permalink
refactor: Improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
leocavalcante committed Dec 6, 2023
1 parent 600c811 commit 10d6285
Show file tree
Hide file tree
Showing 12 changed files with 109 additions and 73 deletions.
Empty file removed .phpctl
Empty file.
72 changes: 56 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,72 @@ Then you can use `phpctl` or `pctl` with a subcommand:
phpctl <command> [arguments]
```

### Commands
For example
```shell
phpctl php -m # To show built-in modules
```
Or
```shell
phpctl sh echo 'Hello, World!' # To run arbitrary sh commands inside the container
```

## Command list

### Developing
| Command | Description |
|-----------------------------|---------------------------------------------------------------------------------------------|
| `help` or `man` | Shows a help message. |
| `doctor` | Inspects the current `PHP_VERSION` and `PHPCTL_IMAGE`. |
| `install` | Installs `phpctl` (and `pctl`) globally in your system. |
| `build` | Builds the current `Dockerfile` (useful for custom images). |
| `images` | Shows local `phpctl` images. |
| `php` | **Runs PHP commands** (`phpctl php -v` or `pctl php -m`). |
| `php` | **Runs PHP commands** (`phpctl php -v` or `phpctl php -m`). |
| `composer` | Runs Composer commands (`phpctl composer install` or `pctl composer validate`). |
| `at [port] [command]` | Runs commands within a bound port (default port is `80`). |
| `server [port] [directory]` | Runs PHP's built-in web-server (default port is `80` and default directory is current `.`). |
| `sh [commands]` | Starts an interactive Shell session or runs `sh` commands. |
| `at [port] [command]` | Run commands within a bound port (default port is `80`). |
| `repl` | Starts a PHP REPL session (powered by [PsySH](https://psysh.org/)). |
| `fix [directory]` | Runs PHP CS Fixer on the given directory. |

### The `.phpctl` file
### Useful
| Command | Description |
|-------------------|----------------------------------------------------------------------|
| `sh [commands]` | Starts an interactive Shell session or runs `sh` commands. |
| `repl` | Starts a PHP REPL session (powered by [PsySH](https://psysh.org/)). |
| `fix [arguments]` | Runs PHP CS Fixer on the given directory. |

### Starters
| Command | Description |
|------------------------------|---------------------------------------------------------------------|
| `install` | Installs `phpctl` (and `pctl`) globally in your system. |
| `new [template] [directory]` | Creates a new project from a template (`composer create-project`). |

### Helpers
| Command | Description |
|------------------------------|--------------------------------------------------------------|
| `help` or `man` | Displays a help message. |
| `doctor` | Inspects the current `PHP_VERSION` and `PHPCTL_IMAGE`. |
| `build` | Builds the current `Dockerfile` (useful for custom images). |
| `images` | Shows local `phpctl` images. |

## The `.phpctlrc` file

You can also add a `.phpctlrc` file at project's root directory with some overrides like:

You can also add a `.phpctl` file with some overrides like:
### Environment variables

| Variable | Description |
| --- | --- |
| `PHP_VERSION` | Values can be `82` and `83` |
| Variable | Description |
|----------------|-----------------------------------|
| `PHP_VERSION` | Values can be `82` and `83` |
| `PHPCTL_IMAGE` | Use to name your own custom image |

For example:
```shell
PHP_VERSION=83
```

### Docker arguments

You can also provide any additional [Docker `run` arguments](https://docs.docker.com/engine/reference/commandline/run/#options) using the `args` variable.

For example, suppose you want to bind the `9501` port from the running `phpctl` container to your host,
you can add the following to your `.phpctlrc` file:
```shell
args=(-p 9501:9501)
```

## Modules
<details>
<summary>For the default Docker image we have the following modules installed (click to expand).</summary>
Expand Down
4 changes: 2 additions & 2 deletions phpctl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
PHPCTL_DIR=$(dirname "$(realpath "$0")")
if [ -s .phpctl ]; then
if [ -s .phpctlrc ]; then
set -a
. .phpctl
. .phpctlrc
set +a
fi

Expand Down
7 changes: 7 additions & 0 deletions src/composer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
composer() {
run -- composer $@
}

new() {
run -- composer create-project $@
}
8 changes: 6 additions & 2 deletions src/docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ images() {
}

run() {
composer_auth="${COMPOSER_AUTH//[$'\t\r\n ']}"
echo "docker run --rm -e COMPOSER_AUTH=$composer_auth -v $(pwd):/opt -w /opt --entrypoint ${2:-php} ${1:-$PHPCTL_TTY} $PHPCTL_IMAGE"
docker run \
--rm "$PHPCTL_TTY" \
-e COMPOSER_AUTH="$COMPOSER_AUTH" \
-v "$(pwd)":/opt -w /opt \
--entrypoint sh \
${args[@]} $1 "$PHPCTL_IMAGE" -c "${*:2}"
}
4 changes: 2 additions & 2 deletions src/doctor.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
doctor() {
echo PHP_VERSION="$PHP_VERSION"
echo PHPCTL_IMAGE="$PHPCTL_IMAGE"
echo -e "\033[0;32mPHP_VERSION\033[0m=$PHP_VERSION"
echo -e "PHPCTL_IMAGE=\033[0;33m$PHPCTL_IMAGE\033[0m"
}
47 changes: 24 additions & 23 deletions src/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@ list() {
}

help() {
echo " "
echo " _ _ _ "
echo " ___| |_ ___ ___| |_| | "
echo "| . | | . | _| _| | "
echo "| _|_|_| _|___|_| |_| "
echo "|_| |_| "
echo " "
echo "Usage: phpctl <command> [arguments]"
echo " "
echo "Commands:"
echo " help Show this help message"
echo " doctor Inspects the current PHP_VERSION and PHPCTL_IMAGE"
echo " install Install phpctl (and pctl) globally in you system"
echo " build Builds the current Dockerfile (useful for custom images)"
echo " images Shows local phpctl images"
echo " php Runs PHP commands"
echo " composer Runs Composer commands"
echo " server [port] [directory] Runs PHP's built-in web server (default port is 80 and default directory is current .)"
echo " sh [commands] Starts an interactive Shell session or runs sh commands"
echo " at [port] [command] Runs commands within a bound port (default port is 80)"
echo " repl Starts a PHP REPL session (powered by PsySH)"
echo " fix [directory] Runs PHP CS Fixer on the given directory."
echo " "
echo -e " _ _ _ "
echo -e " ___| |_ ___ ___| |_| |"
echo -e "| . | | . | _| _| |"
echo -e "| _|_|_| _|___|_| |_|"
echo -e "|_| |_| "
echo -e $(doctor)
echo -e ""
echo -e "\033[0;33mUsage:\033[0m "
echo -e " phpctl <command> [arguments] "
echo -e ""
echo -e "\033[0;33mAvailable commands:\033[0m"
echo -e "\033[0;32m php \033[0m Runs PHP commands"
echo -e "\033[0;32m composer \033[0m Runs Composer commands"
echo -e "\033[0;32m at [port] [command] \033[0m Runs commands within a bound port (default port is 80)"
echo -e "\033[0;32m server [port] [directory] \033[0m Runs PHP's built-in web server (default port is 80 and default directory is current .)"
echo -e "\033[0;32m sh [commands] \033[0m Starts an interactive Shell session or runs sh commands"
echo -e "\033[0;32m repl \033[0m Starts a PHP REPL session (powered by PsySH)"
echo -e "\033[0;32m fix [arguments] \033[0m Runs PHP CS Fixer on the given directory."
echo -e "\033[0;32m install \033[0m Installs phpctl (and pctl) globally in you system"
echo -e "\033[0;32m new [template] [directory] \033[0m Creates a new project from a template (composer create-project)"
echo -e "\033[0;32m help or man \033[0m Displays this help message"
echo -e "\033[0;32m doctor \033[0m Inspects the current PHP_VERSION and PHPCTL_IMAGE"
echo -e "\033[0;32m build \033[0m Builds the current Dockerfile (useful for custom images)"
echo -e "\033[0;32m images \033[0m Shows local phpctl images"
}
29 changes: 9 additions & 20 deletions src/php.sh
Original file line number Diff line number Diff line change
@@ -1,32 +1,21 @@
php() {
$(run "$PHPCTL_TTY" php) ${@:--v}
}

composer() {
$(run "$PHPCTL_TTY" composer) $@
run -- php ${@--v}
}

repl() {
# shellcheck disable=SC2091
$(run "$PHPCTL_TTY" psysh)
run -- psysh
}

server() {
port=${1:-80}
dir=${2:-.}
at "$port" php -S 0.0.0.0:"$port" -t "$dir"
fix() {
run -- php-cs-fixer fix $@
}

at() {
port=${1:-80}
$(run "$PHPCTL_TTY -p$port:$port" "$2") ${@:3}
run "-p$1:$1" ${@:2}
}

fix() {
if [ -n "$1" ]; then
$(run "$PHPCTL_TTY" php-cs-fixer) fix $@
exit 0
fi

$(run "$PHPCTL_TTY" php-cs-fixer) help fix
server() {
port=${1:-80}
dir=${2:-.}
at "$port" php -S 0.0.0.0:"$port" -t "$dir"
}
5 changes: 2 additions & 3 deletions src/sh.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
sh() {
if [ -z "$1" ]; then
# shellcheck disable=SC2091
$(run "$PHPCTL_TTY" sh)
run -- sh
else
$(run "$PHPCTL_TTY" sh) -c "$*"
run -- "$*"
fi
}
3 changes: 0 additions & 3 deletions tests/docker_test.sh

This file was deleted.

1 change: 0 additions & 1 deletion tests/doctor_test.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
function test_doctor() {
assert_contains "PHP_VERSION=" "$(./phpctl doctor)"
assert_contains "PHPCTL_IMAGE=" "$(./phpctl doctor)"
}
2 changes: 1 addition & 1 deletion tests/help_test.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
function test_help_is_default() {
assert_contains "Usage: phpctl" "$(./phpctl)"
assert_contains "phpctl <command> [arguments]" "$(./phpctl)"
}

0 comments on commit 10d6285

Please sign in to comment.