protected function registerController($name, $controller, $mount = '') { if (!isset($controller['match'])) { throw new InvalidRouteConfigurationException(sprintf('Your route confugiration for %s is invalid. You need to provide a \'match\' key to match a url', $name)); } if (!isset($controller['controller'])) { throw new InvalidRouteConfigurationException(sprintf('Your route confugiration for %s is invalid. You need to provide a \'controller\' key that maps to a function or class method', $name)); } try { // create closure of controller class function $method = Utils::getClosure($controller['controller']); } catch (\Exception $e) { throw new InvalidRouteConfigurationException(sprintf('Your route configuration %s is invalid. %s', $name, $e->getMessage())); } $route = preg_replace(array('/\\/+/', '/\\/$/'), array('/', ''), $mount . $controller['match']); $route = preg_replace('/^\\//', '', $route); $route = $this->match($route, $method); // bind route to name $route->bind($name); // handle any defined request methods if (isset($controller['request_methods'])) { if (is_array($controller['request_methods'])) { $methods = implode('|', $controller['request_methods']); } else { $methods = $controller['request_methods']; } //@TODO: check format of methods string and that it is a string else throw InvalidRouteConfigurationException $route->method($methods); } // handle any defined converts if (isset($controller['convert'])) { if (!is_array($controller['convert'])) { throw new InvalidRouteConfigurationException(sprintf("Problem with your defined converts in route '%s' config. Please provide a key pair array e.g. \"param1\": \"\\Class::convertMethod\"", $name)); } foreach ($controller['convert'] as $param => $convert_method) { try { // create closure of controller class function $method = Utils::getClosure($convert_method); } catch (\Exception $e) { throw new InvalidRouteConfigurationException(sprintf('Your defined convert for param %s in route configuration %s is invalid. %s', $param, $name, $e->getMessage())); } $route->convert($param, $method); } } // handle any defined assertions if (isset($controller['assert'])) { if (!is_array($controller['assert'])) { throw new InvalidRouteConfigurationException(sprintf("Problem with your defined assertions in route '%s' config. Please provide a key pair array e.g. \"param1\": \"\\Class::assertMethod\"", $name)); } foreach ($controller['assert'] as $param => $assertion) { $route->assert($param, $assertion); } } // handle any defined default values if (isset($controller['default_values'])) { if (!is_array($controller['default_values'])) { throw new InvalidRouteConfigurationException(sprintf("Problem with your defined default values in route '%s' config. Please provide a key pair array e.g. \"param1\": \"value\"", $name)); } foreach ($controller['default_values'] as $param => $value) { $route->value($param, $value); } } if (isset($controller['response_headers']) && is_array($controller['response_headers'])) { $route->after(function (Request $request, Response $response) use($controller) { foreach ($controller['response_headers'] as $key => $value) { $response->headers->set($key, $value); } }); } $app = $this; // configure Route middleware if (isset($controller['middleware'])) { if (!is_array($controller['middleware'])) { throw new InvalidConfigurationException(sprintf('The defined middleware hooks configuration for route \'%s\' config is invalid. Please provide an array of functions to call.', $name)); } foreach ($controller['middleware'] as $hook) { $route->{$hook}['type'](Utils::getClosure($hook['method'], function ($object) use($app) { if ($object instanceof \Skip\ServiceContainerInterface) { $object->setContainer($app); } })); } } }
protected function transformContext($context) { foreach ($this->context_transformers as $class) { $method = \Skip\Utils::getClosure($class . '::transform'); $context = $method($this->app, $context); } return $context; }