1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
use Common\Response; |
5
|
|
|
use DerAlex\Silex\YamlConfigServiceProvider; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
7
|
|
|
use SilexFriends\Cors\Cors as CorsServiceProvider; |
8
|
|
|
use SilexFriends\Di\Container as DiContainerProvider; |
9
|
|
|
use SilexFriends\Router\Router as RouterServiceProvider; |
10
|
|
|
use Palma\Silex\Provider\DoctrineORMServiceProvider; |
11
|
|
|
use Silex\Application as SilexApplication; |
12
|
|
|
use Silex\Provider\MonologServiceProvider; |
13
|
|
|
use Symfony\Component\HttpFoundation\Request; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @const string DS |
17
|
|
|
*/ |
18
|
|
|
defined('DS') || define('DS', DIRECTORY_SEPARATOR); |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @const string APPLICATION_ENV |
22
|
|
|
*/ |
23
|
|
|
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?: 'production')); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Auto loader |
27
|
|
|
* |
28
|
|
|
* @var \Composer\Autoload\ClassLoader $loader |
29
|
|
|
*/ |
30
|
|
|
$loader = require 'vendor' . DS . 'autoload.php'; |
31
|
|
|
$loader->register(); |
32
|
|
|
|
33
|
|
|
// Fix to read JMS annotations |
34
|
|
|
AnnotationRegistry::registerAutoloadNamespace( |
35
|
|
|
'JMS\Serializer\Annotation', __DIR__ . DS . 'vendor' . DS . 'jms' . DS . 'serializer' . DS . 'src' |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
/* @var $configs array */ |
39
|
|
|
$configs = [ |
40
|
|
|
'config' . DS . 'global' . DS . 'database.yml', |
41
|
|
|
'config' . DS . 'global' . DS . 'services.yml', |
42
|
|
|
'config' . DS . 'global' . DS . 'logger.yml', |
43
|
|
|
]; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Silex Application |
47
|
|
|
* |
48
|
|
|
* @var SilexApplication $app |
49
|
|
|
*/ |
50
|
|
|
$app = new SilexApplication(); |
51
|
|
|
$app['exception_handler']->disable(); |
52
|
|
|
|
53
|
|
|
if (APPLICATION_ENV !== 'production') { |
54
|
|
|
$app['debug'] = true; |
55
|
|
|
$app['testing'] = true; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$configFile = __DIR__ . DS . 'tmp' . DS . 'config.yml'; |
59
|
|
|
$strConfig = ''; |
60
|
|
|
|
61
|
|
|
foreach ($configs as $config) { |
62
|
|
|
$strConfig .= file_get_contents($config) . PHP_EOL; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
file_put_contents($configFile, $strConfig); |
66
|
|
|
|
67
|
|
|
$app->register(new YamlConfigServiceProvider($configFile)); |
68
|
|
|
|
69
|
|
|
// Logger Service |
70
|
|
|
$app->register( |
71
|
|
|
new MonologServiceProvider(), |
72
|
|
|
[ |
73
|
|
|
'monolog.logfile' => $app['config']['log']['logfile'], |
74
|
|
|
'monolog.permission' => $app['config']['log']['permission'], |
75
|
|
|
'monolog.level' => $app['config']['log']['level'], |
76
|
|
|
'monolog.name' => $app['config']['log']['name'], |
77
|
|
|
] |
78
|
|
|
); |
79
|
|
|
|
80
|
|
|
// ORM Service |
81
|
|
|
$app->register( |
82
|
|
|
new DoctrineORMServiceProvider(), |
83
|
|
|
[ |
84
|
|
|
'doctrine_orm.entities_path' => __DIR__ . DS . 'src', |
85
|
|
|
'doctrine_orm.proxies_path' => __DIR__ . DS . 'tmp' . DS . 'proxy', |
86
|
|
|
'doctrine_orm.proxies_namespace' => 'ApplicationPro', |
87
|
|
|
'doctrine_orm.connection_parameters' => $app['config']['database'][APPLICATION_ENV], |
88
|
|
|
'doctrine_orm.simple_annotation_reader' => false |
89
|
|
|
] |
90
|
|
|
); |
91
|
|
|
|
92
|
|
|
// Loading service container |
93
|
|
|
$app->register(new DiContainerProvider($app['config']['services'])); |
94
|
|
|
|
95
|
|
|
// CORS provider |
96
|
|
|
$app->register(new CorsServiceProvider()); |
97
|
|
|
|
98
|
|
|
// Router Provider |
99
|
|
|
$app->register(new RouterServiceProvider(__DIR__ . DS . 'config' . DS . 'routes' . DS . 'routes.yml')); |
100
|
|
|
|
101
|
|
|
$app->before(function (Request $request) use ($app) { |
102
|
|
|
// Skipping OPTIONS requests |
103
|
|
|
if ($request->getMethod() === 'OPTIONS') { |
104
|
|
|
return; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// If body request is JSON, decode it! |
108
|
|
|
if (0 === strpos($request->headers->get('Content-Type', 'application/json'), 'application/json')) { |
109
|
|
|
$data = json_decode($request->getContent(), true); |
110
|
|
|
|
111
|
|
|
$request->request->replace(is_array($data) ? $data : []); |
112
|
|
|
} |
113
|
|
|
}); |
114
|
|
|
|
115
|
|
|
$app->after(function (Request $request, Response $response, SilexApplication $app) { |
116
|
|
|
$app[CorsServiceProvider::HTTP_CORS]($request, $response); |
117
|
|
|
}); |
118
|
|
|
|
119
|
|
|
$app->error(function (\Exception $e, $code = Response::HTTP_INTERNAL_SERVER_ERROR) { |
120
|
|
|
if ($e->getCode() !== 0) { |
121
|
|
|
$code = $e->getCode(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if ($code > 505 || $code < 100) { |
125
|
|
|
$code = 500; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return new Response(["exception" => $e->getMessage(), "status" => "error"], $code); |
129
|
|
|
}); |
130
|
|
|
|
131
|
|
|
return $app; |
132
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.