This repository has been archived by the owner on Feb 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 87
/
app.php
135 lines (113 loc) · 4.23 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
131
132
133
134
135
<?php
/*
* This file is part of the Sismo utility.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\UrlGeneratorServiceProvider;
use Silex\Provider\SymfonyBridgesServiceProvider;
use Sismo\Sismo;
use Sismo\Project;
use Sismo\Storage\Storage;
use Sismo\Builder;
use Symfony\Component\Process\Process;
use Symfony\Component\HttpFoundation\Response;
$app = new Application();
$app->register(new SymfonyBridgesServiceProvider());
$app->register(new UrlGeneratorServiceProvider());
$app->register(new TwigServiceProvider(), array(
'twig.path' => __DIR__.'/templates',
'twig.configure' => $app->protect(function ($twig) use ($app) {
$twig->setCache($app['twig.cache.path']);
}),
));
$app['data.path'] = getenv('SISMO_DATA_PATH') ?: getenv('HOME').'/.sismo/data';
$app['config.file'] = getenv('SISMO_CONFIG_PATH') ?: getenv('HOME').'/.sismo/config.php';
$app['config.storage.file'] = getenv('SISMO_STORAGE_PATH') ?: getenv('HOME').'/.sismo/storage.php';
$app['build.path'] = $app->share(function ($app) { return $app['data.path'].'/build'; });
$app['db.path'] = $app->share(function ($app) {
if (!is_dir($app['data.path'])) {
mkdir($app['data.path'], 0777, true);
}
return $app['data.path'].'/sismo.db';
});
$app['twig.cache.path'] = $app->share(function ($app) { return $app['data.path'].'/cache'; });
$app['git.path'] = getenv('SISMO_GIT_PATH') ?: 'git';
$app['git.cmds'] = array();
$app['db.schema'] = <<<EOF
CREATE TABLE IF NOT EXISTS project (
slug TEXT,
name TEXT,
repository TEXT,
branch TEXT,
command BLOB,
url_pattern TEXT,
PRIMARY KEY (slug)
);
CREATE TABLE IF NOT EXISTS `commit` (
slug TEXT,
sha TEXT,
date TEXT,
message BLOB,
author TEXT,
status TEXT,
output BLOB,
build_date TEXT DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (slug, sha),
CONSTRAINT slug FOREIGN KEY (slug) REFERENCES project(slug) ON DELETE CASCADE
);
EOF;
$app['db'] = $app->share(function () use ($app) {
$db = new \SQLite3($app['db.path']);
$db->busyTimeout(1000);
$db->exec($app['db.schema']);
return $db;
});
$app['storage'] = $app->share(function () use ($app) {
if (is_file($app['config.storage.file'])) {
$storage = require $app['config.storage.file'];
} else {
$storage = new Storage($app['db']);
}
return $storage;
});
$app['builder'] = $app->share(function () use ($app) {
$process = new Process(sprintf('%s --version', $app['git.path']));
if ($process->run() > 0) {
throw new \RuntimeException(sprintf('The git binary cannot be found (%s).', $app['git.path']));
}
return new Builder($app['build.path'], $app['git.path'], $app['git.cmds']);
});
$app['sismo'] = $app->share(function () use ($app) {
$sismo = new Sismo($app['storage'], $app['builder']);
if (!is_file($app['config.file'])) {
throw new \RuntimeException(sprintf("Looks like you forgot to define your projects.\nSismo looked into \"%s\".", $app['config.file']));
}
$projects = require $app['config.file'];
if (null === $projects) {
throw new \RuntimeException(sprintf('The "%s" configuration file must return an array of Projects (returns null).', $app['config.file']));
}
if (!is_array($projects)) {
throw new \RuntimeException(sprintf('The "%s" configuration file must return an array of Projects (returns a non-array).', $app['config.file']));
}
foreach ($projects as $project) {
if (!$project instanceof Project) {
throw new \RuntimeException(sprintf('The "%s" configuration file must return an array of Project instances.', $app['config.file']));
}
$sismo->addProject($project);
}
return $sismo;
});
$app->error(function (\Exception $e, $code) use ($app) {
if ($app['debug']) {
return;
}
$error = 404 == $code ? $e->getMessage() : null;
return new Response($app['twig']->render('error.twig', array('error' => $error)), $code);
});
return $app;