Completed
Push — master ( 7eae42...8f150d )
by Thiago
10:26
created

bootstrap.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 22.

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.

Loading history...
2
declare(strict_types = 1);
3
4
use Common\Response;
5
use DerAlex\Silex\YamlConfigServiceProvider;
6
use Doctrine\Common\Annotations\AnnotationRegistry;
7
use MrPrompt\Silex\Cors\Cors as CorsServiceProvider;
8
use MrPrompt\Silex\Di\Container as DiContainerProvider;
9
use MrPrompt\Silex\Header\Header as HeaderServiceProvider;
10
use MrPrompt\Silex\Router\Router as RouterServiceProvider;
11
use MrPrompt\Silex\Uuid as UuidServiceProvider;
12
use Palma\Silex\Provider\DoctrineORMServiceProvider;
13
use Silex\Application as SilexApplication;
14
use Silex\Provider\MonologServiceProvider;
15
use Symfony\Component\HttpFoundation\Request;
16
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
17
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
18
19
/**
20
 * @const string DS
21
 */
22
defined('DS') || define('DS', DIRECTORY_SEPARATOR);
23
24
/**
25
 * @const string APPLICATION_ENV
26
 */
27
defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ?: 'production'));
28
29
/**
30
 * Auto loader
31
 *
32
 * @var \Composer\Autoload\ClassLoader $loader
33
 */
34
$loader = require 'vendor' . DS . 'autoload.php';
35
$loader->register();
36
37
// Fix to read JMS annotations
38
AnnotationRegistry::registerAutoloadNamespace(
39
    'JMS\Serializer\Annotation', __DIR__ . DS . 'vendor' . DS . 'jms' . DS . 'serializer' . DS . 'src'
40
);
41
42
/* @var $configs array */
43
$configs = [
44
    'config' . DS . 'global' . DS . 'database.yml',
45
    'config' . DS . 'global' . DS . 'services.yml',
46
    'config' . DS . 'global' . DS . 'logger.yml',
47
];
48
49
/**
50
 * Silex Application
51
 *
52
 * @var SilexApplication $app
53
 */
54
$app = new SilexApplication();
55
$app['exception_handler']->disable();
56
57
if (APPLICATION_ENV !== 'production') {
58
    $app['debug']   = true;
59
    $app['testing'] = true;
60
}
61
62
$configFile = __DIR__ . DS . 'tmp' . DS . 'config.yml';
63
$strConfig = '';
64
65
foreach ($configs as $config) {
66
    $strConfig .= file_get_contents($config) . PHP_EOL;
67
}
68
69
file_put_contents($configFile, $strConfig);
70
71
$app->register(new YamlConfigServiceProvider($configFile));
72
73
// Logger Service
74
$app->register(
75
    new MonologServiceProvider(),
76
    [
77
        'monolog.logfile' => $app['config']['log']['logfile'],
78
        'monolog.permission' => $app['config']['log']['permission'],
79
        'monolog.level' => $app['config']['log']['level'],
80
        'monolog.name' => $app['config']['log']['name'],
81
    ]
82
);
83
84
// ORM Service
85
$app->register(
86
    new DoctrineORMServiceProvider(),
87
    [
88
        'doctrine_orm.entities_path' => __DIR__ . DS . 'src',
89
        'doctrine_orm.proxies_path' => __DIR__ . DS . 'tmp' . DS . 'proxy',
90
        'doctrine_orm.proxies_namespace' => 'ApplicationPro',
91
        'doctrine_orm.connection_parameters' => $app['config']['database'][APPLICATION_ENV],
92
        'doctrine_orm.simple_annotation_reader' => false
93
    ]
94
);
95
96
// Loading service container
97
$app->register(new DiContainerProvider($app['config']['services']));
98
99
// CORS provider
100
$app->register(new CorsServiceProvider());
101
102
// Token Header Provider
103
$app->register(new HeaderServiceProvider());
104
105
// Uuid Provider
106
$app->register(new UuidServiceProvider());
107
108
// Router Provider
109
$app->register(new RouterServiceProvider(__DIR__ . DS . 'config' . DS . 'routes' . DS . 'routes.yml'));
110
111
$app->before(function (Request $request) use ($app) {
112
    // Skipping OPTIONS requests
113
    if ($request->getMethod() === 'OPTIONS') {
114
        return;
115
    }
116
117
    // If body request is JSON, decode it!
118
    if (0 === strpos($request->headers->get('Content-Type', 'application/json'), 'application/json')) {
119
        $data = json_decode($request->getContent(), true);
120
121
        $request->request->replace(is_array($data) ? $data : []);
122
    }
123
});
124
125
$app->after(function (Request $request, Response $response, SilexApplication $app) {
126
    $app[CorsServiceProvider::HTTP_CORS]($request, $response);
127
});
128
129
$app->error(function (\Exception $e, $code = Response::HTTP_INTERNAL_SERVER_ERROR) {
130
    if ($e->getCode() !== 0) {
131
        $code = $e->getCode();
132
    }
133
134
    if ($code > 505 || $code < 100) {
135
        $code = 500;
136
    }
137
138
    return new Response(["exception" => $e->getMessage(), "status" => "error"], $code);
139
});
140
141
return $app;
142