Skip to content

Commit c8506da

Browse files
committed
Add initial codebase
Created the project's initial codebase covering requested features. It includes PHP classes to handle HTTP requests and responses, and several supporting classes like enumeration classes for HTTP methods, status, and schemes. Accompanied by exception classes to manage unexpected cases. Also, includes tests for testing these features along with setup files for Github actions and PHPStan.
0 parents  commit c8506da

19 files changed

+986
-0
lines changed

.github/workflows/php.yml

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: PHP CI/CD Pipeline
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
11+
jobs:
12+
build:
13+
14+
runs-on: ubuntu-latest
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v2
19+
20+
- name: Setup PHP
21+
uses: shivammathur/setup-php@v2
22+
with:
23+
php-version: '8.3'
24+
extensions: mbstring, xml, ctype, iconv, intl, pdo_sqlite
25+
coverage: xdebug
26+
27+
- name: Validate composer.json and composer.lock
28+
run: composer validate
29+
30+
- name: Install dependencies
31+
run: composer install --prefer-dist --no-progress
32+
33+
- name: Run tests
34+
run: ./vendor/bin/phpunit tests
35+
36+
# If you have a deployment step, you can include it here.
37+
# This could be pushing to a Docker registry, deploying to a cloud platform, etc.
38+
# - name: Deploy...

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.dev/
2+
.idea/
3+
composer.lock
4+
vendor/
5+
.phpunit.result.cache

.run/phpmd.run.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="phpmd" type="PhpLocalRunConfigurationType" factoryName="PHP Console" path="$PROJECT_DIR$/vendor/bin/phpmd" scriptParameters="../../src ansi cleancode,codesize,controversial,design,naming,unusedcode">
3+
<method v="2" />
4+
</configuration>
5+
</component>

.run/phpstan.run.xml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="phpstan" type="PhpLocalRunConfigurationType" factoryName="PHP Console" path="$PROJECT_DIR$/vendor/bin/phpstan" scriptParameters="analyse -c ..\..\phpstan.neon">
3+
<method v="2" />
4+
</configuration>
5+
</component>

LICENSE.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# License
2+
3+
This project is licensed under the terms of the MIT license.
4+
5+
The MIT License (MIT)
6+
7+
Copyright (c) 2023 [CommonPHP.org](https://www.commonphp.org/)
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in all
17+
copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
SOFTWARE.

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# CommonPHP Web Library
2+
3+
The CommonPHP Web Library provides a suite of components designed to streamline the development of web applications by offering a standardized, easy-to-use set of utilities for handling HTTP requests and responses, along with robust exception handling. Inspired by the flexibility of PHP and the need for more straightforward, decoupled components in modern web development, this library aims to offer PHP developers a reliable toolkit for enhancing their web projects.
4+
5+
## Features
6+
7+
- **HTTP Request Handling**: Simplifies the process of managing HTTP requests, including methods, schemes, headers, and cookies.
8+
- **HTTP Response Management**: Offers a structured way to create and manage HTTP responses, including status codes, headers, and body content.
9+
- **Exception Handling**: Provides specialized exceptions to handle common web development issues, improving the debuggability and reliability of your application.
10+
- **Support Classes**: Includes enums for request methods, schemes, and response statuses, making your code more readable and maintainable.
11+
12+
## Installation
13+
14+
You can install the CommonPHP Web Library using Composer:
15+
16+
```bash
17+
composer require comphp/web
18+
```
19+
20+
Replace `your/package-name` with the actual package name of the CommonPHP Web Library on Packagist.
21+
22+
## Usage
23+
24+
### Handling Requests
25+
26+
To handle an HTTP request, you can easily instantiate a `Request` object:
27+
28+
```php
29+
use CommonPHP\Web\Request;
30+
31+
$request = Request::fromRequest();
32+
```
33+
34+
This will automatically populate the `Request` object with details from the current HTTP request, including method, scheme, headers, and any parameters.
35+
36+
### Creating Responses
37+
38+
To create an HTTP response, you can use the `Response` class:
39+
40+
```php
41+
use CommonPHP\Web\Response;
42+
use CommonPHP\Web\Support\ResponseStatus;
43+
44+
$response = new Response(
45+
body: 'Hello, world!',
46+
status: ResponseStatus::SUCCESS_OK,
47+
headers: ['Content-Type' => 'text/plain']
48+
);
49+
50+
$response->send();
51+
```
52+
53+
This will send a response with the specified body, status code, and headers.
54+
55+
### Exception Handling
56+
57+
The library includes several exceptions designed to handle common errors in web development:
58+
59+
- `UndefinedRequestMethodException`
60+
- `UndefinedRequestSchemeException`
61+
- `UndefinedResponseStatusCodeException`
62+
63+
These can be used to catch and respond to errors more effectively in your application.
64+
65+
## Contributing
66+
67+
We welcome contributions from the community, whether it's through submitting bug reports, proposing enhancements, or creating pull requests. Please refer to our CONTRIBUTING.md file for more details on how to contribute to the CommonPHP Web Library.
68+
69+
## License
70+
71+
This library is licensed under the [MIT License](LICENSE.md).

composer.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "comphp/web",
3+
"type": "library",
4+
"description": "Provides base functionality for web-based applications",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "timothy.mcclatchey",
9+
"email": "timothy@commonphp.org"
10+
}
11+
],
12+
"autoload": {
13+
"psr-4": {
14+
"CommonPHP\\Web\\": "src/"
15+
}
16+
},
17+
"autoload-dev": {
18+
"psr-4": {
19+
"CommonPHP\\Tests\\Web\\": "tests/"
20+
}
21+
},
22+
"require": {
23+
"php": "^8.3"
24+
},
25+
"require-dev": {
26+
"phpunit/phpunit": "^10.5.9",
27+
"phpstan/phpstan": "^1.10.58",
28+
"phpmd/phpmd": "^2.13"
29+
}
30+
}

