Skip to content

Commit 236d669

Browse files
author
Wazabii
committed
Added handlers support
1 parent fe46397 commit 236d669

File tree

9 files changed

+137
-177
lines changed

9 files changed

+137
-177
lines changed

Dir.php

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,72 +5,47 @@
55
namespace MaplePHP\Http;
66

77
use MaplePHP\Http\Interfaces\DirInterface;
8+
use MaplePHP\Http\Interfaces\DirHandlerInterface;
89

910
class Dir implements DirInterface
1011
{
1112
private $dir;
12-
private $publicDirPath;
13+
private $handler;
14+
1315

1416
public function __construct($dir)
1517
{
1618
$this->dir = $dir;
17-
18-
// Will move this
19-
$envDir = getenv("APP_PUBLIC_DIR");
20-
$this->publicDirPath = "public/";
21-
if (is_string($envDir) && $this->validateDir($envDir)) {
22-
$this->publicDirPath = ltrim(rtrim($envDir, "/"), "/")."/";
23-
}
2419
}
2520

2621
/**
27-
* Get root dir
28-
* @param string $path
29-
* @return string
22+
* Not required but recommended. You can pass on Directory shortcuts to the class
23+
* E.g. getPublic, getCss
24+
* @param DirHandlerInterface $handler
3025
*/
31-
public function getDir(string $path = ""): string
26+
public function setHandler(DirHandlerInterface $handler): void
3227
{
33-
return $this->dir . $path;
28+
$this->handler = $handler;
3429
}
3530

3631
/**
3732
* Get root dir
3833
* @param string $path
3934
* @return string
4035
*/
41-
public function getRoot(string $path = ""): string
42-
{
43-
return $this->getDir($path);
44-
}
45-
46-
/**
47-
* Get resource dir
48-
* @param string $path
49-
* @return string
50-
*/
51-
public function getResources(string $path = ""): string
52-
{
53-
return $this->getDir("resources/{$path}");
54-
}
55-
56-
/**
57-
* Get resource dir
58-
* @param string $path
59-
* @return string
60-
*/
61-
public function getPublic(string $path = ""): string
36+
public function getDir(string $path = ""): string
6237
{
63-
return $this->getDir("{$this->publicDirPath}{$path}");
38+
return $this->dir . $path;
6439
}
6540

6641
/**
67-
* Get storage dir
42+
* Get root dir
6843
* @param string $path
6944
* @return string
7045
*/
71-
public function getStorage(string $path = ""): string
46+
public function getRoot(string $path = ""): string
7247
{
73-
return $this->getDir("storage/{$path}");
48+
return $this->getDir($path);
7449
}
7550

7651
/**
@@ -80,22 +55,26 @@ public function getStorage(string $path = ""): string
8055
*/
8156
public function getLogs(string $path = ""): string
8257
{
83-
return $this->getStorage("logs/{$path}");
58+
if(!is_null($this->handler)) {
59+
return $this->handler->getLogs($path);
60+
61+
}
62+
return "";
8463
}
8564

8665
/**
87-
* Get cache dir
88-
* @param string $path
89-
* @return string
66+
* Access handler
67+
* @param string $method
68+
* @param array $args
69+
* @return mixed
70+
* @psalm-taint-sink
9071
*/
91-
public function getCaches(string $path = ""): string
72+
public function __call($method, $args): mixed
9273
{
93-
return $this->getStorage("caches/{$path}");
94-
}
95-
96-
public function validateDir(string $path): bool
97-
{
98-
$fullPath = realpath($_ENV['APP_DIR'].$path);
99-
return (is_string($fullPath) && strpos($fullPath, $_ENV['APP_DIR']) === 0);
74+
if (!is_null($this->handler) && method_exists($this->handler, $method)) {
75+
return call_user_func_array([$this->handler, $method], $args);
76+
} else {
77+
throw new \BadMethodCallException("The method ({$method}) does not exist in \"".__CLASS__."\" (DirInterface or DirHandlerInterface).", 1);
78+
}
10079
}
10180
}

