Skip to content

Commit b40526c

Browse files
improve for package
publish
1 parent 6251ce4 commit b40526c

File tree

3 files changed

+44
-345
lines changed

3 files changed

+44
-345
lines changed

README.md

Lines changed: 32 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
# PHP HTTP Server
1+
# PHP HTTP Server [![Latest Version on Packagist](https://img.shields.io/packagist/v/arishem/php-http-server.svg?style=flat-square)](https://packagist.org/packages/arishem/php-http-server)
32

43
A lightweight, dependency-free HTTP server written in PHP. This server supports basic routing, middleware, and WebSocket integration, and is designed to be modular and easy to extend.
54

@@ -18,7 +17,7 @@ A lightweight, dependency-free HTTP server written in PHP. This server supports
1817

1918
### Prerequisites
2019

21-
- PHP 7.4 or higher.
20+
- PHP 8.3 or higher.
2221
- Basic knowledge of PHP and HTTP.
2322

2423
### Installation
@@ -58,81 +57,43 @@ To start the server, run:
5857

5958
The server will listen on `http://localhost:8080` by default. You can customize the host and port by modifying the `Server` constructor in `public/index.php`.
6059

61-
---
62-
63-
### Handling Requests
64-
65-
The server parses incoming HTTP requests into a `Request` object, which provides the following methods:
66-
67-
- `getMethod()`: Returns the HTTP method (e.g., `GET`, `POST`).
68-
- `getUri()`: Returns the request URI (e.g., `/`, `/users/123`).
69-
- `getHeaders()`: Returns an associative array of request headers.
70-
- `getBody()`: Returns the request body (if any).
71-
72-
Example:
73-
74-
```php
75-
$request = new Request($rawRequest);
76-
echo "Method: " . $request->getMethod() . "\n";
77-
echo "URI: " . $request->getUri() . "\n";
78-
echo "Headers: " . print_r($request->getHeaders(), true) . "\n";
79-
echo "Body: " . $request->getBody() . "\n";
80-
```
81-
82-
---
83-
84-
### Sending Responses
85-
86-
The server uses a `Response` object to send HTTP responses. The `Response` class provides the following methods:
87-
88-
- `setStatusCode(int $statusCode)`: Sets the HTTP status code (e.g., `200`, `404`).
89-
- `setHeader(string $name, string $value)`: Adds a header to the response.
90-
- `setBody(string $body)`: Sets the response body.
91-
- `send(resource $conn)`: Sends the response to the client.
92-
93-
Example:
94-
95-
```php
96-
$response = new Response();
97-
$response->setStatusCode(200)
98-
->setHeader('Content-Type', 'text/plain')
99-
->setBody('Hello, World!')
100-
->send($conn);
101-
```
102-
103-
---
104-
105-
### Example Workflow
106-
107-
1. The server accepts a connection and reads the raw HTTP request.
108-
2. The raw request is parsed into a `Request` object.
109-
3. The server processes the request and creates a `Response` object.
110-
4. The response is sent back to the client, and the connection is closed.
60+
The WebSocket server, if configured, will listen on `ws://localhost:8081` by default.
11161

11262
---
11363

11464
## Directory Structure
11565

11666
```
11767
php-http-server/
118-
├── src/
119-
│ ├── Core/
120-
│ │ ├── Server.php # Main HTTP server logic
121-
│ │ ├── Request.php # HTTP request class
122-
│ │ ├── Response.php # HTTP response class
123-
│ │ ├── Router.php # HTTP response class
124-
│ │ └── RouterInterface.php # Router interface
125-
│ ├── WebSocket/
126-
│ │ ├── WebSocketServer.php # WebSocket server logic
127-
│ │ └── WebSocketInterface.php # WebSocket frame handling logic
128-
│ ├── Middleware/
129-
│ │ ├── MiddlewareInterface.php # Middleware contract
130-
│ │ ├── ModifyRequestResponseMiddleware.php # Middleware to modify Request and Response
131-
│ │ └── ExampleMiddleware.php # Example middleware
132-
├── public/
133-
│ └── index.php # Entry point for the server
134-
├── composer.json # Composer configuration (optional, for autoloading)
135-
└── README.md # Project documentation
68+
├── src/ # Source code directory
69+
│ ├── Core/ # Core server logic
70+
│ │ ├── Server.php # Main HTTP server logic
71+
│ │ ├── Request.php # HTTP request class
72+
│ │ ├── Response.php # HTTP response class
73+
│ │ ├── Router.php # HTTP routing logic
74+
│ │ └── RouterInterface.php # Router interface
75+
│ ├── WebSocket/ # WebSocket server implementation
76+
│ │ ├── WebSocketServer.php # WebSocket server logic
77+
│ │ └── WebSocketInterface.php # WebSocket frame handling logic
78+
│ ├── Middleware/ # Middleware components
79+
│ │ ├── MiddlewareInterface.php # Middleware contract
80+
│ │ ├── ModifyRequestResponseMiddleware.php # Middleware to modify Request and Response
81+
│ │ └── ExampleMiddleware.php # Example middleware
82+
├── public/ # Public directory (entry point for the server)
83+
│ └── index.php # Entry point for the server
84+
├── tests/ # Test cases directory
85+
│ ├── Core/ # Tests for core components
86+
│ │ ├── RequestTest.php # Unit test for Request
87+
│ │ ├── ResponseTest.php # Unit test for Response
88+
│ │ ├── RouterTest.php # Unit test for Router
89+
│ │ └── test_template.php # Test template
90+
│ ├── Cache/ # Cache-related tests
91+
│ │ ├── test_cache # Cache test cases
92+
│ │ └── CacheTest.php # Unit test for cache functionality
93+
│ └── Middleware/ # Middleware-related tests
94+
│ └── TestMiddleware.php # Example middleware test
95+
├── composer.json # Composer configuration (optional, for autoloading)
96+
└── README.md # Project documentation
13697
```
13798

13899
---

0 commit comments

Comments
 (0)