From 258b1545c3b28bb44ddc7b23493debc28446541f Mon Sep 17 00:00:00 2001 From: Jordan Johnson-Doyle Date: Mon, 25 Feb 2019 22:19:35 +0000 Subject: [PATCH] Add routing method for posts page --- src/Routing/PostsRoute.php | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Routing/Routing.php | 13 +++++++++++++ src/Routing/RoutingServiceProvider.php | 1 + 3 files changed, 87 insertions(+) diff --git a/src/Routing/PostsRoute.php b/src/Routing/PostsRoute.php index e69de29..291ded7 100644 --- a/src/Routing/PostsRoute.php +++ b/src/Routing/PostsRoute.php @@ -0,0 +1,73 @@ + + */ +class PostsRoute extends Route +{ + /** + * Create a new Route instance. + * + * @param array|string $methods + * @param \Closure|array $action + * + * @return void + */ + public function __construct($methods, $action) + { + if (!get_option('page_for_posts')) { + trigger_error('Attempted to define a posts route when the posts page isn\'t set. Set it in the Reading Settings in Wordpress.', E_USER_NOTICE); + } + + parent::__construct($methods, wp_make_link_relative(get_permalink(get_option('page_for_posts'))) ?: '/posts', $action); + } + + /** + * Determine if the route matches given request. + * + * @param Request $request + * @param bool $includingMethod + * + * @return bool + */ + public function matches(Request $request, $includingMethod = true) + { + return query()->isPostsPage ?? false; + } + + /** + * Run the route action and return the response. + * + * @return mixed + */ + protected function runCallable() + { + // bind the current post to the parameters of the function + $function = new ReflectionFunction($this->action['uses']); + $params = $function->getParameters(); + + foreach ($params as $param) { + if ($param->getClass() + && ($param->getClass()->isSubclassOf(Post::class) || $param->getClass()->getName() === Post::class)) { + $builder = $param->getClass()->getMethod('query')->invoke(null); + $post = $builder->find(Wordpress::id()); + + $this->setParameter($param->getName(), $post); + } + } + + return parent::runCallable(); + } +} diff --git a/src/Routing/Routing.php b/src/Routing/Routing.php index f3b8d2a..bf085f7 100644 --- a/src/Routing/Routing.php +++ b/src/Routing/Routing.php @@ -80,6 +80,19 @@ class Routing return Route::getRoutes()->add($route); } + public function posts($action) + { + $action = $this->formatAction($action); + + $route = (new PostsRoute($action['method'], $action)) + ->setRouter(app('router')) + ->setContainer(app(Container::class)); + + $route = $this->applyStack($route); + + return Route::getRoutes()->add($route); + } + /** * Register a new archive route with the router. Optionally supply * the post types you'd like to supply with this route. diff --git a/src/Routing/RoutingServiceProvider.php b/src/Routing/RoutingServiceProvider.php index e81e990..936ca26 100644 --- a/src/Routing/RoutingServiceProvider.php +++ b/src/Routing/RoutingServiceProvider.php @@ -30,5 +30,6 @@ class RoutingServiceProvider extends ServiceProvider Router::macro('singular', [$routing, 'singular']); Router::macro('author', [$routing, 'author']); Router::macro('category', [$routing, 'category']); + Router::macro('posts', [$routing, 'posts']); } } -- libgit2 1.7.2