forked from flyimg/flyimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.php
130 lines (114 loc) · 3.24 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
require_once __DIR__ . '/constants.php';
require_once __DIR__ . '/vendor/autoload.php';
use Symfony\Component\Debug\ErrorHandler;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\Routing\RouteCollection;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Monolog\Registry;
use Silex\Application;
/**
* Create directories if they don't exist
*/
if (!is_dir(UPLOAD_DIR)) {
mkdir(UPLOAD_DIR, 0777, true);
}
if (!is_dir(TMP_DIR)) {
mkdir(TMP_DIR, 0777, true);
}
/**
* Create Silex application
*/
$app = new Application();
/**
* Load application parameters
*/
$app['params'] = new \Core\Entity\AppParameters(__DIR__ . '/config/parameters.yml');
/**
* Logging via Monolog settings
*/
$logLevel = $app['params']->parameterByKey('log_level');
$logger = new Logger('flyimg');
$logger->pushHandler(new StreamHandler('php://stdout', $logLevel));
Registry::addLogger($logger);
$app['logger'] = $logger;
$exceptionHandlerFunction = function (\Exception $e) use ($app): void {
$request = $app['request_stack']->getCurrentRequest();
$app['logger']->error(
'',
[
'error' => $e->getMessage(),
'uri' => is_null($request) ? '' : $request->getUri(),
'user_agent' => is_null($request) ? '' : $request->headers->get('User-Agent'),
'file' => $e->getFile() . ':' . $e->getLine()
]
);
};
if (!isset($_ENV['env']) || $_ENV['env'] !== 'test') {
$app->error($exceptionHandlerFunction);
}
/**
* Register error handler
*/
ErrorHandler::register();
$exceptionHandler = ExceptionHandler::register($app['params']->parameterByKey('debug'));
$exceptionHandler->setHandler($exceptionHandlerFunction);
/**
* 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 configuration
*/
$app['debug'] = $app['params']->parameterByKey('debug');
return $app;