Environment.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function has($key): bool
3636
{
3737
return (bool)($this->get($key, null));
3838
}
39-
40-
39+
4140
/**
4241
* Return all env
4342
* @return array
@@ -51,7 +50,7 @@ public function fetch(): array
5150
* Get URI enviment Part data that will be passed to UriInterface and match to public object if exists.
5251
* @return array
5352
*/
54-
public function getUriParts(array $add): array
53+
public function getUriParts(array $add = []): array
5554
{
5655
$arr = array();
5756
$arr['scheme'] = ($this->get("HTTPS") === 'on') ? 'https' : 'http';

Interfaces/DirHandlerInterface.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* DirHandlerInterface
4+
* Is used to extend upon the Dir instance with more dir methods
5+
*/
6+
namespace MaplePHP\Http\Interfaces;
7+
8+
interface DirHandlerInterface
9+
{
10+
11+
}

Interfaces/DirInterface.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -19,38 +19,16 @@ public function getDir(string $path = ""): string;
1919
public function getRoot(string $path = ""): string;
2020

2121
/**
22-
* Get resource dir
23-
* @param string $path
24-
* @return string
25-
*/
26-
public function getResources(string $path = ""): string;
27-
28-
/**
29-
* Get resource dir
30-
* @param string $path
31-
* @return string
32-
*/
33-
public function getPublic(string $path = ""): string;
34-
35-
/**
36-
* Get storage dir
37-
* @param string $path
38-
* @return string
22+
* Not required but recommended. You can pass on Directory shortcuts to the class
23+
* E.g. getPublic, getCss
24+
* @param DirHandlerInterface $handler
3925
*/
40-
public function getStorage(string $path = ""): string;
26+
public function setHandler(DirHandlerInterface $handler): void;
4127

4228
/**
4329
* Get log dir
4430
* @param string $path
4531
* @return string
4632
*/
4733
public function getLogs(string $path = ""): string;
48-
49-
50-
/**
51-
* Get cache dir
52-
* @param string $path
53-
* @return string
54-
*/
55-
public function getCaches(string $path = ""): string;
5634
}

Interfaces/UrlHandlerInterface.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
/**
3+
* UrlHandlerInterface
4+
* Is used to extend upon the Url instance with more url methods
5+
*/
6+
namespace MaplePHP\Http\Interfaces;
7+
8+
interface UrlHandlerInterface
9+
{
10+
/**
11+
* Get the public dir path
12+
* @return string|null
13+
*/
14+
public function getPublicDirPath(): ?string;
15+
}

Interfaces/UrlInterface.php

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -107,37 +107,24 @@ public function last(): string;
107107
public function getRoot(string $path = "", bool $endSlash = false): string;
108108

109109
/**
110-
* Get full URL (path is changeable with @add and @withType method)
111-
* @param string $addToPath add to URI
112-
* @return string
113-
*/
114-
public function getUrl(string $addToPath = ""): string;
115-
116-
/**
117-
* Get URL to public directory
118-
* @param string $path add to URI
119-
* @return string
120-
*/
121-
public function getPublic(string $path = ""): string;
122-
123-
/**
124-
* Get URL to resources directory
125-
* @param string $path add to URI
110+
* Get root URL DIR
111+
* @param string $path add to URI
112+
* @param bool $endSlash add slash to the end of root URL (default false)
126113
* @return string
127114
*/
128-
public function getResource(string $path = ""): string;
115+
public function getRootDir(string $path = "", bool $endSlash = false): string;
129116

130117
/**
131-
* Get URL to js directory
132-
* @param string $path add to URI
118+
* Get full URL (path is changeable with @add and @withType method)
119+
* @param string $addToPath add to URI
133120
* @return string
134121
*/
135-
public function getJs(string $path, bool $isProd = false): string;
122+
public function getUrl(string $addToPath = ""): string;
136123

137124
/**
138-
* Get URL to css directory
139-
* @param string $path add to URI
140-
* @return string
125+
* Not required but recommended. You can pass on URL shortcuts to the class
126+
* E.g. getPublic, getCss
127+
* @param UrlHandlerInterface $handler
141128
*/
142-
public function getCss(string $path): string;
129+
public function setHandler(UrlHandlerInterface $handler): void;
143130
}

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# MaplePHP - PSR Http Message
2-
The library is fully integrated with PSR Http Message and designed for use with MaplePHP framework.
1+
# MaplePHP - PSR-7 Http Message
2+
The library is fully integrated with PSR-7 Http Message and designed for use with MaplePHP framework.
33

44
## Request
5-
None of the construct attributes are required and will auto propagate if you leave them be.
5+
66
```php
77
$request = new Http\ServerRequest(UriInterface $uri, EnvironmentInterface $env);
88
```
@@ -53,6 +53,15 @@ echo $response->hasHeader("Content-Length"); // True
5353
echo $response->getHeader("Content-Length"); // 1299
5454
echo $response->getBody(); // StreamInterface
5555
```
56+
57+
## A standard example usage
58+
```php
59+
$stream = new Http\Stream(Http\Stream::TEMP);
60+
$response = new Http\Response($stream);
61+
$env = new Http\Environment();
62+
$request = new Http\ServerRequest(new Http\Uri($env->getUriParts()), $env);
63+
```
64+
5665
## Stream
5766
None of the construct attributes are required and will auto propagate if you leave them be.
5867
```php

0 commit comments

Comments
 (0)