Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions php/.tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
php 8
39 changes: 25 additions & 14 deletions php/README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,34 @@
# PHP

Skeleton project for PHP with PHPUnit for testing.
Skeleton project for PHP.

## Usage (on MacOS or Linux)
- `./script/setup` to install dependencies using brew
- If this doesn't work install PHP CLI and [Composer](https://getcomposer.org/) manually
- `./script/test` to run the tests
- `./script/start` to run the code
## Prerequisites

The toolchain this project needs is declared in [`.tool-versions`](./.tool-versions):

```
php 8
```

*Note: You need [brew](https://brew.sh/) installed for the setup script to work. If you haven't installed brew before it will require at least 5gb of space. Downloading these packages will take a long time, so please ensure you do so well in advance of the interview.*
Install those however you prefer, as long as they end up on your `PATH`. The quickest route is
[mise](https://mise.jdx.dev/), which reads the file for you:

## Usage (on Windows)
- `PowerShell.exe -ExecutionPolicy UnRestricted -File .\script\setup.ps1` to download temp install of php and composer dependencies
- If this doesn't work install [PHP](https://www.php.net/manual/en/install.windows.php) and [Composer](https://getcomposer.org/) manually
- `php composer.phar test` to run the tests
- `php composer.phar start` to run the code
```sh
mise install
```

Note that mise compiles PHP from source, so it needs some system libraries to build against.
If `mise install` fails, run [`./script/system-packages`](./script/system-packages) to install
them and try again. You can skip this if you install PHP from a package manager instead
(`apt install php`, `brew install php`), as that comes pre-built.

## Usage
- `./script/setup` to fetch dependencies
- `./script/test` to run the tests
- `./script/start` to run the app

## Structure
- Code located in [`code.php`](./code.php)
- Tests located in [`tests.php`](./tests.php)
- Tests located in [`Tests.php`](./Tests.php)

However, you're free to organise your code as you like.
However, you're free to organise your code as you like.
3 changes: 2 additions & 1 deletion php/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
},
"scripts": {
"start": "php code.php",
"test": "phpunit Tests.php"
"test": "vendor/bin/phpunit Tests.php"
}
}

22 changes: 18 additions & 4 deletions php/script/setup
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
#!/usr/bin/env bash

set -e
set -euo pipefail

brew install php
brew install phpunit
brew install composer
cd "$(dirname "${BASH_SOURCE[0]}")/.."

# Bootstrap composer locally if not present
if [[ ! -f composer.phar ]]; then
EXPECTED_SIG="$(curl -fsSL https://composer.github.io/installer.sig)"
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
ACTUAL_SIG="$(php -r "echo hash_file('sha384', 'composer-setup.php');")"
if [[ "$EXPECTED_SIG" != "$ACTUAL_SIG" ]]; then
echo "Composer installer signature verification failed" >&2
rm -f composer-setup.php
exit 1
fi
php composer-setup.php
rm -f composer-setup.php
fi

php composer.phar install
27 changes: 0 additions & 27 deletions php/script/setup.ps1

This file was deleted.

6 changes: 4 additions & 2 deletions php/script/start
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -e
set -euo pipefail

composer run-script start
cd "$(dirname "${BASH_SOURCE[0]}")/.."

php composer.phar run-script start
92 changes: 92 additions & 0 deletions php/script/system-packages
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/env bash

# Installs the system libraries PHP is compiled against.
#
# You only need this if you are *building* PHP from source, which is what mise does.
# If you install PHP from a package manager (apt install php, brew install php) the
# packaging has already taken care of this and you can skip it.
#
# This is separate from `script/setup` because it has to run *before* the toolchain is
# built. By the time `setup` runs, PHP is already compiled and on your PATH.

set -euo pipefail

# Debian/Ubuntu package names, used by the CI runner and most devcontainers.
APT_PACKAGES=(
autoconf
bison
build-essential
libbz2-dev
libcurl4-openssl-dev
libfreetype6-dev
libgd-dev
libgmp-dev
libicu-dev
libjpeg-dev
libonig-dev
libpng-dev
libpq-dev
libreadline-dev
libsodium-dev
libsqlite3-dev
libssl-dev
libwebp-dev
libxml2-dev
libxslt1-dev
libzip-dev
pkg-config
re2c
zlib1g-dev
)

# macOS equivalents. Xcode Command Line Tools provide the compiler toolchain itself.
BREW_PACKAGES=(
autoconf
bison
freetype
gd
gettext
gmp
icu4c
jpeg
libiconv
libpng
libsodium
libxml2
libzip
oniguruma
pkg-config
re2c
webp
)

if command -v apt-get >/dev/null 2>&1; then
# Containers often run as root, without sudo installed.
if [[ "$(id -u)" -eq 0 ]]; then
sudo=()
elif command -v sudo >/dev/null 2>&1; then
sudo=(sudo)
else
echo "This script needs root to install packages, but sudo is not available." >&2
echo "Re-run it as root, or install these packages yourself:" >&2
printf ' %s\n' "${APT_PACKAGES[*]}" >&2
exit 1
fi

"${sudo[@]}" apt-get update --quiet
"${sudo[@]}" apt-get install --yes --no-install-recommends "${APT_PACKAGES[@]}"

elif command -v brew >/dev/null 2>&1; then
brew install "${BREW_PACKAGES[@]}"

else
echo "Could not find apt-get or brew, so this script cannot install packages for you." >&2
echo "" >&2
echo "PHP is compiled from source, and needs the development headers for roughly:" >&2
echo " autoconf bison libcurl libgd libicu libjpeg libpng libsodium libsqlite3" >&2
echo " libssl libxml2 libzip oniguruma pkg-config re2c zlib" >&2
echo "" >&2
echo "Install the equivalents for your system, or install PHP from your package" >&2
echo "manager instead of building it - see .tool-versions for the version needed." >&2
exit 1
fi
6 changes: 4 additions & 2 deletions php/script/test
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/usr/bin/env bash

set -e
set -euo pipefail

composer run-script test
cd "$(dirname "${BASH_SOURCE[0]}")/.."

php composer.phar run-script test
Loading