Skip to content

Commit d175f05

Browse files
committed
Initial commit
0 parents  commit d175f05

File tree

5 files changed

+164
-0
lines changed

5 files changed

+164
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
.phpunit.result.cache
3+
/vendor/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Hadi Akbarzadeh [email protected]
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
13+
all 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
21+
THE SOFTWARE.

README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Headers Reader for PHP
2+
3+
> A simple way to read headers from the `getallheaders` function in PHP.
4+
5+
<hr>
6+
7+
## 🫡 Usage
8+
9+
### 🚀 Installation
10+
11+
You can install the package via composer:
12+
13+
```bash
14+
composer require nabeghe/headers-reader
15+
```
16+
17+
<hr>
18+
19+
### Example 1 - Main Class
20+
21+
```php
22+
use Nabeghe\HeadersReader\Headers;
23+
24+
echo 'X-Custom-1: '.Headers::get('X-Custom-1', 'The default value for X-Custom-1')."\n<br>";
25+
echo 'X-Custom-2: '.Headers::get('X-Custom-2', 'The default value for X-Custom-2')."\n<br>";
26+
echo 'X-Custom-3: '.Headers::get('X-Custom-3', 'The default value for X-Custom-3')."\n<br>";
27+
echo 'X-Custom-4: '.Headers::get('X-Custom-4', 'The default value for X-Custom-4')."\n<br>";
28+
echo 'X-Custom-5: '.Headers::get('X-Custom-4')."\n<br><br>"; // Default value is blank.
29+
30+
print_r(Headers::all()); // Returns all headers.
31+
32+
Headers::flush(); // Clears the cache.
33+
```
34+
35+
### Example 1 - Custom Class
36+
37+
```php
38+
use Nabeghe\HeadersReader\Headers;
39+
40+
class MyHeaders extends Headers
41+
{
42+
public const DEFAULT = 'The general default value';
43+
44+
public const DEFAULTS = [
45+
'X-Custom-1' => 'The default value for X-Custom-1',
46+
'X-Custom-2' => 'The default value for X-Custom-2',
47+
'X-Custom-3' => 'The default value for X-Custom-3',
48+
'X-Custom-4' => 'The default value for X-Custom-4',
49+
];
50+
}
51+
52+
echo 'X-Custom-1: '.MyHeaders::get('X-Custom-1')."\n<br>";
53+
echo 'X-Custom-2: '.MyHeaders::get('X-Custom-2')."\n<br>";
54+
echo 'X-Custom-3: '.MyHeaders::get('X-Custom-3')."\n<br>";
55+
echo 'X-Custom-4: '.MyHeaders::get('X-Custom-4')."\n<br>";
56+
echo 'X-Custom-5: '.MyHeaders::get('X-Custom-5')."\n<br>";
57+
```
58+
59+
<hr>
60+
61+
## 📖 License
62+
63+
Copyright (c) Hadi Akbarzadeh
64+
65+
Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "nabeghe/headers-reader",
3+
"description": "A simple way to read headers from the `getallheaders` function in PHP.",
4+
"keywords": [
5+
"header", "headers", "headers reader", "headers reader", "reader", "getallheaders",
6+
"library", "helper", "support"
7+
],
8+
"type": "library",
9+
"version": "1.0.0",
10+
"homepage": "https://github.com/nabeghe/reflecty-php",
11+
"license": "MIT",
12+
"autoload": {
13+
"psr-4": {
14+
"Nabeghe\\HeadersReader\\": "src/"
15+
}
16+
},
17+
"authors": [
18+
{
19+
"name": "Hadi Akbarzadeh",
20+
"email": "[email protected]",
21+
"homepage": "https://elatel.ir",
22+
"role": "Developer"
23+
}
24+
],
25+
"require": {
26+
"php": ">=5.6"
27+
}
28+
}

src/Headers.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php namespace Nabeghe\HeadersReader;
2+
3+
class Headers
4+
{
5+
public const DEFAULT = '';
6+
7+
public const DEFAULTS = [];
8+
9+
protected static ?array $headers;
10+
11+
protected static function init()
12+
{
13+
if (!isset(static::$headers)) {
14+
$headers = getallheaders();
15+
if (!is_array($headers)) {
16+
$headers = [];
17+
}
18+
static::$headers = $headers;
19+
}
20+
}
21+
22+
public static function all(): array
23+
{
24+
static::init();
25+
return static::$headers;
26+
}
27+
28+
public static function get($name, $default = null)
29+
{
30+
static::init();
31+
32+
if (isset(static::$headers[$name])) {
33+
return static::$headers[$name];
34+
}
35+
36+
if (func_num_args() > 1) {
37+
return $default;
38+
}
39+
40+
return static::DEFAULTS[$name] ?? static::DEFAULT;
41+
}
42+
43+
public static function flush()
44+
{
45+
static::$headers = null;
46+
}
47+
}

0 commit comments

Comments
 (0)