From cee204d0dfac455688d7e00c97d6fc7c951c3f51 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Thu, 6 Oct 2016 17:59:33 +0100 Subject: [PATCH] Meta helpers for post & autobind post to the method params --- src/Models/Post.php | 26 ++++++++++++++++++++++++++ src/Providers/WordpressServiceProvider.php | 1 + src/Routing/Route.php | 40 ---------------------------------------- src/Routing/SingularRoute.php | 26 ++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 40 deletions(-) delete mode 100644 src/Routing/Route.php diff --git a/src/Models/Post.php b/src/Models/Post.php index c33ecc2..bd610dc 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -2,6 +2,7 @@ namespace Koselig\Models; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -50,6 +51,31 @@ class Post extends Model } /** + * Get meta values for this Post. + * + * @param array|string|null $key + * @return mixed + */ + public function getMeta($key = null) + { + /** + * @var Collection + */ + $meta = $this->meta; + + if (is_array($key)) { + $meta = $meta->whereIn('meta_key', $key); + } elseif (is_string($key)) { + $meta = $meta->where('meta_key', $key)->first(); + return $meta ? $meta->meta_value : null; + } + + return $meta->mapWithKeys(function ($item) { + return [$item->meta_key => $item->meta_value]; + })->all(); + } + + /** * Get the author that this post belongs to. * * @return BelongsTo diff --git a/src/Providers/WordpressServiceProvider.php b/src/Providers/WordpressServiceProvider.php index 183b476..d9aa5f5 100644 --- a/src/Providers/WordpressServiceProvider.php +++ b/src/Providers/WordpressServiceProvider.php @@ -76,6 +76,7 @@ class WordpressServiceProvider extends ServiceProvider if ($this->app->runningInConsole()) { // allow wordpress to run, even when running from console (ie. artisan compiling) $_SERVER['SERVER_PROTOCOL'] = 'https'; + $_SERVER['HTTP_HOST'] = parse_url(config('app.url'))['host']; } require ABSPATH . 'wp-settings.php'; diff --git a/src/Routing/Route.php b/src/Routing/Route.php deleted file mode 100644 index cd50c6e..0000000 --- a/src/Routing/Route.php +++ /dev/null @@ -1,40 +0,0 @@ - - */ -class Route extends LaravelRoute -{ - /** - * Determine if the route matches given request. - * - * @param \Illuminate\Http\Request $request - * @param bool $includingMethod - * @return bool - */ - public function matches(Request $request, $includingMethod = true) - { - $this->compileRoute(); - - foreach ($this->getValidators() as $validator) { - if (!$includingMethod && $validator instanceof MethodValidator) { - continue; - } - - if (!$validator->matches($this, $request)) { - return false; - } - } - - return true; - } -} diff --git a/src/Routing/SingularRoute.php b/src/Routing/SingularRoute.php index 8753b29..09904ec 100644 --- a/src/Routing/SingularRoute.php +++ b/src/Routing/SingularRoute.php @@ -3,7 +3,9 @@ namespace Koselig\Routing; use Illuminate\Http\Request; use Illuminate\Routing\Route; +use Koselig\Models\Post; use Koselig\Support\Wordpress; +use ReflectionFunction; /** * Singular route, this route is matched when the user @@ -37,6 +39,30 @@ class SingularRoute extends Route } /** + * 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(); + } + + /** * Format a nice string for php artisan route:list. * * @return string -- libgit2 1.7.2