Skip to content
Merged
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
11 changes: 11 additions & 0 deletions compiled_starters/php/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

# (This file is empty since PHP programs don't use a compile step)
11 changes: 11 additions & 0 deletions compiled_starters/php/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec php app/main.php "$@"
1 change: 1 addition & 0 deletions compiled_starters/php/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
37 changes: 37 additions & 0 deletions compiled_starters/php/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png)

This is a starting point for PHP solutions to the
["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview).

[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the
protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server
that is capable of serving multiple clients.

Along the way you'll learn about TCP servers,
[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html),
and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your HTTP server implementation is in `app/main.php`. Study
and uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `php (8.5)` installed locally
1. Run `./your_program.sh` to run your program, which is implemented in
`app/main.php`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
19 changes: 19 additions & 0 deletions compiled_starters/php/app/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
error_reporting(E_ALL);

// You can use print statements as follows for debugging, they'll be visible when running tests.
fwrite(STDERR, "Logs from your program will appear here!\n");

// TODO: Uncomment the code below to pass the first stage
//
// $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//
// // Since the tester restarts your program quite often, setting SO_REUSEADDR
// // ensures that we don't run into 'Address already in use' errors
// socket_set_option($serverSocket, SOL_SOCKET, SO_REUSEADDR, 1);
//
// socket_bind($serverSocket, "localhost", 4221);
// socket_listen($serverSocket);
// socket_accept($serverSocket);
//
// socket_close($serverSocket);
11 changes: 11 additions & 0 deletions compiled_starters/php/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the PHP version used to run your code
# on Codecrafters.
#
# Available versions: php-8.5
buildpack: php-8.5
15 changes: 15 additions & 0 deletions compiled_starters/php/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec php app/main.php "$@"
1 change: 1 addition & 0 deletions course-definition.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ languages:
- slug: "java"
- slug: "javascript"
- slug: "kotlin"
- slug: "php"
- slug: "python"
- slug: "ruby"
- slug: "rust"
Expand Down
7 changes: 7 additions & 0 deletions dockerfiles/php-8.5.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# syntax=docker/dockerfile:1.7-labs
FROM php:8.5.0-cli-alpine3.22

# For ext-sockets installation
RUN apk add linux-headers=~6.14.2-r0 --no-cache

RUN docker-php-ext-install pcntl sockets
11 changes: 11 additions & 0 deletions solutions/php/01-at4/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

# (This file is empty since PHP programs don't use a compile step)
11 changes: 11 additions & 0 deletions solutions/php/01-at4/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec php app/main.php "$@"
1 change: 1 addition & 0 deletions solutions/php/01-at4/code/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto
37 changes: 37 additions & 0 deletions solutions/php/01-at4/code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
![progress-banner](https://codecrafters.io/landing/images/default_progress_banners/http-server.png)

This is a starting point for PHP solutions to the
["Build Your Own HTTP server" Challenge](https://app.codecrafters.io/courses/http-server/overview).

[HTTP](https://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol) is the
protocol that powers the web. In this challenge, you'll build a HTTP/1.1 server
that is capable of serving multiple clients.

Along the way you'll learn about TCP servers,
[HTTP request syntax](https://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html),
and more.

**Note**: If you're viewing this repo on GitHub, head over to
[codecrafters.io](https://codecrafters.io) to try the challenge.

# Passing the first stage

The entry point for your HTTP server implementation is in `app/main.php`. Study
and uncomment the relevant code, and push your changes to pass the first stage:

```sh
git commit -am "pass 1st stage" # any msg
git push origin master
```

Time to move on to the next stage!

# Stage 2 & beyond

Note: This section is for stages 2 and beyond.

1. Ensure you have `php (8.5)` installed locally
1. Run `./your_program.sh` to run your program, which is implemented in
`app/main.php`.
1. Commit your changes and run `git push origin master` to submit your solution
to CodeCrafters. Test output will be streamed to your terminal.
14 changes: 14 additions & 0 deletions solutions/php/01-at4/code/app/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
error_reporting(E_ALL);

$serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
socket_set_option($serverSocket, SOL_SOCKET, SO_REUSEADDR, 1);

socket_bind($serverSocket, "localhost", 4221);
socket_listen($serverSocket);
socket_accept($serverSocket);

socket_close($serverSocket);
11 changes: 11 additions & 0 deletions solutions/php/01-at4/code/codecrafters.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Set this to true if you want debug logs.
#
# These can be VERY verbose, so we suggest turning them off
# unless you really need them.
debug: false

# Use this to change the PHP version used to run your code
# on Codecrafters.
#
# Available versions: php-8.5
buildpack: php-8.5
15 changes: 15 additions & 0 deletions solutions/php/01-at4/code/your_program.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
#
# Use this script to run your program LOCALLY.
#
# Note: Changing this script WILL NOT affect how CodeCrafters runs your program.
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit early if any commands fail

# Copied from .codecrafters/run.sh
#
# - Edit this to change how your program runs locally
# - Edit .codecrafters/run.sh to change how your program runs remotely
exec php app/main.php "$@"
32 changes: 32 additions & 0 deletions solutions/php/01-at4/diff/app/main.php.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
@@ -1,19 +1,14 @@
<?php
error_reporting(E_ALL);

-// You can use print statements as follows for debugging, they'll be visible when running tests.
-fwrite(STDERR, "Logs from your program will appear here!\n");
+$serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

-// TODO: Uncomment the code below to pass the first stage
-//
-// $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
-//
-// // Since the tester restarts your program quite often, setting SO_REUSEADDR
-// // ensures that we don't run into 'Address already in use' errors
-// socket_set_option($serverSocket, SOL_SOCKET, SO_REUSEADDR, 1);
-//
-// socket_bind($serverSocket, "localhost", 4221);
-// socket_listen($serverSocket);
-// socket_accept($serverSocket);
-//
-// socket_close($serverSocket);
\ No newline at end of file
+// Since the tester restarts your program quite often, setting SO_REUSEADDR
+// ensures that we don't run into 'Address already in use' errors
+socket_set_option($serverSocket, SOL_SOCKET, SO_REUSEADDR, 1);
+
+socket_bind($serverSocket, "localhost", 4221);
+socket_listen($serverSocket);
+socket_accept($serverSocket);
+
+socket_close($serverSocket);
\ No newline at end of file
11 changes: 11 additions & 0 deletions starter_templates/php/code/.codecrafters/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to compile your program on CodeCrafters
#
# This runs before .codecrafters/run.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

# (This file is empty since PHP programs don't use a compile step)
11 changes: 11 additions & 0 deletions starter_templates/php/code/.codecrafters/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/sh
#
# This script is used to run your program on CodeCrafters
#
# This runs after .codecrafters/compile.sh
#
# Learn more: https://codecrafters.io/program-interface

set -e # Exit on failure

exec php app/main.php "$@"
19 changes: 19 additions & 0 deletions starter_templates/php/code/app/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
error_reporting(E_ALL);

// You can use print statements as follows for debugging, they'll be visible when running tests.
fwrite(STDERR, "Logs from your program will appear here!\n");

// TODO: Uncomment the code below to pass the first stage
//
// $serverSocket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
//
// // Since the tester restarts your program quite often, setting SO_REUSEADDR
// // ensures that we don't run into 'Address already in use' errors
// socket_set_option($serverSocket, SOL_SOCKET, SO_REUSEADDR, 1);
//
// socket_bind($serverSocket, "localhost", 4221);
// socket_listen($serverSocket);
// socket_accept($serverSocket);
//
// socket_close($serverSocket);
3 changes: 3 additions & 0 deletions starter_templates/php/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
attributes:
required_executable: "php (8.5)"
user_editable_file: "app/main.php"
Loading