index.php 5.16 KB
Newer Older
Zachary Seguin's avatar
Zachary Seguin committed
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
<?php

/*
    Copyright (c) 2015 Zachary Seguin

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.
*/

namespace ZacharySeguin\YouWantFood;

require_once(__DIR__ . '/vendor/autoload.php');
require_once(__DIR__ . '/config.php');

30 31
use Symfony\Component\HttpFoundation\Response;

Zachary Seguin's avatar
Zachary Seguin committed
32
$app = new \Silex\Application();
Zachary Seguin's avatar
Zachary Seguin committed
33
$app['debug'] = false;
Zachary Seguin's avatar
Zachary Seguin committed
34
$app->register(new \Silex\Provider\ServiceControllerServiceProvider());
35
$app->register(new \Silex\Provider\UrlGeneratorServiceProvider());
Zachary Seguin's avatar
Zachary Seguin committed
36

37 38 39 40
$app['database.controller'] = $app->share(function() {
   return new Controller\DatabaseController(DATABASE_HOSTNAME, DATABASE_PORT, DATABASE_DB, DATABASE_USER, DATABASE_PASSWORD);
});

41 42 43 44
$app['email.controller'] = $app->share(function() {
   return new Controller\EmailController(EMAIL_HOSTNAME, EMAIL_PORT, EMAIL_SSL, EMAIL_USER, EMAIL_PASSWORD, json_decode(EMAIL_FROM, true));
});

Zachary Seguin's avatar
Zachary Seguin committed
45 46 47 48 49 50 51 52
$app['frontend.controller'] = $app->share(function() {
   return new Controller\FrontendController();
});

$app['uwaterloo-api.controller'] = $app->share(function() {
   return new Controller\UWaterlooAPIController(UWATERLOO_API_KEY, UWATERLOO_API_BASE);
});

53
$app['email-subscription.controller'] = $app->share(function($app) {
54
   return new Controller\EmailSubscriptionController($app['database.controller'], $app['email.controller']);
55 56
});

Zachary Seguin's avatar
Zachary Seguin committed
57
$app['reviews.controller'] = $app->share(function($app) {
58
   return new Controller\ReviewsController($app['database.controller'], $app['email.controller']);
Zachary Seguin's avatar
Zachary Seguin committed
59 60
});

61 62
$app['inspections.controller'] = $app->share(function($app) {
   return new Controller\InspectionsController($app['database.controller']);
Zachary Seguin's avatar
Zachary Seguin committed
63 64 65
});

$app->get('/', "frontend.controller:frontendAction");
Zachary Seguin's avatar
Zachary Seguin committed
66 67 68 69
$app->get('/not-found', "frontend.controller:frontendAction")
      ->bind('/not-found');
$app->get('/error', "frontend.controller:frontendAction")
      ->bind('/error');
Zachary Seguin's avatar
Zachary Seguin committed
70
$app->get('/about', "frontend.controller:frontendAction");
Zachary Seguin's avatar
Zachary Seguin committed
71 72
$app->get('/outlet/{outlet_id}', "frontend.controller:frontendAction")
   ->assert('outlet_id', '\d+');
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
$app->get('/email/subscribe', "frontend.controller:frontendAction")
   ->bind('/email/subscribe');
$app->get('/email-subscribe', function() use($app) {
   return $app->redirect($app['url_generator']->generate('/email/subscribe'), 301);
});
$app->get('/email/unsubscribe', "frontend.controller:frontendAction")
   ->bind('/email/unsubscribe');
$app->get('/email-unsubscribe', function() use($app) {
   return $app->redirect($app['url_generator']->generate('/email/unsubscribe'), 301);
});
$app->get('/email/confirmed', "frontend.controller:frontendAction")
   ->bind('/email/confirmed');
$app->get('/email/confirmation-error', "frontend.controller:frontendAction")
   ->bind('/email/confirmation-error');

$app->get('/email/confirm/{token}', "email-subscription.controller:confirmAction");
Zachary Seguin's avatar
Zachary Seguin committed
89 90 91 92 93 94 95 96 97

$app->get('/api/outlets.json', "uwaterloo-api.controller:outletsAction");
$app->get('/api/menu.json', "uwaterloo-api.controller:menuAction");
$app->get('/api/menu/{outlet_id}.json', "uwaterloo-api.controller:menuForOutletAction")
   ->assert('outlet_id', '\d+');

$app->post('/api/email/subscribe.json', "email-subscription.controller:subscribeAction");
$app->post('/api/email/unsubscribe.json', "email-subscription.controller:unsubscribeAction");

Zachary Seguin's avatar
Zachary Seguin committed
98 99 100 101
$app->get('/api/reviews/outlet/{outlet_id}.json', 'reviews.controller:outletAction')
   ->assert('outlet_id', '\d+');
$app->post('/api/reviews/outlet/{outlet_id}/add.json', 'reviews.controller:addOutletReviewAction')
   ->assert('outlet_id', '\d+');
102
$app->get('/reviews/outlets/reject/{moderation_token}', 'reviews.controller:rejectOutletReviewAction');
Zachary Seguin's avatar
Zachary Seguin committed
103

104 105
$app->get('/api/inspections/facilities.json', 'inspections.controller:facilitiesAction');
$app->get('/api/inspections/facility/{facility_id}.json', 'inspections.controller:facilityAction');
106 107
$app->get('/api/inspections/uwaterloo/{uwaterloo_id}.json', 'inspections.controller:uwaterlooAction')
   ->assert('uwaterloo_id', '\d+');
108

Zachary Seguin's avatar
Zachary Seguin committed
109 110 111 112
$app->error(function (\Exception $e, $code) use ($app) {
   if ($code == 404) {
      return $app->redirect($app['url_generator']->generate('/not-found'));
   } else {
113
      return new Response('Sorry, an error occurred.', $code);// $app->redirect($app['url_generator']->generate('/error'));
Zachary Seguin's avatar
Zachary Seguin committed
114 115 116
   }// End of if
});

Zachary Seguin's avatar
Zachary Seguin committed
117 118 119
$app->run();

?>