Skip to content

Commit 2c80c47

Browse files
committed
[#.x] - added router provider and error handler provider
1 parent 13e78f6 commit 2c80c47

File tree

2 files changed

+186
-0
lines changed

2 files changed

+186
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Services\Providers;
15+
16+
use Phalcon\Api\Domain\Services\Container;
17+
use Phalcon\Api\Domain\Services\Env\EnvManager;
18+
use Phalcon\Di\DiInterface;
19+
use Phalcon\Di\ServiceProviderInterface;
20+
use Phalcon\Logger\Logger;
21+
22+
use function date_default_timezone_set;
23+
use function error_reporting;
24+
use function hrtime;
25+
use function memory_get_usage;
26+
use function number_format;
27+
use function register_shutdown_function;
28+
use function sprintf;
29+
30+
class ErrorHandlerProvider implements ServiceProviderInterface
31+
{
32+
public function register(DiInterface $container): void
33+
{
34+
$logger = $container->getShared(Container::LOGGER);
35+
36+
date_default_timezone_set(EnvManager::appTimezone());
37+
$errors = 'development' === EnvManager::appEnv() ? 'On' : 'Off';
38+
ini_set('display_errors', $errors);
39+
error_reporting(E_ALL);
40+
41+
set_error_handler(
42+
function (int $number, string $message, string $file, int $line) use ($logger) {
43+
$logger
44+
->error(
45+
sprintf(
46+
'[#:%s]-[L: %s] : %s (%s)',
47+
$number,
48+
$line,
49+
$message,
50+
$file
51+
)
52+
)
53+
;
54+
55+
return true;
56+
}
57+
);
58+
59+
register_shutdown_function([$this, 'onShutdown'], $container);
60+
}
61+
62+
protected function onShutdown(DiInterface $container): bool
63+
{
64+
/** @var Logger $logger */
65+
$logger = $container->getShared(Container::LOGGER);
66+
/** @var int $time */
67+
$time = $container->getShared(Container::TIME);
68+
$memory = memory_get_usage() / 1000000;
69+
$execution = hrtime(true) - $time;
70+
$execution = $execution / 1000000000;
71+
72+
if (EnvManager::appLogLevel() >= 1) {
73+
$logger
74+
->info(
75+
sprintf(
76+
'Shutdown completed [%s]s - [%s]MB',
77+
number_format($execution, 4),
78+
number_format($memory, 2),
79+
)
80+
)
81+
;
82+
}
83+
84+
return true;
85+
}
86+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon API.
5+
*
6+
* (c) Phalcon Team <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Api\Domain\Services\Providers;
15+
16+
use Phalcon\Api\Domain\Interfaces\DomainInterface;
17+
use Phalcon\Api\Domain\Interfaces\ResponderInterface;
18+
use Phalcon\Api\Domain\Interfaces\RoutesInterface;
19+
use Phalcon\Api\Domain\Services\ActionHandler;
20+
use Phalcon\Api\Domain\Services\Container;
21+
use Phalcon\Di\DiInterface;
22+
use Phalcon\Di\ServiceProviderInterface;
23+
use Phalcon\Events\Manager as EventsManager;
24+
use Phalcon\Mvc\Micro;
25+
26+
/**
27+
* @phpstan-import-type TMiddleware from RoutesInterface
28+
*/
29+
class RouterProvider implements ServiceProviderInterface
30+
{
31+
public function register(DiInterface $container): void
32+
{
33+
/** @var Micro $application */
34+
$application = $container->getShared(Container::APPLICATION);
35+
/** @var EventsManager $eventsManager */
36+
$eventsManager = $container->getShared(Container::EVENTS_MANAGER);
37+
38+
$this->attachRoutes($application);
39+
$this->attachMiddleware($application, $eventsManager);
40+
41+
$application->get('/health', function () {
42+
/** empty */
43+
});
44+
45+
$application->setEventsManager($eventsManager);
46+
}
47+
48+
/**
49+
* @param Micro $application
50+
* @param EventsManager $eventsManager
51+
*
52+
* @return void
53+
*/
54+
private function attachMiddleware(
55+
Micro $application,
56+
EventsManager $eventsManager
57+
): void {
58+
/** @var TMiddleware $middleware */
59+
$middleware = RoutesEnum::middleware();
60+
foreach ($middleware as $service => $method) {
61+
$instance = $application->getService($service);
62+
$eventsManager->attach('micro', $instance);
63+
$application->$method($instance);
64+
}
65+
}
66+
67+
/**
68+
* Attaches routes to the application, lazy loaded
69+
*
70+
* @param Micro $application
71+
*
72+
* @return void
73+
*/
74+
private function attachRoutes(Micro $application): void
75+
{
76+
/** @var ResponderInterface $responder */
77+
$responder = $application->getService(Container::RESPONDER_JSON);
78+
79+
/** @var array<RoutesInterface> $routes */
80+
$routes = RoutesEnum::cases();
81+
foreach ($routes as $route) {
82+
$serviceName = $route->service();
83+
$collection = new Micro\Collection();
84+
/** @var DomainInterface $service */
85+
$service = $application->getService($serviceName);
86+
$action = new ActionHandler($service, $responder);
87+
88+
$collection
89+
->setHandler($action)
90+
->setPrefix($route->prefix())
91+
->{$route->method()}(
92+
$route->suffix(),
93+
'__invoke'
94+
)
95+
;
96+
97+
$application->mount($collection);
98+
}
99+
}
100+
}

0 commit comments

Comments
 (0)