-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.php
76 lines (60 loc) · 2.04 KB
/
index.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
<?php
require_once 'vendor/autoload.php';
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
$app = new Silex\Application();
$app->register(
new Silex\Provider\TwigServiceProvider(),
array(
'twig.path' => 'views'
)
);
$app['http'] = $app->share(function() {
return new Guzzle\Service\Client('https://api.github.com');
});
$app['anonymous'] = $app->share(function() use ($app) {
$user = new GitHub\User($app['http']);
$user->login('rosetta-anon', 'rosetta123');
return $user;
});
$app['github'] = $app->share(function() use ($app) {
return new RestFest\GitHub($app['http'], $app['anonymous']);
});
function getHackdayResponse($content)
{
$response = new Response();
$response->headers->set('Content-Type', 'application/vnd.org.restfest.2012.hackday+xml');
$response->setContent($content);
return $response;
}
$app->get('/', function() use ($app) {
return getHackdayResponse($app['twig']->render('entry.twig', array('self' => "http://{$_SERVER['HTTP_HOST']}")));
});
$app->get('/tickets/', function() use ($app) {
return getHackdayResponse($app['github']->getIssues());
});
$app->post('/tickets/', function(Request $r) use ($app) {
try {
if ($number = $app['github']->createIssue($r->getContent())) {
return new Response('', 201, array('Location' => "http://{$_SERVER['HTTP_HOST']}/tickets/$number"));
}
} catch (\InvalidArgumentException $e) {
return new Response($e->getMessage(), 400);
}
return new Response('', 500);
});
$app->get('/tickets/{id}', function($id) use ($app) {
return getHackdayResponse($app['github']->getIssue($id));
});
$app->delete('/tickets/{id}', function($id) use ($app) {
if ($app['github']->deleteIssue($id)) {
return new Response('', 200);
}
});
$app->put('/tickets/{id}', function(Request $r, $id) use ($app) {
if ($app['github']->updateIssue($id, $r->getContent())) {
return new Response('', 201);
}
return new Response('', 500);
});
$app->run();