-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
109 lines (85 loc) · 2.69 KB
/
index.php
File metadata and controls
109 lines (85 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
use \Psr\Http\Message\ResponseInterface;
use \Psr\Http\Message\ServerRequestInterface;
use \Psr\Http\Server\MiddlewareInterface;
use \Psr\Container\ContainerInterface;
use \Morphable\Micro\Route;
use \Psr\Http\Server\RequestHandlerInterface;
use \Morphable\Micro\Route\Dispatcher;
require __DIR__ . '/vendor/autoload.php';
class A implements MiddlewareInterface {
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
echo "middleware\n";
return $handler->handle($request);
}
}
class SomeDependency
{
public function idkWhatImDoing()
{
// return psr-7 response interface
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
$responseBody = $psr17Factory->createStream('Hello world');
return $psr17Factory->createResponse(200)->withBody($responseBody);
}
}
class BController {
protected $idk;
public function __construct($idkWhatImDoing)
{
$this->idk = $idkWhatImDoing;
}
public function someAction($request, $args) {
return $this->idk->idkWhatImDoing();
}
public function __invoke($request, $args)
{
return $this->idk->idkWhatImDoing();
}
}
// create request
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();
$creator = new \Nyholm\Psr7Server\ServerRequestCreator(
$psr17Factory, // ServerRequestFactory
$psr17Factory, // UriFactory
$psr17Factory, // UploadedFileFactory
$psr17Factory // StreamFactory
);
$request = $creator->fromGlobals();
$container = new \League\Container\Container();
$container->add('middleware', A::class);
$container->add('idk', SomeDependency::class);
$container->add('controller', BController::class)->addArgument('idk');
$micro = new \Morphable\Micro();
$micro->setContainer($container);
$router = $micro->routing();
$router->add('GET', '/', ['controller', 'someAction'])->middleware(['middleware']);
$router->group('api', function ($router) {
$router->group('user', function ($router) {
$router->add('GET', '/', function () {
die('user index');
});
$router->add('GET', '/edit', function () {
die('edit');
});
$router->add('GET', '/:id', function ($req, $args) {
die($args['id']);
})->middleware('middleware');
});
})->middleware('middleware');
$router->add('get', '/channel/callback/?:method', function ($req, $args) {
echo '<pre>';
print_r($args);
echo '</pre>';
die;
})->match($request);
try {
$response = $micro->handle($request);
} catch (\Exception $e) {
die('404');
}
echo '<pre>';
print_r((string) $response->getBody());
echo '</pre>';
die;