示例#1
0
 /**
  * Returns a callable given its string representation.
  *
  * @param string $name
  *
  * @return array A callable array
  *
  * @throws \InvalidArgumentException In case the method does not exist.
  */
 public function resolveCallback($name)
 {
     if (!is_callable($callback = parent::resolveCallback($name))) {
         if (!class_exists($name) || !in_array(Middleware::class, class_implements($name))) {
             throw new \Exception($name . ' is not a valid middleware class');
         }
         $callback = [$this->container->get($name), 'execute'];
     }
     return $callback;
 }
示例#2
0
 protected function addRoute(ControllerCollection $ctr, $name, array $config)
 {
     $config = new ArrayCollection($config);
     if (!($path = $config['path'])) {
         return;
     }
     if (!($defaults = $config['defaults'])) {
         return;
     }
     $defaults = new ArrayCollection($defaults);
     if (!($to = $defaults->remove('_controller'))) {
         return;
     }
     $route = $ctr->match($path, $to);
     $before = $defaults->remove('_before') ?: '::before';
     $before = $this->resolveBefore($before);
     $route->before($before);
     $after = $defaults->remove('_after') ?: '::after';
     $after = $this->resolveAfter($after);
     $route->after($after);
     foreach ($defaults as $key => $value) {
         $route->value($key, $value);
     }
     foreach ($config['requirements'] ?: [] as $variable => $callback) {
         $callback = $this->callbackResolver->resolveCallback($callback);
         $requirement = is_callable($callback) ? call_user_func($callback) : $callback;
         $route->assert($variable, $requirement);
     }
     if ($host = $config['host']) {
         $route->getRoute()->setHost($host);
     }
     if ($methods = $config['methods']) {
         $route->getRoute()->setMethods($methods);
     }
     if ($schemes = $config['schemes']) {
         $route->getRoute()->setSchemes($schemes);
     }
     $route->bind($name);
 }