forked from modess/git-pretty-stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
78 lines (65 loc) · 2.38 KB
/
index.php
File metadata and controls
78 lines (65 loc) · 2.38 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
<?php
ini_set('memory_limit', '512M');
set_time_limit(300);
require_once __DIR__.'/vendor/autoload.php';
use Symfony\Component\HttpFoundation\Response;
use GitPrettyStats\Repository;
use GitPrettyStats\RepositoryFactory;
$app = new Silex\Application();
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views',
));
$app->error(function(\Exception $e) use($app) {
return $app["twig"]->render("error.html", array("message" => $e->getMessage()));
});
$app->before(function() use ($app) {
$repositoriesPath = 'repositories';
$configFilePath = __DIR__ . '/config.php';
$app['config'] = (file_exists($configFilePath)) ? require_once __DIR__ . '/config.php' : null;
$app['repositoryFactory'] = new RepositoryFactory($app['config']);
$app['repositories'] = $app['repositoryFactory']->all();
if (count($app['repositories']) == 0) {
throw new RuntimeException("No repositories found in path: $repositoriesPath", 0);
}
});
$app->get('/', function () use ($app) {
return $app['twig']->render(
'index.html',
array(
'repositories' => $app['repositoryFactory']->toArray()
)
);
});
$app->get('repository/{name}', function ($name) use ($app) {
$repository = $app['repositoryFactory']->fromName($name);
return $app['twig']->render(
'repository.html',
array(
'repositories' => $app['repositories'],
'name' => $repository->getName(),
'branch' => $repository->gitter->getCurrentBranch(),
'statsEndpoint' => $app["request"]->getBaseUrl() . "/git-stats/" . $name,
)
);
});
$app->get('/git-stats/{name}', function($name) use($app) {
$cache_file = __DIR__.'/cache/'.$name.'.json';
$from_cache
= isset($app['config']['cacheTime'])
&& $app['config']['cacheTime']
&& file_exists($cache_file)
&& filemtime($cache_file) > (time() - $app['config']['cacheTime']);
if ($from_cache) {
$statistics = json_decode(file_get_contents($cache_file));
} else {
$repository = $app['repositoryFactory']->fromName($name);
$repository->loadCommits();
$statistics = $repository->getStatistics();
file_put_contents($cache_file, json_encode($statistics));
}
return $app->json(
$statistics
);
});
$app['debug'] = true;
$app->run();