TODO Move all "features" things to a class with index() and get() methods
| 8 | |
| 9 | // TODO Move all "features" things to a class with index() and get() methods |
| 10 | class Application extends Slim |
| 11 | { |
| 12 | public $configDirectory; |
| 13 | public $config; |
| 14 | |
| 15 | protected function initConfig() |
| 16 | { |
| 17 | $config = array(); |
| 18 | if (!file_exists($this->configDirectory) || !is_dir($this->configDirectory)) { |
| 19 | throw new Exception('Config directory is missing: ' . $this->configDirectory, 500); |
| 20 | } |
| 21 | foreach (preg_grep('/\\.php$/', scandir($this->configDirectory)) as $filename) { |
| 22 | $config = array_replace_recursive($config, include $this->configDirectory . '/' . $filename); |
| 23 | } |
| 24 | return $config; |
| 25 | } |
| 26 | |
| 27 | public function __construct(array $userSettings = array(), $configDirectory = 'config') |
| 28 | { |
| 29 | // Slim initialization |
| 30 | parent::__construct($userSettings); |
| 31 | $this->config('debug', false); |
| 32 | $this->notFound(function () { |
| 33 | $this->handleNotFound(); |
| 34 | }); |
| 35 | $this->error(function ($e) { |
| 36 | $this->handleException($e); |
| 37 | }); |
| 38 | |
| 39 | // Config |
| 40 | $this->configDirectory = __DIR__ . '/../../' . $configDirectory; |
| 41 | $this->config = $this->initConfig(); |
| 42 | |
| 43 | // /features |
| 44 | $this->get('/features', function () { |
| 45 | $features = new Features($this->config['features']); |
| 46 | $this->response->headers->set('Content-Type', 'application/json'); |
| 47 | $this->response->setBody(json_encode($features->getFeatures())); |
| 48 | }); |
| 49 | |
| 50 | $this->get('/features/:id', function ($id) { |
| 51 | $features = new Features($this->config['features']); |
| 52 | $feature = $features->getFeature($id); |
| 53 | if ($feature === null) { |
| 54 | return $this->notFound(); |
| 55 | } |
| 56 | $this->response->headers->set('Content-Type', 'application/json'); |
| 57 | $this->response->setBody(json_encode($feature)); |
| 58 | }); |
| 59 | } |
| 60 | |
| 61 | public function handleNotFound() |
| 62 | { |
| 63 | throw new Exception( |
| 64 | 'Resource ' . $this->request->getResourceUri() . ' using ' |
| 65 | . $this->request->getMethod() . ' method does not exist.', |
| 66 | 404 |
| 67 | ); |
nothing calls this directly
no outgoing calls
no test coverage detected