forked from mrprompt/silex-api-skel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.php
131 lines (106 loc) · 3.64 KB
/
bootstrap.php
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
declare(strict_types = 1);
use Common\Response;
use DerAlex\Silex\YamlConfigServiceProvider;
use Doctrine\Common\Annotations\AnnotationRegistry;
use MrPrompt\Silex\Cors\Cors as CorsServiceProvider;
use MrPrompt\Silex\Di\Container as DiContainerProvider;
use MrPrompt\Silex\Router\Router as RouterServiceProvider;
use Palma\Silex\Provider\DoctrineORMServiceProvider;
use Silex\Application as SilexApplication;
use Silex\Provider\MonologServiceProvider;
use Symfony\Component\HttpFoundation\Request;
/**
* @const string DS
*/
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
/**
* @const string APPLICATION_ENV
*/
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?: 'production'));
/**
* Auto loader
*
* @var \Composer\Autoload\ClassLoader $loader
*/
$loader = require 'vendor' . DS . 'autoload.php';
$loader->register();
// Fix to read JMS annotations
AnnotationRegistry::registerAutoloadNamespace(
'JMS\Serializer\Annotation', __DIR__ . DS . 'vendor' . DS . 'jms' . DS . 'serializer' . DS . 'src'
);
/* @var $configs array */
$configs = [
'config' . DS . 'global' . DS . 'database.yml',
'config' . DS . 'global' . DS . 'services.yml',
'config' . DS . 'global' . DS . 'logger.yml',
];
/**
* Silex Application
*
* @var SilexApplication $app
*/
$app = new SilexApplication();
$app['exception_handler']->disable();
if (APPLICATION_ENV !== 'production') {
$app['debug'] = true;
$app['testing'] = true;
}
$configFile = __DIR__ . DS . 'tmp' . DS . 'config.yml';
$strConfig = '';
foreach ($configs as $config) {
$strConfig .= file_get_contents($config) . PHP_EOL;
}
file_put_contents($configFile, $strConfig);
$app->register(new YamlConfigServiceProvider($configFile));
// Logger Service
$app->register(
new MonologServiceProvider(),
[
'monolog.logfile' => $app['config']['log']['logfile'],
'monolog.permission' => $app['config']['log']['permission'],
'monolog.level' => $app['config']['log']['level'],
'monolog.name' => $app['config']['log']['name'],
]
);
// ORM Service
$app->register(
new DoctrineORMServiceProvider(),
[
'doctrine_orm.entities_path' => __DIR__ . DS . 'src',
'doctrine_orm.proxies_path' => __DIR__ . DS . 'tmp' . DS . 'proxy',
'doctrine_orm.proxies_namespace' => 'ApplicationPro',
'doctrine_orm.connection_parameters' => $app['config']['database'][APPLICATION_ENV],
'doctrine_orm.simple_annotation_reader' => false
]
);
// Loading service container
$app->register(new DiContainerProvider($app['config']['services']));
// CORS provider
$app->register(new CorsServiceProvider());
// Router Provider
$app->register(new RouterServiceProvider(__DIR__ . DS . 'config' . DS . 'routes' . DS . 'routes.yml'));
$app->before(function (Request $request) use ($app) {
// Skipping OPTIONS requests
if ($request->getMethod() === 'OPTIONS') {
return;
}
// If body request is JSON, decode it!
if (0 === strpos($request->headers->get('Content-Type', 'application/json'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : []);
}
});
$app->after(function (Request $request, Response $response, SilexApplication $app) {
$app[CorsServiceProvider::HTTP_CORS]($request, $response);
});
$app->error(function (\Exception $e, $code = Response::HTTP_INTERNAL_SERVER_ERROR) {
if ($e->getCode() !== 0) {
$code = $e->getCode();
}
if ($code > 505 || $code < 100) {
$code = 500;
}
return new Response(["exception" => $e->getMessage(), "status" => "error"], $code);
});
return $app;