Skip to content

Commit

Permalink
Add possibility to call route methods from SlimRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtahavranek committed Dec 6, 2023
1 parent 6fbc10c commit e172214
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
},
"require": {
"php": ">=7.2",
"php": ">=7.4",
"psr/http-message": "^1.0"
},
"suggest": {
Expand All @@ -30,6 +30,9 @@
},
"config": {
"sort-packages": true,
"process-timeout": 600
"process-timeout": 600,
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}
16 changes: 16 additions & 0 deletions src/Bridge/Slim/SlimRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use BrandEmbassy\Router\Route;
use Slim\Interfaces\RouteInterface;
use function assert;
use function call_user_func_array;
use function is_callable;

/**
* @final
Expand Down Expand Up @@ -37,4 +39,18 @@ public function getCallable(): callable

return $this->route->getCallable();
}


/**
* @param mixed $arguments
*
* @return mixed
*/
public function __call(string $method, $arguments)
{
$callable = [$this->route, $method];
assert(is_callable($callable));

return call_user_func_array($callable, $arguments);
}
}
6 changes: 2 additions & 4 deletions src/Bridge/Slim/SlimRouterNetteDiFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,12 @@ private static function createSlimRouter(Container $container, array $routes): R
throw new LogicException(sprintf('Route with pattern: "%s" must have name.', $pattern));
}

$callbackProvider = function () use ($container, $data) {
return self::getService($container, $data['service']);
};
$callbackProvider = fn() => self::getService($container, $data['service']);

$route = $router->map(
explode(self::METHOD_DELIMITER, $method),
$pattern,
new RouteCallbackAccessor($callbackProvider)
new RouteCallbackAccessor($callbackProvider),
);
$route->setName($data['name']);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/Slim/SlimRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testPathFor(): void
['GET'],
'/foo/{foo}/bar/{bar}',
function (): void {
}
},
);
$route->setName('fooRoute');
$router = new SlimRouter($slimRouter);
Expand Down

0 comments on commit e172214

Please sign in to comment.