Skip to content

Commit 3c9d25b

Browse files
committed
Lib init
0 parents  commit 3c9d25b

13 files changed

Lines changed: 445 additions & 0 deletions

File tree

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Http
2+
SESSION_DURATION=0
3+
SESSION_KEEP_CONNECT=30
4+
SESSION_CACHE_LIMITER=private
5+
HTTP_EXPIRES=60

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.lock
3+
.env
4+
.history/*

LICENSE

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

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Http library (bubu framework)
2+
3+
## Installation with composer
4+
5+
```bash
6+
$ composer require bubu-framework/http
7+
```
8+
9+
### Configuration
10+
11+
Follow .env.example syntaxe
12+
13+
## Usage
14+
15+
___

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "bubu-framework/http",
3+
"description": "Http part (bubu framework)",
4+
"type": "library",
5+
"license": "MIT",
6+
"homepage": "https://github.com/bubu-framework/http",
7+
"authors": [
8+
{
9+
"name": "QuentinBubu",
10+
"email": "QuentinBubu.dev@gmail.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"Bubu\\Http\\": "src/"
16+
}
17+
},
18+
"autoload-dev": {
19+
"psr-4": {
20+
"Bubu\\Http\\Tests\\" : "tests/"
21+
}
22+
},
23+
"require": {
24+
"vlucas/phpdotenv": "^5.3"
25+
}
26+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Bubu\Http\HttpRequire\Exception;
4+
5+
class HttpRequireException extends \Exception
6+
{
7+
8+
}

src/HttpRequire/HttpRequire.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Bubu\Http\HttpRequire;
4+
5+
use Bubu\Http\HttpRequire\Exception\HttpRequireException;
6+
7+
class HttpRequire
8+
{
9+
/**
10+
* https
11+
*
12+
* @param bool $throwException True for throw exception, false for just redirect to https
13+
* @return never
14+
*/
15+
public static function https(bool $throwException = false)
16+
{
17+
if ($throwException) {
18+
throw new HttpRequireException('HTTPS is required');
19+
} else {
20+
if (empty($_SERVER['HTTPS']) || $_SERVER['HTTPS'] === 'off') {
21+
$location = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
22+
header('HTTP/1.1 301 Moved Permanently');
23+
header('Location: ' . $location);
24+
exit;
25+
}
26+
}
27+
}
28+
}

