forked from flyimg/flyimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.php
107 lines (89 loc) · 2.96 KB
/
app.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
<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Max-Age: 1000");
header("Access-Control-Allow-Headers: X-Requested-With, Content-Type, Origin, Cache-Control, Pragma, Authorization, Accept, Accept-Encoding");
header("Access-Control-Allow-Methods: PUT, POST, GET, OPTIONS, DELETE");
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/constants.php';
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Routing\RouteCollection;
$app = new Silex\Application();
/** @var \Core\Entity\AppParameters $app['params'] */
$app['params'] = new \Core\Entity\AppParameters(__DIR__ . '/config/parameters.yml');
$app['env'] = $_ENV['env'] ?? 'dev';
$exceptionHandlerFunction = function (\Exception $e): void {
$out = fopen('php://stdout', 'w');
fputs(
$out,
"Message: {$e->getMessage()} \nFile: {$e->getFile()}\nLine: {$e->getLine()}\nTrace: {$e->getTraceAsString()}"
);
fclose($out);
};
ErrorHandler::register();
$exceptionHandler = ExceptionHandler::register($app['params']->parameterByKey('debug'));
$exceptionHandler->setHandler($exceptionHandlerFunction);
if ('test' !== $app['env']) {
$app->error($exceptionHandlerFunction);
}
if (!is_dir(UPLOAD_DIR)) {
mkdir(UPLOAD_DIR, 0777, true);
}
if (!is_dir(TMP_DIR)) {
mkdir(TMP_DIR, 0777, true);
}
/**
* Routes
*/
$routesResolver = new \Core\Resolver\RoutesResolver();
$app['routes'] = $app->extend(
'routes',
function (RouteCollection $routes) use ($routesResolver) {
return $routesResolver->parseRoutesFromYamlFile($routes, __DIR__ . '/config/routes.yml');
}
);
/** Register Storage provider */
switch ($app['params']->parameterByKey('storage_system')) {
case 's3':
$app->register(new \Core\StorageProvider\S3StorageProvider());
break;
case 'local':
default:
$app->register(new \Core\StorageProvider\LocalStorageProvider());
break;
}
/**
* Controller Resolver
*
* @param \Silex\Application $app
*
* @return \Core\Resolver\ControllerResolver
*/
$app['resolver'] = function (\Silex\Application $app) {
return new \Core\Resolver\ControllerResolver($app, $app['logger']);
};
/**
* Register Image Handler
*
* @param \Silex\Application $app
*
* @return \Core\Handler\ImageHandler
*/
$app['image.handler'] = function (\Silex\Application $app): \Core\Handler\ImageHandler {
return new \Core\Handler\ImageHandler(
$app['flysystems']['storage_handler'],
$app['params']
);
};
/**
* To generate a hashed url when security key is enabled
* Example usage: php app.php encrypt w_200,h_200,c_1/Rovinj-Croatia.jpg
*/
if (!empty($argv[1]) && !empty($argv[2]) && $argv[1] == 'encrypt') {
printf("Hashed request: %s\n", $app['image.handler']->securityHandler()->encrypt($argv[2]));
return;
}
/** debug conf */
$app['debug'] = $app['params']->parameterByKey('debug');
return $app;