|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Alexander Torosh <[email protected]> |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Api; |
|
7
|
|
|
|
|
8
|
|
|
use Core\Cache\ApcuCache; |
|
9
|
|
|
use Core\Config\EnvironmentLoader; |
|
10
|
|
|
use Phalcon\Db\Adapter\Pdo\Postgresql; |
|
11
|
|
|
use Phalcon\Di; |
|
12
|
|
|
use Phalcon\Http\Request; |
|
13
|
|
|
use Phalcon\Http\Response; |
|
14
|
|
|
use Phalcon\Mvc\Micro; |
|
15
|
|
|
use Phalcon\Mvc\Model\Manager as ModelsManager; |
|
16
|
|
|
use Phalcon\Mvc\Model\Metadata\Memory as ModelsMetadata; |
|
17
|
|
|
use Phalcon\Mvc\Router as PhalconRouter; |
|
18
|
|
|
|
|
19
|
|
|
class Application |
|
20
|
|
|
{ |
|
21
|
|
|
public function run() |
|
22
|
|
|
{ |
|
23
|
|
|
// DI Container |
|
24
|
|
|
$container = new Di(); |
|
25
|
|
|
|
|
26
|
|
|
// Initialize micro app |
|
27
|
|
|
$app = new Micro($container); |
|
28
|
|
|
|
|
29
|
|
|
// Set serverCache service |
|
30
|
|
|
$app->setService('serverCache', (new ApcuCache())->init(), true); |
|
31
|
|
|
|
|
32
|
|
|
$this->initConfiguration($container); |
|
33
|
|
|
$this->initApplicationServices($app); |
|
34
|
|
|
$this->initDatabase($app); |
|
35
|
|
|
|
|
36
|
|
|
// Initialize API Routing |
|
37
|
|
|
$apiRouter = new Router(); |
|
38
|
|
|
$apiRouter->init($app); |
|
39
|
|
|
|
|
40
|
|
|
// Handle exceptions |
|
41
|
|
|
$app = $this->handleExceptions($app); |
|
42
|
|
|
|
|
43
|
|
|
// Handle request |
|
44
|
|
|
$app->handle($_SERVER['REQUEST_URI']); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
private function initConfiguration(Di $container) |
|
48
|
|
|
{ |
|
49
|
|
|
$configLoader = new EnvironmentLoader(); |
|
50
|
|
|
$configLoader->setDI($container); |
|
51
|
|
|
$configLoader->load(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
private function initApplicationServices(Micro $app) |
|
55
|
|
|
{ |
|
56
|
|
|
$app->setService('router', new PhalconRouter(), true); |
|
57
|
|
|
$app->setService('request', new Request(), true); |
|
58
|
|
|
$app->setService('response', new Response(), true); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
private function initDatabase(Micro $app) |
|
62
|
|
|
{ |
|
63
|
|
|
$database = new Postgresql([ |
|
64
|
|
|
'host' => getenv('DB_HOST'), |
|
65
|
|
|
'port' => getenv('DB_PORT'), |
|
66
|
|
|
'username' => getenv('DB_USER'), |
|
67
|
|
|
'password' => getenv('DB_PASS'), |
|
68
|
|
|
'dbname' => getenv('DB_NAME'), |
|
69
|
|
|
]); |
|
70
|
|
|
$app->setService('db', $database, true); |
|
71
|
|
|
$app->setService('modelsManager', new ModelsManager()); |
|
72
|
|
|
$app->setService('modelsMetadata', new ModelsMetadata()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
private function handleExceptions(Micro $app): Micro |
|
76
|
|
|
{ |
|
77
|
|
|
$app->error( |
|
78
|
|
|
function ($exception) use ($app) { |
|
79
|
|
|
if ('development' === getenv('APP_ENV')) { |
|
80
|
|
|
throw $exception; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$code = $exception->getCode() ?: 503; |
|
84
|
|
|
|
|
85
|
|
|
/** @var \Phalcon\Http\Response $response */ |
|
86
|
|
|
$response = $app->response; |
|
87
|
|
|
$response->setJsonContent([ |
|
88
|
|
|
'code' => $code, |
|
89
|
|
|
'status' => 'error', |
|
90
|
|
|
'message' => $exception->getMessage(), |
|
91
|
|
|
]); |
|
92
|
|
|
|
|
93
|
|
|
$response->setStatusCode($code); |
|
94
|
|
|
$response->send(); |
|
95
|
|
|
} |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
return $app; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|