|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# https://github.com/cubny/php-built-in-server-manager/blob/9a5cbeaad50a108d6058b882b83ba23fbd7722a9/server |
| 4 | + |
| 5 | +# default hostname |
| 6 | +HOST=localhost |
| 7 | +# default port number |
| 8 | +PORT=8080 |
| 9 | +# script name |
| 10 | +NAME=${0##*/} |
| 11 | + |
| 12 | +usage () { |
| 13 | + cat <<EOF |
| 14 | +
|
| 15 | + $NAME (PHP built-in web server manager) Version 0.2.0 |
| 16 | + PHP builtin server manager on port $PORT |
| 17 | +
|
| 18 | + usage: ./$NAME <command> [<hostname>:<port>] |
| 19 | +
|
| 20 | + Available commands: |
| 21 | +
|
| 22 | + start Starts PHP built-in web server server on specified hostname:port, default is localhost:$PORT |
| 23 | + stop Stops the PHP built-in web server |
| 24 | + restart Stops and Starts on previously specified hostname:port |
| 25 | + status Status of "$NAME" process |
| 26 | + log Show the PHP built-in web server logs. Use the -f option for a live update |
| 27 | +
|
| 28 | +
|
| 29 | + |
| 30 | + $NAME homepage: <https://github.com/cubny/php-built-in-server-manager> |
| 31 | +
|
| 32 | +EOF |
| 33 | +return 0 |
| 34 | +} |
| 35 | + |
| 36 | +setup_colors() { |
| 37 | + |
| 38 | +if which tput >/dev/null 2>&1; then |
| 39 | + ncolors=$(tput colors) |
| 40 | + fi |
| 41 | + if [ -t 1 ] && [ -n "$ncolors" ] && [ "$ncolors" -ge 8 ]; then |
| 42 | + RED="$(tput setaf 1)" |
| 43 | + GREEN="$(tput setaf 2)" |
| 44 | + YELLOW="$(tput setaf 3)" |
| 45 | + BLUE="$(tput setaf 4)" |
| 46 | + BOLD="$(tput bold)" |
| 47 | + NORMAL="$(tput sgr0)" |
| 48 | + else |
| 49 | + RED="" |
| 50 | + GREEN="" |
| 51 | + YELLOW="" |
| 52 | + BLUE="" |
| 53 | + BOLD="" |
| 54 | + NORMAL="" |
| 55 | + fi |
| 56 | +} |
| 57 | + |
| 58 | +# if no command specified exit and show usage |
| 59 | +if [[ $# < 1 ]]; then |
| 60 | + echo $NAME: no command specified |
| 61 | + usage |
| 62 | + exit 1 |
| 63 | +fi |
| 64 | + |
| 65 | +# if hostname:port specified override defaults |
| 66 | +if [[ $# > 1 ]]; then |
| 67 | + IFS=':' read -r -a hostport <<< "$2" |
| 68 | + if [[ ! -z "${hostport[0]}" ]]; then |
| 69 | + HOST=${hostport[0]} |
| 70 | + fi |
| 71 | + if [[ ! -z "${hostport[1]}" ]]; then |
| 72 | + PORT=${hostport[1]} |
| 73 | + fi |
| 74 | +fi |
| 75 | + |
| 76 | +# pidfile contents would be hostname:port:pid |
| 77 | +PIDFILE=tests/server.pid |
| 78 | +LOGFILE=tests/server.log |
| 79 | + |
| 80 | +validate_server () { |
| 81 | + which php &> /dev/null |
| 82 | + if [[ $? -eq 1 ]]; then |
| 83 | + printf "${YELLOW}Error: PHP not found. ${NORMAL}Please install PHP version 5.4 or greater!\n" |
| 84 | + return 1 |
| 85 | + fi |
| 86 | + |
| 87 | + php -h | grep -q -- '-S' |
| 88 | + if [[ $? -eq 1 ]]; then |
| 89 | + printf "${YELLOW}Error: PHP version must be 5.4 or greater!${NORMAL}\n" |
| 90 | + return 1 |
| 91 | + fi |
| 92 | + |
| 93 | + return 0 |
| 94 | +} |
| 95 | + |
| 96 | +start_server () { |
| 97 | + validate_server |
| 98 | + |
| 99 | + if [[ $? -eq 1 ]]; then |
| 100 | + return 1 |
| 101 | + fi |
| 102 | + |
| 103 | + if [[ -e "$PIDFILE" ]]; then |
| 104 | + printf "${YELLOW}Server seems to be running!${NORMAL}\n" |
| 105 | + echo |
| 106 | + echo if not, there is probably a zombie "$PIDFILE" in this directory. |
| 107 | + echo if you are sure no server is running just remove "$PIDFILE" manually and start again |
| 108 | + return 1 |
| 109 | + else |
| 110 | + printf "${GREEN}"$NAME" started on $HOST:$PORT${NORMAL}\n" |
| 111 | + php -S "$HOST":"$PORT" -c tests/php.ini >> "$LOGFILE" 2>&1 & |
| 112 | + echo "$HOST":"$PORT":$! > $PIDFILE |
| 113 | + return 0 |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +read_pidfile() { |
| 118 | + if [[ -e "$PIDFILE" ]]; then |
| 119 | + PIDFILECONTENT=`cat "$PIDFILE"` |
| 120 | + IFS=: read HOST PORT PID <<< "$PIDFILECONTENT:" |
| 121 | + return 0 |
| 122 | + else |
| 123 | + return 1 |
| 124 | + fi |
| 125 | +} |
| 126 | + |
| 127 | +stop_server () { |
| 128 | + if read_pidfile; then |
| 129 | + kill -9 "$PID" |
| 130 | + rm -f "$PIDFILE" |
| 131 | + printf "${GREEN}"$NAME" stopped!${NORMAL}\n" |
| 132 | + return 0 |
| 133 | + else |
| 134 | + printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" |
| 135 | + return 1 |
| 136 | + fi |
| 137 | +} |
| 138 | + |
| 139 | +status_server() { |
| 140 | + if read_pidfile && kill -0 "$PID" ; then |
| 141 | + printf "${BLUE}"$NAME" is running on ${HOST}:${PORT}${NORMAL}\n" |
| 142 | + else |
| 143 | + printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" |
| 144 | + fi |
| 145 | +} |
| 146 | + |
| 147 | + |
| 148 | +log_server() { |
| 149 | + if read_pidfile && kill -0 "$PID" ; then |
| 150 | + if [[ "$1" = "-f" ]]; then |
| 151 | + TAIL_OPTS="-f" |
| 152 | + fi |
| 153 | + tail $TAIL_OPTS "$LOGFILE" |
| 154 | + else |
| 155 | + printf "${YELLOW}"$NAME" is not running!${NORMAL}\n" |
| 156 | + fi |
| 157 | +} |
| 158 | + |
| 159 | + |
| 160 | +setup_colors |
| 161 | + |
| 162 | +case $1 in |
| 163 | + start) start_server;; |
| 164 | + stop) stop_server;; |
| 165 | + restart) stop_server; start_server ;; |
| 166 | + status) status_server;; |
| 167 | + log) log_server $2;; |
| 168 | + -h) usage ;; |
| 169 | + --help) usage ;; |
| 170 | + *) usage;; |
| 171 | +esac |
0 commit comments