Skip to content

Commit 7e230f1

Browse files
committed
🎉 completed project setup
0 parents  commit 7e230f1

File tree

7 files changed

+944
-0
lines changed

7 files changed

+944
-0
lines changed

.github/FUNDING.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# These are supported funding model platforms
2+
3+
open_collective: leaf

.gitignore

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

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<!-- markdownlint-disable no-inline-html -->
2+
<p align="center">
3+
<br><br>
4+
<img src="https://leaf-docs.netlify.app/images/logo.png" height="100"/>
5+
<h1 align="center">Leaf HTTP Module</h1>
6+
<br><br>
7+
</p>
8+
9+
# Leaf PHP
10+
11+
[![Latest Stable Version](https://poser.pugx.org/leafs/http/v/stable)](https://packagist.org/packages/leafs/http)
12+
[![Total Downloads](https://poser.pugx.org/leafs/http/downloads)](https://packagist.org/packages/leafs/http)
13+
[![License](https://poser.pugx.org/leafs/http/license)](https://packagist.org/packages/leafs/http)
14+
15+
Leaf's core http functionality packaged as a serve-yourself module. Although seperated from Leaf core, it is still part of the default initial installation and doesn't need to be installed manually (unless you want a particular version).
16+
17+
## Installation
18+
19+
You can easily install Leaf using [Composer](https://getcomposer.org/).
20+
21+
```bash
22+
composer require leafs/http
23+
```
24+
25+
## View Leaf's docs [here](https://leafphp.netlify.app/#/)
26+
27+
Built with ❤ by [**Mychi Darko**](https://mychi.netlify.app)

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "leafs/http",
3+
"description": "Leaf PHP HTTP module.",
4+
"keywords": [
5+
"http",
6+
"request",
7+
"response",
8+
"headers",
9+
"leaf",
10+
"php",
11+
"framework"
12+
],
13+
"homepage": "https://leafphp.netlify.app/#/",
14+
"type": "library",
15+
"license": "MIT",
16+
"authors": [
17+
{
18+
"name": "Michael Darko",
19+
"email": "[email protected]",
20+
"homepage": "https://mychi.netlify.app",
21+
"role": "Developer"
22+
}
23+
],
24+
"autoload": {
25+
"psr-4": {
26+
"Leaf\\Http\\": "src"
27+
}
28+
},
29+
"minimum-stability": "stable",
30+
"require": {
31+
"leafs/anchor": "^1.0"
32+
}
33+
}

src/Headers.php

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
<?php
2+
3+
namespace Leaf\Http;
4+
5+
/**
6+
* HTTP Headers
7+
* ---------------------
8+
* Response header management made simple with Leaf
9+
*
10+
* @author Michael Darko
11+
* @since 2.0.0
12+
*/
13+
class Headers
14+
{
15+
protected static $http_code;
16+
17+
/**
18+
* Get or Set an HTTP code for response
19+
*
20+
* @param int|null $http_code The current response code.
21+
*/
22+
public static function status($http_code = null)
23+
{
24+
if (!$http_code) return self::$http_code;
25+
self::$http_code = $http_code;
26+
}
27+
28+
/**
29+
* Force an HTTP code for response using PHP's `http_response_code`
30+
*/
31+
public static function resetStatus($http_code = null)
32+
{
33+
return http_response_code($http_code);
34+
}
35+
36+
/**
37+
* Get all headers passed into application
38+
*
39+
* @param bool $safeOutput Try to sanitize header data
40+
*/
41+
public static function all($safeOutput = false): array
42+
{
43+
if ($safeOutput === false) return self::findHeaders();
44+
return \Leaf\Anchor::sanitize(self::findHeaders());
45+
}
46+
47+
/**
48+
* Return a particular header passed into app
49+
*
50+
* @param string|array $param The header(s) to return
51+
* @param bool $safeOutput Try to sanitize header data
52+
*
53+
* @return string|array
54+
*/
55+
public static function get($params, $safeOutput = false)
56+
{
57+
if (is_string($params)) return self::all($safeOutput)[$params] ?? null;
58+
59+
$data = [];
60+
foreach ($params as $param) {
61+
$data[$param] = self::get($param, $safeOutput);
62+
}
63+
return $data;
64+
}
65+
66+
/**
67+
* Set a new header
68+
*/
69+
public static function set($key, $value = "", $replace = true, $http_code = null): void
70+
{
71+
if (!is_array($key)) {
72+
header("$key: $value", $replace, $http_code ?? self::$http_code);
73+
} else {
74+
foreach ($key as $header => $header_value) {
75+
self::set($header, $header_value, $replace, $http_code);
76+
}
77+
}
78+
}
79+
80+
public static function remove($keys)
81+
{
82+
if (!is_array($keys)) {
83+
header_remove($keys);
84+
} else {
85+
foreach ($keys as $key) {
86+
self::remove($key);
87+
}
88+
}
89+
}
90+
91+
public static function contentPlain($code = null): void
92+
{
93+
self::set("Content-Type", "text/plain", true, $code ?? self::$http_code);
94+
}
95+
96+
public static function contentHtml($code = null): void
97+
{
98+
self::set("Content-Type", "text/html", true, $code ?? self::$http_code);
99+
}
100+
101+
public static function contentJSON($code = null): void
102+
{
103+
self::set("Content-Type", "application/json", true, $code ?? self::$http_code);
104+
}
105+
106+
public static function accessControl($key, $value = "", $code = null)
107+
{
108+
if (is_string($key)) {
109+
self::set("Access-Control-$key", $value, true, $code ?? self::$http_code);
110+
} else {
111+
foreach ($key as $header => $header_value) {
112+
self::accessControl($header, $header_value, $code);
113+
}
114+
}
115+
}
116+
117+
protected static function findHeaders()
118+
{
119+
if (function_exists("getallheaders") && \getallheaders()) return \getallheaders();
120+
121+
$headers = [];
122+
foreach ($_SERVER as $name => $value) {
123+
if ((substr($name, 0, 5) == 'HTTP_') || ($name == 'CONTENT_TYPE') || ($name == 'CONTENT_LENGTH')) {
124+
$headers[str_replace([' ', 'Http'], ['-', 'HTTP'], ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
125+
}
126+
}
127+
return $headers;
128+
}
129+
}

0 commit comments

Comments
 (0)