forked from flyimg/flyimg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.php
90 lines (75 loc) · 2.25 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
<?php
use Core\Resolver\ControllerResolver;
use Core\Service\ImageManager;
use Monolog\Logger;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Yaml\Yaml;
use League\Flysystem\Cached\Storage\Predis as Cache;
use Predis\Client;
$loader = require_once __DIR__ . '/vendor/autoload.php';
$app = new Silex\Application();
/**
* Define Constants && Load parameters files
*/
define('ROOT_DIR', __DIR__);
define('UPLOAD_DIR', ROOT_DIR . '/var/upload/');
define('TMP_DIR', ROOT_DIR . '/var/tmp/');
define('LOG_DIR', ROOT_DIR . '/var/log /');
if(!is_dir(UPLOAD_DIR)){
mkdir(UPLOAD_DIR, 0777, true);
}
if(!is_dir(TMP_DIR)){
mkdir(TMP_DIR, 0777, true);
}
if(!is_dir(LOG_DIR)){
mkdir(LOG_DIR, 0777, true);
}
$app['params'] = Yaml::parse(file_get_contents(ROOT_DIR . '/config/parameters.yml'));
/**
* Routes
*/
$app['routes'] = $app->extend('routes', function (RouteCollection $routes) {
$loader = new YamlFileLoader(new FileLocator(__DIR__ . '/config'));
$collection = $loader->load('routes.yml');
$routes->addCollection($collection);
return $routes;
});
/**
* Register Fly System Provider
*/
if (getenv('nocache') == 1 || !$app['params']['cache']) {
$adapter = 'League\Flysystem\Adapter\Local';
$args = [UPLOAD_DIR];
} else {
$redisClient = new Client('tcp://redis-service:6379');
$adapter = 'League\Flysystem\Cached\CachedAdapter';
$args = [
new League\Flysystem\Adapter\Local(UPLOAD_DIR),
new Cache($redisClient)
];
}
$app->register(new WyriHaximus\SliFly\FlysystemServiceProvider(), [
'flysystem.filesystems' => [
'upload_dir' => [
'adapter' => $adapter,
'args' => $args
],
],
]);
/**
* Monolog Service
*/
$app->register(new Silex\Provider\MonologServiceProvider(), array(
'monolog.name' => 'fly-image',
'monolog.level' => Logger::ERROR,
'monolog.logfile' => LOG_DIR . 'dev.log',
));
$app['resolver'] = $app->share(function () use ($app) {
return new ControllerResolver($app, $app['logger']);
});
$app['image.manager'] = $app->share(function ($app) {
return new ImageManager($app['params'], $app['flysystems']['upload_dir']);
});
return $app;