src/Reponse/DefaultHttpReponse.php

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
<?php
2+
3+
namespace Bubu\Http\Reponse;
4+
5+
trait DefaultHttpReponse
6+
{
7+
8+
// 200
9+
10+
public function reponse200(): self
11+
{
12+
$this->httpCode = 200;
13+
$this->message = 'OK';
14+
return $this;
15+
}
16+
17+
public function reponse201(): self
18+
{
19+
$this->httpCode = 201;
20+
$this->message = 'CREATED';
21+
return $this;
22+
}
23+
24+
public function reponse202(): self
25+
{
26+
$this->httpCode = 202;
27+
$this->message = 'ACCEPTED';
28+
return $this;
29+
}
30+
31+
public function reponse206(): self
32+
{
33+
$this->httpCode = 206;
34+
$this->message = 'Partial Content';
35+
return $this;
36+
}
37+
38+
39+
// 300
40+
41+
public function reponse301(): self
42+
{
43+
$this->httpCode = 301;
44+
$this->message = 'Moved Permanently';
45+
return $this;
46+
}
47+
48+
public function reponse302(): self
49+
{
50+
$this->httpCode = 302;
51+
$this->message = 'Found';
52+
return $this;
53+
}
54+
55+
public function reponse304(): self
56+
{
57+
$this->httpCode = 304;
58+
$this->message = 'Not Modified';
59+
return $this;
60+
}
61+
62+
63+
// 400
64+
65+
public function reponse400(): self
66+
{
67+
$this->httpCode = 400;
68+
$this->message = 'Bad Request';
69+
return $this;
70+
}
71+
72+
public function reponse401(): self
73+
{
74+
$this->httpCode = 401;
75+
$this->message = 'Unauthorized';
76+
return $this;
77+
}
78+
79+
public function reponse402(): self
80+
{
81+
$this->httpCode = 402;
82+
$this->message = 'Payement Required';
83+
return $this;
84+
}
85+
86+
public function reponse403(): self
87+
{
88+
$this->httpCode = 403;
89+
$this->message = 'Forbidden';
90+
return $this;
91+
}
92+
93+
public function reponse404(): self
94+
{
95+
$this->httpCode = 404;
96+
$this->message = 'OK';
97+
return $this;
98+
}
99+
100+
public function reponse405(): self
101+
{
102+
$this->httpCode = 405;
103+
$this->message = 'Method Not Allowed';
104+
return $this;
105+
}
106+
107+
public function reponse406(): self
108+
{
109+
$this->httpCode = 406;
110+
$this->message = 'Not Acceptable';
111+
return $this;
112+
}
113+
114+
public function reponse408(): self
115+
{
116+
$this->httpCode = 408;
117+
$this->message = 'Request Time-out';
118+
return $this;
119+
}
120+
121+
122+
// 500
123+
124+
public function reponse500(): self
125+
{
126+
$this->httpCode = 500;
127+
$this->message = 'Internal Server Error';
128+
return $this;
129+
}
130+
131+
public function reponse501(): self
132+
{
133+
$this->httpCode = 501;
134+
$this->message = 'Not Implemented';
135+
return $this;
136+
}
137+
138+
public function reponse502(): self
139+
{
140+
$this->httpCode = 502;
141+
$this->message = 'Bad Gateway';
142+
return $this;
143+
}
144+
145+
public function reponse503(): self
146+
{
147+
$this->httpCode = 503;
148+
$this->message = 'Service Unavailable';
149+
return $this;
150+
}
151+
152+
public function reponse505(): self
153+
{
154+
$this->httpCode = 505;
155+
$this->message = 'HTTP Version not supported';
156+
return $this;
157+
}
158+
159+
public function reponse511(): self
160+
{
161+
$this->httpCode = 511;
162+
$this->message = 'Network authentication required';
163+
return $this;
164+
}
165+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
namespace Bubu\Http\Reponse\Exception;
3+
4+
class ReponseException extends \Exception
5+
{
6+
7+
}

src/Reponse/Reponse.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Bubu\Http\Reponse;
4+
5+
class Reponse
6+
{
7+
use DefaultHttpReponse;
8+
9+
private int $httpCode = 200;
10+
private string $message = 'OK';
11+
private float $httpVersion = 1.1;
12+
private array $headers = [];
13+
private string $content = '';
14+
15+
public function setHttpCode(int $code): self
16+
{
17+
$this->httpCode = $code;
18+
return $this;
19+
}
20+
21+
public function setHttpMessage(string $message): self
22+
{
23+
$this->message = $message;
24+
return $this;
25+
}
26+
27+
public function createHeader(string $name, string $value): self
28+
{
29+
$this->headers[$name] = $value;
30+
return $this;
31+
}
32+
33+
public function deleteHeader(string $name): self
34+
{
35+
unset($this->headers[$name]);
36+
return $this;
37+
}
38+
39+
public function setup(): self
40+
{
41+
header("HTTP/{$this->httpVersion} {$this->httpCode} {$this->message}");
42+
http_response_code($this->httpCode);
43+
foreach ($this->headers as $key => $value) {
44+
header("{$key}: {$value}");
45+
}
46+
return $this;
47+
}
48+
49+
public function setContent(string $content): self
50+
{
51+
$this->content = $content;
52+
return $this;
53+
}
54+
55+
public function send(): void
56+
{
57+
echo $this->content;
58+
}
59+
}

0 commit comments

Comments
 (0)