From 42ee5ea11f3d074eff76f321d97c02d7a41be783 Mon Sep 17 00:00:00 2001 From: mmghv Date: Mon, 11 Sep 2017 18:04:47 +0200 Subject: [PATCH] Update to support Lumen5.5 --- .travis.yml | 1 + README.md | 8 ++++---- src/RouteBindingServiceProvider.php | 15 ++++++++++++++- tests/IntegratedTest.php | 4 +++- 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 58306e4..52b649b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,6 +5,7 @@ php: - 5.5 - 5.6 - 7.0 + - 7.1 script: - composer install diff --git a/README.md b/README.md index e865d35..75b2526 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Lumen Route Binding [![Build Status](https://travis-ci.org/mmghv/lumen-route-binding.svg?branch=master)](https://travis-ci.org/mmghv/lumen-route-binding) -[![Lumen Version](https://img.shields.io/badge/Lumen-5.0%20to%205.4-orange.svg)](https://github.com/laravel/lumen) +[![Lumen Version](https://img.shields.io/badge/Lumen-5.0%20to%205.5-orange.svg)](https://github.com/laravel/lumen) [![Latest Stable Version](https://poser.pugx.org/mmghv/lumen-route-binding/v/stable)](https://packagist.org/packages/mmghv/lumen-route-binding) [![Total Downloads](https://poser.pugx.org/mmghv/lumen-route-binding/downloads)](https://packagist.org/packages/mmghv/lumen-route-binding) [![Latest Unstable Version](https://poser.pugx.org/mmghv/lumen-route-binding/v/unstable)](https://packagist.org/packages/mmghv/lumen-route-binding) [![License](https://poser.pugx.org/mmghv/lumen-route-binding/license)](LICENSE) -This package Adds support for `Route Model Binding` in Lumen (5.0 to 5.4). +This package Adds support for `Route Model Binding` in Lumen (5.0 to 5.5). > As known, Lumen doesn't support `Route Model Binding` out of the box due to the fact that Lumen doesn't use the Illuminate router that Laravel uses, Instead, It uses the [FastRoute](https://github.com/nikic/FastRoute) which is much faster. With this package, We add support for the powerful `Route Model Binding` while still benefit the speed of the FastRoute in Lumen. @@ -35,7 +35,7 @@ composer require mmghv/lumen-route-binding "^1.0" #### Register the service provider -You have 2 options, continue reading .. +In the coming section .. ## Usage @@ -44,7 +44,7 @@ You have 2 options, continue reading .. ### Where to Define our Bindings -As mentioned before, You have 2 options : +You have 2 options : #### OPTION 1 diff --git a/src/RouteBindingServiceProvider.php b/src/RouteBindingServiceProvider.php index bb0766d..37cb92b 100644 --- a/src/RouteBindingServiceProvider.php +++ b/src/RouteBindingServiceProvider.php @@ -60,11 +60,24 @@ protected function getRoutesResolver() $routeCollector = new RouteCollector(new RouteParser, new DataGenerator); // Get routes data from application - foreach ($this->app->getRoutes() as $route) { + foreach ($this->getRoutes() as $route) { $routeCollector->addRoute($route['method'], $route['uri'], $route['action']); } return $routeCollector->getData(); }; } + + /** + * Get routes data. + * + * @return array + */ + protected function getRoutes() + { + // Support lumen < 5.5 by checking for the router property. + $router = property_exists($this->app, 'router') ? $this->app->router : $this->app; + + return $router->getRoutes(); + } } diff --git a/tests/IntegratedTest.php b/tests/IntegratedTest.php index 1e83048..ed16c43 100644 --- a/tests/IntegratedTest.php +++ b/tests/IntegratedTest.php @@ -26,8 +26,10 @@ public function testRouteBindingAndItsServiceProviderWorksAsExpectedWithLumen() return "{$val} Resolved"; }); + $router = isset($app->router) ? $app->router : $app; + // Register a route with a wildcard - $app->get('/{wildcard}', function ($wildcard) { + $router->get('/{wildcard}', function ($wildcard) { return response($wildcard); });