|
#!/usr/bin/env bash |
|
|
|
# Highly opinionated script for setting up a full development environment for PHP on macOS |
|
# Author: @thomas-alrek |
|
|
|
launchconfig=$(cat << 'EOF' |
|
[ |
|
{ |
|
"name": "Listen for Xdebug", |
|
"type": "php", |
|
"request": "launch", |
|
"port": 9003 |
|
}, |
|
{ |
|
"name": "Launch currently open script", |
|
"type": "php", |
|
"request": "launch", |
|
"program": "${file}", |
|
"cwd": "${fileDirname}", |
|
"port": 0, |
|
"runtimeArgs": [ |
|
"-dxdebug.start_with_request=yes" |
|
], |
|
"env": { |
|
"XDEBUG_MODE": "debug,develop", |
|
"XDEBUG_CONFIG": "client_port=${port}" |
|
} |
|
}, |
|
{ |
|
"name": "Launch Built-in web server", |
|
"type": "php", |
|
"request": "launch", |
|
"runtimeArgs": [ |
|
"-dxdebug.mode=debug", |
|
"-dxdebug.start_with_request=yes", |
|
"-S", |
|
"localhost:0" |
|
], |
|
"program": "", |
|
"cwd": "${workspaceRoot}", |
|
"port": 9003, |
|
"serverReadyAction": { |
|
"pattern": "Development Server \\(http://localhost:([0-9]+)\\) started", |
|
"uriFormat": "http://localhost:%s", |
|
"action": "openExternally" |
|
} |
|
} |
|
] |
|
EOF |
|
); |
|
|
|
function command_exists { |
|
if ! command -v $1 &> /dev/null |
|
then |
|
return 1; |
|
exit |
|
fi |
|
|
|
return 0; |
|
} |
|
|
|
function check_os { |
|
if ! command_exists uname; then |
|
echo "Error: Unable to determine operating system"; |
|
exit 1; |
|
fi |
|
|
|
if [[ $(uname -s) != "Darwin" ]]; then |
|
echo "Error: This script is only intended to run on macOS"; |
|
exit 1; |
|
fi |
|
} |
|
|
|
function check_curl { |
|
if ! command_exists curl; then |
|
echo "Error: curl is required to run this script"; |
|
exit 1; |
|
fi |
|
} |
|
|
|
function install_homebrew { |
|
if ! command_exists brew; then |
|
echo "Installing homebrew..."; |
|
/usr/bin/env bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" > /dev/null 2>&1; |
|
fi |
|
} |
|
|
|
function install_xdebug { |
|
if command_exists pecl; then |
|
if ! php -m -c 2>/dev/null | grep Xdebug > /dev/null 2>&1; then |
|
echo "Installing xdebug..."; |
|
pecl install xdebug > /dev/null 2>&1; |
|
fi |
|
phpini="$(php-config --ini-path)/php.ini"; |
|
if ! cat $phpini | grep "xdebug.so" > /dev/null 2>&1; then |
|
echo 'zend_extension="xdebug.so"' >> $phpini; |
|
echo 'xdebug.mode = debug' >> $phpini; |
|
echo 'xdebug.start_with_request = yes' >> $phpini; |
|
echo 'xdebug.log_level = 0' >> $phpini; |
|
fi |
|
fi; |
|
} |
|
|
|
function install_jq { |
|
if ! command_exists jq; then |
|
echo "Installing jq..."; |
|
brew install jq > /dev/null 2>&1; |
|
fi |
|
} |
|
|
|
function install_php { |
|
if ! command_exists php; then |
|
echo "Installing php..."; |
|
brew install php > /dev/null 2>&1; |
|
fi |
|
} |
|
|
|
function install_composer { |
|
if ! command_exists composer; then |
|
echo "Installing composer..."; |
|
brew install composer > /dev/null 2>&1; |
|
fi |
|
} |
|
|
|
function install_phpstan { |
|
if ! test -f ~/.composer/vendor/bin/phpstan; then |
|
echo "Installing phpstan..."; |
|
composer global require phpstan/phpstan > /dev/null 2>&1; |
|
fi |
|
} |
|
|
|
function install_vscode_extension { |
|
echo "Installing vscode extension $1..."; |
|
execute_vscode_command --install-extension $1; |
|
} |
|
|
|
function install_vscode_extensions { |
|
install_vscode_extension faelv.composer-companion; |
|
install_vscode_extension bmewburn.vscode-intelephense-client; |
|
set_vscode_config intelephense.environment.phpVersion "\"$(php-config --version)\""; |
|
install_vscode_extension ssigwart.php-extra-completions; |
|
install_vscode_extension xdebug.php-debug; |
|
install_vscode_extension SanderRonde.phpstan-vscode; |
|
set_vscode_config phpstan.binPath "\"$HOME/.composer/vendor/bin/phpstan\""; |
|
set_vscode_config phpstan.enabled true; |
|
set_vscode_config editor.formatOnPaste true; |
|
set_vscode_config editor.formatOnSave true; |
|
install_vscode_extension cjhowe7.laravel-blade; |
|
install_vscode_extension amiralizadeh9480.laravel-extra-intellisense; |
|
} |
|
|
|
function write_vscode_config { |
|
config_path="$HOME/Library/Application Support/Code/User/settings.json" |
|
if ! test -f ~/.composer/vendor/bin/phpstan; then |
|
touch "$config_path"; |
|
fi |
|
if ! grep -q '[^[:space:]]' < "$config_path"; then |
|
echo "{}" > "$config_path"; |
|
fi |
|
updated_config=$(jq "$1" "$config_path"); |
|
} |
|
|
|
function set_vscode_config { |
|
echo "Setting config \"$1\" to $2..."; |
|
write_vscode_config ".$1 = $2" |
|
} |
|
|
|
function configure_vscode { |
|
install_jq; |
|
install_vscode_extensions; |
|
set_vscode_config editor.defaultFormatter "\"vscode.php-language-features\"" |
|
set_vscode_config launch.version "\"0.2.0\""; |
|
echo "Setting vscode launch configuration..." |
|
write_vscode_config ".launch.configurations = $launchconfig" |
|
} |
|
|
|
function install_vscode { |
|
if ! test -f /Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron; then |
|
echo "Installing vscode..."; |
|
brew install --cask visual-studio-code > /dev/null 2>&1; |
|
fi |
|
|
|
configure_vscode; |
|
} |
|
|
|
function execute_vscode_command { |
|
if command_exists code; then |
|
code "${@:1}" > /dev/null 2>&1 |
|
return 0 |
|
fi |
|
|
|
ELECTRON_RUN_AS_NODE=1 |
|
/Applications/Visual\ Studio\ Code.app/Contents/MacOS/Electron "${@:1}" --ms-enable-electron-run-as-node > /dev/null 2>&1 |
|
return 0 |
|
} |
|
|
|
function install { |
|
check_os; |
|
check_curl; |
|
install_homebrew; |
|
install_php; |
|
install_xdebug; |
|
install_composer; |
|
install_phpstan; |
|
install_vscode; |
|
} |
|
|
|
install; |
|
|
|
exit 0; |