phpstan.neon

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: 9
3+
paths:
4+
- src
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
6+
namespace CommonPHP\Web\Exceptions;
7+
8+
use Throwable;
9+
10+
/**
11+
* Class UndefinedRequestMethodException
12+
*
13+
* This exception is thrown when an undefined HTTP request method is encountered.
14+
*/
15+
class UndefinedRequestMethodException extends WebException
16+
{
17+
public function __construct(string $requestMethod, ?Throwable $previous = null)
18+
{
19+
parent::__construct('The request method '.$requestMethod.' is not a known HTTP request method: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods', $previous);
20+
$this->code = 2303;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CommonPHP\Web\Exceptions;
6+
7+
use Throwable;
8+
9+
/**
10+
* Class UndefinedRequestSchemeException
11+
*
12+
* Exception that is thrown when the provided request scheme is not a known HTTP request scheme.
13+
* This class extends the WebException class.
14+
*/
15+
class UndefinedRequestSchemeException extends WebException
16+
{
17+
public function __construct(string $requestScheme, ?Throwable $previous = null)
18+
{
19+
parent::__construct('The request scheme '.$requestScheme.' is not a known HTTP request scheme: http,https', $previous);
20+
$this->code = 2302;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CommonPHP\Web\Exceptions;
6+
7+
use Throwable;
8+
9+
/**
10+
* Exception thrown when an undefined HTTP response status code is encountered.
11+
*
12+
* This exception is thrown when an HTTP response status code is not recognized or not within the
13+
* standard HTTP status codes.
14+
*/
15+
class UndefinedResponseStatusCodeException extends WebException
16+
{
17+
public function __construct(int $status, ?Throwable $previous = null)
18+
{
19+
parent::__construct('The status '.$status.' is not a known HTTP status: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status', $previous);
20+
$this->code = 2301;
21+
}
22+
}

src/Exceptions/WebException.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace CommonPHP\Web\Exceptions;
6+
7+
use Exception;
8+
use Throwable;
9+
10+
/**
11+
* Class WebException
12+
*
13+
* This class represents an exception related to web operations.
14+
* It extends the base Exception class.
15+
*/
16+
class WebException extends Exception
17+
{
18+
public function __construct(string $message = "", ?Throwable $previous = null)
19+
{
20+
parent::__construct($message, 2300, $previous);
21+
}
22+
}

0 commit comments

Comments
 (0)