Skip to content

Commit 02f7f19

Browse files
authored
Merge pull request #150 from codecrafters-io/andy/add-php
Add PHP support
2 parents 99f489d + fd57099 commit 02f7f19

File tree

21 files changed

+289
-0
lines changed

21 files changed

+289
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
# (This file is empty since PHP programs don't use a compile step)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to run your program on CodeCrafters
4+
#
5+
# This runs after .codecrafters/compile.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
exec php app/main.php "$@"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* text=auto

compiled_starters/php/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png)
2+
3+
This is a starting point for PHP solutions to the
4+
["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview).
5+
6+
[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the
7+
protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server
8+
that is capable of serving multiple clients.
9+
10+
Along the way you'll learn about TCP servers,
11+
[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html),
12+
and more.
13+
14+
**Note**: If you're viewing this repo on GitHub, head over to
15+
[codecrafters.io](https://codecrafters.io) to try the challenge.
16+
17+
# Passing the first stage
18+
19+
The entry point for your HTTP server implementation is in `app/main.php`. Study
20+
and uncomment the relevant code, and push your changes to pass the first stage:
21+
22+
```sh
23+
git commit -am "pass 1st stage" # any msg
24+
git push origin master
25+
```
26+
27+
Time to move on to the next stage!
28+
29+
# Stage 2 & beyond
30+
31+
Note: This section is for stages 2 and beyond.
32+
33+
1. Ensure you have `php (8.5)` installed locally
34+
1. Run `./your_program.sh` to run your program, which is implemented in
35+
`app/main.php`.
36+
1. Commit your changes and run `git push origin master` to submit your solution
37+
to CodeCrafters. Test output will be streamed to your terminal.

compiled_starters/php/app/main.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
error_reporting(E_ALL);
3+
4+
// You can use print statements as follows for debugging, they'll be visible when running tests.
5+
fwrite(STDERR, "Logs from your program will appear here!\n");
6+
7+
// TODO: Uncomment the code below to pass the first stage
8+
//
9+
// $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
10+
//
11+
// // Since the tester restarts your program quite often, setting SO_REUSEADDR
12+
// // ensures that we don't run into 'Address already in use' errors
13+
// socket_set_option($serverSocket, SOL_SOCKET, SO_REUSEADDR, 1);
14+
//
15+
// socket_bind($serverSocket, "localhost", 4221);
16+
// socket_listen($serverSocket);
17+
// socket_accept($serverSocket);
18+
//
19+
// socket_close($serverSocket);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Set this to true if you want debug logs.
2+
#
3+
# These can be VERY verbose, so we suggest turning them off
4+
# unless you really need them.
5+
debug: false
6+
7+
# Use this to change the PHP version used to run your code
8+
# on Codecrafters.
9+
#
10+
# Available versions: php-8.5
11+
buildpack: php-8.5
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/sh
2+
#
3+
# Use this script to run your program LOCALLY.
4+
#
5+
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit early if any commands fail
10+
11+
# Copied from .codecrafters/run.sh
12+
#
13+
# - Edit this to change how your program runs locally
14+
# - Edit .codecrafters/run.sh to change how your program runs remotely
15+
exec php app/main.php "$@"

course-definition.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ languages:
7575
- slug: "java"
7676
- slug: "javascript"
7777
- slug: "kotlin"
78+
- slug: "php"
7879
- slug: "python"
7980
- slug: "ruby"
8081
- slug: "rust"

dockerfiles/php-8.5.Dockerfile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# syntax=docker/dockerfile:1.7-labs
2+
FROM php:8.5.0-cli-alpine3.22
3+
4+
# For ext-sockets installation
5+
RUN apk add linux-headers=~6.14.2-r0 --no-cache
6+
7+
RUN docker-php-ext-install pcntl sockets
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/sh
2+
#
3+
# This script is used to compile your program on CodeCrafters
4+
#
5+
# This runs before .codecrafters/run.sh
6+
#
7+
# Learn more: https://codecrafters.io/program-interface
8+
9+
set -e # Exit on failure
10+
11+
# (This file is empty since PHP programs don't use a compile step)

0 commit comments

Comments
 (0)