Skip to content

Commit f7f18a7

Browse files
committed
[#.x] - started using providers, moved routing out
1 parent 3a98171 commit f7f18a7

File tree

1 file changed

+21
-45
lines changed

1 file changed

+21
-45
lines changed

public/index.php

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,41 @@
22

33
declare(strict_types=1);
44

5-
use Phalcon\Api\Domain\Interfaces\DomainInterface;
6-
use Phalcon\Api\Domain\Interfaces\ResponderInterface;
7-
use Phalcon\Api\Domain\Middleware\ResponseSender;
8-
use Phalcon\Api\Domain\Services\ActionHandler;
95
use Phalcon\Api\Domain\Services\Container;
6+
use Phalcon\Api\Domain\Services\Providers\ErrorHandlerProvider;
7+
use Phalcon\Api\Domain\Services\Providers\RouterProvider;
8+
use Phalcon\Di\ServiceProviderInterface;
109
use Phalcon\Mvc\Micro;
1110

1211
require_once dirname(__DIR__) . '/vendor/autoload.php';
1312

1413
$container = new Container();
1514
$application = new Micro($container);
15+
$container->set(Container::APPLICATION, $application, true);
16+
$now = hrtime(true);
17+
$container->set(
18+
Container::TIME,
19+
function () use ($now) {
20+
return $now;
21+
},
22+
true
23+
);
1624

1725
/**
18-
* Routes
26+
* Providers
1927
*/
20-
$routes = [
21-
[
22-
'method' => 'get',
23-
'pattern' => '/',
24-
'service' => Container::HELLO_SERVICE,
25-
'responder' => Container::HELLO_RESPONDER_JSON,
26-
],
28+
$providers = [
29+
ErrorHandlerProvider::class,
30+
RouterProvider::class,
2731
];
2832

29-
foreach ($routes as $route) {
30-
$method = $route['method'];
31-
$pattern = $route['pattern'];
32-
$serviceName = $route['service'];
33-
$responderName = $route['responder'];
34-
35-
$application->$method(
36-
$pattern,
37-
function () use ($container, $serviceName, $responderName) {
38-
/** @var DomainInterface $service */
39-
$service = $container->get($serviceName);
40-
/** @var ResponderInterface $responder */
41-
$responder = $container->get($responderName);
42-
43-
$action = new ActionHandler($service, $responder);
44-
$action->__invoke();
45-
}
46-
);
33+
/** @var class-string $provider */
34+
foreach ($providers as $provider) {
35+
/** @var ServiceProviderInterface $service */
36+
$service = new $provider();
37+
$container->register($service);
4738
}
4839

49-
$application->finish(
50-
function () use ($container) {
51-
$response = $container->getShared(Container::RESPONSE);
52-
$sender = new ResponseSender();
53-
54-
$sender->__invoke($response);
55-
}
56-
);
57-
58-
$application->notFound(
59-
function () {
60-
echo "404 - Not Found - " . date("Y-m-d H:i:s");
61-
}
62-
);
63-
6440

6541
/** @var string $uri */
6642
$uri = $_SERVER['REQUEST_URI'] ?? '';

0 commit comments

Comments
 (0)