-
Notifications
You must be signed in to change notification settings - Fork 1
/
Application.php
94 lines (71 loc) · 2.52 KB
/
Application.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
<?php
/*
* This file is part of the ZeldaLogs website.
*
* (c) 2010-2011 Julien Brochet <mewt@madalynn.eu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require 'bootstrap.php';
use Silex\Application;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
$app->get('/{year}', function(Application $app, $year) {
$logs = $app['log.manager']->retrieveFiles()
->retrieveByYear($year);
$years = $app['log.manager']->getYears(true);
return $app['twig']->render('year.html.twig', array(
'current_year' => $year,
'years' => $years,
'year' => $logs,
));
})->value('year', date('Y'))
->assert('year', '\d{4}')
->bind('homepage');
$app->get('/{year}/{month}/{day}/{page}', function(Application $app, $year, $month, $day, $page) {
try {
$date = new \DateTime(implode('-', array($year, $month, $day)));
} catch(\Exception $e) {
$app->abort(404, 'La date est invalide.');
}
$day = $app['log.manager']->retrieveByDate($date);
if (null === $day) {
$app->abort(404, 'Ce jour n\'est pas archivé, désolé.');
}
return $app['twig']->render('day.html.twig', array(
'day' => $day,
'page' => $page
));
})->value('page', 1)
->assert('year', '\d{4}')
->assert('month', '\d{2}')
->assert('day', '\d{2}')
->assert('page', '\d+');
$app->post('/search', function(Application $app) {
$request = $app['request'];
$search = $request->get('search');
$where = $request->get('where');
$date = $app['log.manager']->parseSerializedDate($where);
if (null === $date) {
throw new \LogicException('Impossible de récupérer correctement la date donnée.');
}
$day = $app['log.manager']->retrieveByDate($date);
if (null === $day) {
throw new NotFoundHttpException('Ce jour n\'est pas archivé.');
}
$lines = $day->search($search);
return $app['twig']->render('search.html.twig', array(
'day' => $day,
'search' => $search,
'lines' => $lines
));
})->bind('search');
$app->error(function (\Exception $e) use ($app) {
return $app['twig']->render('error.html.twig', array('e' => $e));
});
$app->get('/logout', function(Application $app) {
$app['session']->remove('username');
return $app->redirect($app['url_generator']->generate('homepage'));
})->bind('logout');
return $app;