-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.php
167 lines (142 loc) · 5.46 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
require_once __DIR__.'/vendor/autoload.php';
use Silex\Provider\DoctrineServiceProvider;
use Silex\Provider\FormServiceProvider;
use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Knp\Provider\RepositoryServiceProvider;
use Symfony\Component\Validator\Constraints as Assert;
use Century\Controller\RideModificationController;
use Century\Controller\UserRegistrationController;
use Century\Controller\UserProfileController;
use Century\Controller\RideDisplayController;
use Century\Controller\LeaderboardController;
use StravaDL\StravaDownloader;
use Mailgun\Mailgun;
$app = new Silex\Application();
//Debug mode
$app['debug'] = getenv("APP_ENV") == "dev";
/**
Register Services
**/
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new FormServiceProvider());
$app->register(new TranslationServiceProvider(), array(
'locale_fallback' => 'en',
));
$app->register(new Silex\Provider\ValidatorServiceProvider());
$app->register(new TwigServiceProvider(), array(
'twig.path' => __DIR__ . '/views',
));
$app->register(new DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_mysql',
'host' => getenv('VDT_DB_STRAWBERRY_HOST'),
'dbname' => getenv('VDT_DB_STRAWBERRY_NAME'),
'user' => getenv('VDT_DB_STRAWBERRY_USER'),
'password' => getenv('VDT_DB_STRAWBERRY_PASS'),
)
));
$app->register(new RepositoryServiceProvider(), array('repository.repositories' => array(
'rides' => 'Century\\Repository\\RideRepo',
'users' => 'Century\\Repository\\UserRepo'
)));
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'pattern' => '^.*$',
'anonymous' => true,
'form' => array('login_path' => '/login', 'check_path' => '/login_check'),
'logout' => array('logout_path' => '/logout'),
'users' => $app->share(function() use ($app) {
return new Century\Provider\UserProvider($app['db']);
})
),
),
'security.access_rules' => array(
// You can rename ROLE_USER as you wish
array('^/add', 'ROLE_USER'),
array('^/add/0|[1-9][0-9]*/edit', 'ROLE_USER'),
array('^/add/0|[1-9][0-9]*/delete', 'ROLE_USER'),
array('^/profile/^.*$/edit', 'ROLE_USER')
)
));
//Twig Options/Extensions
$app['twig'] = $app->share($app->extend('twig', function($twig, $app) {
$twig->addFilter('miles', new \Twig_Filter_Function('miles'));
$twig->addFilter('km', new \Twig_Filter_Function('km'));
function miles($km)
{
return round((int) $km * 0.621371192, 1);
}
function km($km)
{
return $km;
}
return $twig;
}));
$client_secret = getenv('STRAVA_SECRET');
$client_id = getenv('STRAVA_ID');
$app['strava.key'] = getenv('STRAVA_KEY');
$app['stravaDL'] = $app->share(function () use ($client_secret, $client_id){
return new StravaDownloader($client_secret, $client_id);
});
$app['mailgun'] = $app->share(function (){
return new Mailgun(getenv('MAILGUN_KEY'));
});
/*
$app['miles_to_km'] = $app->share(function() use ($app) {
return new MilesToKmDataTransformer($app);
});
$app['km_to_miles'] = $app->share(function() use ($app) {
return new KmToMilesDataTransformer($app);
});
*/
/**
Register Controllers:
**/
$app['ride.modification.controller'] = $app->share(function() use ($app) {
return new RideModificationController($app);
});
$app['user.registration.controller'] = $app->share(function() use ($app) {
return new UserRegistrationController($app);
});
$app['user.profile.controller'] = $app->share(function() use ($app) {
return new UserProfileController($app);
});
$app['ride.display.controller'] = $app->share(function() use ($app) {
return new RideDisplayController($app);
});
$app['leaderboard.controller'] = $app->share(function() use ($app) {
return new LeaderboardController($app);
});
/**
Controllers:
**/
//Ride Modification:
$app->match('/add', "ride.modification.controller:addRidePage");
$app->post('/add/manual', "ride.modification.controller:addRideManual");
$app->post('/add/strava', "ride.modification.controller:addRideStrava");
$app->match('/ride/{id}/edit', "ride.modification.controller:editRide");
$app->match('/ride/{id}/delete', "ride.modification.controller:deleteRide");
//Ride Display:
//$app->get('/rides', "ride_display.controller:allRides");
//$app->get('/ride/{ride_id}', "ride_display.controller:singleRide");
//User Registration:
$app->match('/login', "user.registration.controller:login");
$app->match('/register', "user.registration.controller:register");
$app->match('/resetpassword', "user.registration.controller:resetPassword");
//Profile
$app->get('/profile/{username}', "user.profile.controller:displayProfile");
$app->match('/profile/{username}/edit', "user.profile.controller:editProfile");
$app->match('/profile/{username}/changepassword', "user.profile.controller:changePassword");
//Leaderboard
$app->get('/leaderboard', "leaderboard.controller:leaderboard");
$app->get('/leaderboard/{year}', "leaderboard.controller:leaderboardByYear")->assert('year', '\d{4}');
//Index
$app->get('/', function () use ($app) {
return $app['twig']->render('index.html.twig', $app['leaderboard.controller']->getLeaderboardData());
});
$app->run();