ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
CallableResolver.php
Go to the documentation of this file.
1 <?php
9 namespace Slim;
10 
14 
20 {
21  const CALLABLE_PATTERN = '!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';
22 
26  private $container;
27 
32  {
33  $this->container = $container;
34  }
35 
49  public function resolve($toResolve)
50  {
51  if (is_callable($toResolve)) {
52  return $toResolve;
53  }
54 
55  if (!is_string($toResolve)) {
56  $this->assertCallable($toResolve);
57  }
58 
59  // check for slim callable as "class:method"
60  if (preg_match(self::CALLABLE_PATTERN, $toResolve, $matches)) {
61  $resolved = $this->resolveCallable($matches[1], $matches[2]);
62  $this->assertCallable($resolved);
63 
64  return $resolved;
65  }
66 
67  $resolved = $this->resolveCallable($toResolve);
68  $this->assertCallable($resolved);
69 
70  return $resolved;
71  }
72 
83  protected function resolveCallable($class, $method = '__invoke')
84  {
85  if ($this->container->has($class)) {
86  return [$this->container->get($class), $method];
87  }
88 
89  if (!class_exists($class)) {
90  throw new RuntimeException(sprintf('Callable %s does not exist', $class));
91  }
92 
93  return [new $class($this->container), $method];
94  }
95 
101  protected function assertCallable($callable)
102  {
103  if (!is_callable($callable)) {
104  throw new RuntimeException(sprintf(
105  '%s is not resolvable',
106  is_array($callable) || is_object($callable) ? json_encode($callable) : $callable
107  ));
108  }
109  }
110 }
__construct(ContainerInterface $container)
$container
Definition: wac.php:13
Describes the interface of a container that exposes methods to read its entries.
This class resolves a string of the format &#39;class:method&#39; into a closure that can be dispatched...
Slim Framework (https://slimframework.com)
Definition: App.php:9
resolve($toResolve)
Resolve toResolve into a closure so that the router can dispatch.
resolveCallable($class, $method='__invoke')
Check if string is something in the DIC that&#39;s callable or is a class name which has an __invoke() me...