Meta helpers for post & autobind post to the method params
Diff
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(-)
@@ -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
@@ -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';
@@ -1,40 +0,0 @@
<?php
namespace Koselig\Routing;
use Illuminate\Http\Request;
use Illuminate\Routing\Route as LaravelRoute;
use Illuminate\Support\Facades\Input;
use Symfony\Component\Routing\Route as SymfonyRoute;
/**
* Extend the base Laravel routing functionality to add multisite support
* to Route::get, Route::post, etc methods.
*
* @author Jordan Doyle <jordan@doyle.wf>
*/
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;
}
}
@@ -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