Repository navigation
Expand file tree
/
Copy pathApplication.php
More file actions
140 lines (109 loc) · 4.7 KB
/
Copy pathApplication.php
File metadata and controls
140 lines (109 loc) · 4.7 KB
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
131
132
133
134
135
136
137
138
139
140
<?php
/*
* This file is part of the KnowledgePlan website.
*
* (c) 2012 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_once 'bootstrap.php';
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
$app->get('/', function() use ($app) {
return $app['twig']->render('homepage.html.twig', array(
'experiences' => $app['kp.experiences']
));
})->bind('homepage');
$app->get('/experience/{name}', function($name) use ($app) {
// The controller don't check if the experience exists.
// Indeed, it will be the aim of the javascript to do it
return $app['twig']->render('experience.html.twig', array(
'name' => $name
));
})->bind('experience');
$app->match('/options', function() use ($app) {
$simulations = Finder::create()->files()
->in($app['kp.simulations_folder'])
->getIterator();
$form = $app['form.factory']->createBuilder('form')
->add('name', 'text', array('label' => 'Name'))
->add('file', 'file', array('label' => 'Simulation file'))
->getForm();
if ('POST' === $app['request']->getMethod()) {
$form->bindRequest($app['request']);
if ($form->isValid()) {
$data = $form->getData();
$file = $data['file'];
$name = strtolower($data['name']);
$simulation = $app['kp.simulations_folder'].'/'.$name;
$newSimulation = $simulation.'_new';
// Check the file mimetype
// if ('text/plain' !== $file->getClientMimeType()) {
// throw new \InvalidArgumentException('The file mimetype must be "text/plain".');
// }
// Move the file to the correct folder
$file->move(dirname($newSimulation), basename($newSimulation));
// Is it a correct simulation output file?
try {
$app['kp.simulation_manager']->get(pathinfo($newSimulation, PATHINFO_FILENAME), true, false);
$correct = true;
} catch (\Exception $e) {
$correct = false;
}
if (false === $correct) {
// Remove the uncorrect file
unlink($newSimulation);
$app['session']->setFlash('error', 'Invalid format');
} else {
// The file is correct
if (true === file_exists($simulation)) {
$app['kp.simulation_manager']->remove($name);
unlink($simulation);
}
rename($newSimulation, $simulation);
$app['session']->getFlashBag()->set('success', sprintf('The simulation output "%s" has been correctly updated.', $name));
// Force creation of the cache file
$app['kp.simulation_manager']->get($name);
}
return $app->redirect($app['url_generator']->generate('options'));
}
}
return $app['twig']->render('options.html.twig', array(
'simulations' => $simulations,
'form' => $form->createView()
));
})->bind('options');
$app->get('/js/experience-{name}.js', function($name) use ($app) {
if (false === $app['kp.experience_manager']->has($name)) {
return $app->abort(404, sprintf('The experience "%s does not exist.', $name));
}
// The streamed response speed up the result
$response = new StreamedResponse(function() use ($app, $name) {
echo $app['twig']->render('experience.js.twig', array(
'experience' => $app['kp.experience_manager']->get($name)
));
});
$response->headers->set('Content-Type', 'text/javascript');
return $response;
})->bind('experience_js');
$app->error(function(\Exception $exception, $code) use ($app) {
return $app['twig']->render('error.html.twig', array(
'code' => $code,
'exception' => $exception,
));
});
$app->get('/remove/{filename}', function($filename) use ($app) {
$path = $app['kp.simulations_folder'].'/'.$filename;
unlink($path);
$app['kp.simulation_manager']->remove($filename);
// Flash
$app['session']->setFlash('success', sprintf('The simulation "%s" has been correctly removed.', $filename));
return $app->redirect($app['url_generator']->generate('options'));
})->bind('remove');
$app->get('/clear-cache', function() use ($app) {
$app['kp.simulation_manager']->removeAll();
return new Response('ok');
})->bind('clear_cache');
return $app;