|
| 1 | +<?php |
| 2 | + |
| 3 | +use Symfony\Component\HttpFoundation\Request; |
| 4 | +use Symfony\Component\HttpFoundation\JsonResponse; |
| 5 | + |
| 6 | +class eventController extends SectionEvent |
| 7 | +{ |
| 8 | + public static function about() |
| 9 | + { |
| 10 | + return [ |
| 11 | + 'name' => 'Symphony API Framework: Event Controller', |
| 12 | + 'author' => array( |
| 13 | + 'name' => 'Alistair Kearney', |
| 14 | + 'website' => 'http://cp.kickd', |
| 15 | + 'email' => 'alistair@ruleandmake.com'), |
| 16 | + 'version' => 'Symphony 2.6.3', |
| 17 | + 'release-date' => '2015-08-28T02:10:22+00:00', |
| 18 | + 'trigger-condition' => 'POST|PUT|PATCH|DELETE' |
| 19 | + ]; |
| 20 | + } |
| 21 | + |
| 22 | + public function load() |
| 23 | + { |
| 24 | + $request = Request::createFromGlobals(); |
| 25 | + |
| 26 | + // This ensures the composer autoloader for the framework is included |
| 27 | + Symphony::ExtensionManager()->create('api_framework'); |
| 28 | + |
| 29 | + // Event controllor only responds to certain methods. GET is handled by the data sources |
| 30 | + if($request->getMethod() == 'GET'){ |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) { |
| 35 | + $data = json_decode($request->getContent(), true, 512, JSON_BIGINT_AS_STRING); |
| 36 | + $request->request->replace(is_array($data) ? $data : []); |
| 37 | + } |
| 38 | + |
| 39 | + //@TODO: Use the current path to resolve controllers deeper than 1 level |
| 40 | + //Frontend::instance()->Page()->Params()['current-path'] |
| 41 | + |
| 42 | + $controllerPath = WORKSPACE . '/controllers'; |
| 43 | + $controllerName = 'Controller' . ucfirst(Frontend::instance()->Page()->pageData()['handle']); |
| 44 | + |
| 45 | + include_once WORKSPACE . "/controllers/$controllerName.php"; |
| 46 | + $controller = new $controllerName(); |
| 47 | + $method = strtolower($request->getMethod()); |
| 48 | + |
| 49 | + if(!method_exists($controller, $method)){ |
| 50 | + throw new \Exception("405 method not found (".$request->getMethod().")"); |
| 51 | + } |
| 52 | + |
| 53 | + $controller->execute(); |
| 54 | + |
| 55 | + // Prepare the response. |
| 56 | + $response = new JsonResponse(); |
| 57 | + $response->headers->set('Content-Type', 'application/json'); |
| 58 | + |
| 59 | + $response = $controller->$method($request, $response); |
| 60 | + $response->send(); |
| 61 | + exit; |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + public static function documentation() |
| 66 | + { |
| 67 | + return '<h3>Event Controller</h3><p>Handles passing off work to controllors depending on what has been requested.</p>'; |
| 68 | + } |
| 69 | +} |
0 commit comments