From 2bd278137cadda421f1c26cba356128c8668ee37 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Tue, 25 Oct 2016 17:54:53 +0100 Subject: [PATCH] Include Rememberable --- composer.json | 3 ++- composer.lock | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- config/wordpress.php | 1 + src/Models/Comment.php | 17 +++++++++++++++++ src/Models/Meta.php | 17 +++++++++++++++++ src/Models/Option.php | 17 +++++++++++++++++ src/Models/Post.php | 14 ++++++++++++++ src/Models/Term.php | 17 +++++++++++++++++ src/Models/TermTaxonomy.php | 17 +++++++++++++++++ src/Models/User.php | 18 +++++++++++++++++- src/Models/UserMeta.php | 28 ++++++++++++++++++++++++++++ 11 files changed, 197 insertions(+), 4 deletions(-) diff --git a/composer.json b/composer.json index 75adac0..ffed439 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,8 @@ "require": { "illuminate/support": "5.3.*", "illuminate/database": "^5.3", - "illuminate/routing": "^5.3" + "illuminate/routing": "^5.3", + "watson/rememberable": "^2.0" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index b172d58..aff464b 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "6c58ad1439bbf1caa3093d16a54c1ed4", - "content-hash": "422f593da2fdd296c003567b90182d68", + "hash": "e07950081a6df76470a24f8aa9be508e", + "content-hash": "68f5b8ac3064bd34ac440dec5170d899", "packages": [ { "name": "doctrine/inflector", @@ -1112,6 +1112,54 @@ "description": "Symfony Translation Component", "homepage": "https://symfony.com", "time": "2016-08-05 08:37:39" + }, + { + "name": "watson/rememberable", + "version": "2.0.0", + "source": { + "type": "git", + "url": "https://github.com/dwightwatson/rememberable.git", + "reference": "7ab6978f8dfaea8864028128faea854440c7af81" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dwightwatson/rememberable/zipball/7ab6978f8dfaea8864028128faea854440c7af81", + "reference": "7ab6978f8dfaea8864028128faea854440c7af81", + "shasum": "" + }, + "require": { + "illuminate/database": "~5.0", + "illuminate/support": "~5.0", + "php": ">=5.4.0" + }, + "require-dev": { + "mockery/mockery": "0.9.*", + "phpunit/phpunit": "4.2.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "Watson\\Rememberable\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Dwight Watson", + "email": "dwight@studiousapp.com" + } + ], + "description": "Query caching for Laravel 5", + "keywords": [ + "caching", + "eloquent", + "laravel", + "remember" + ], + "time": "2016-08-23 11:22:20" } ], "packages-dev": [], diff --git a/config/wordpress.php b/config/wordpress.php index e88ee69..2b5835c 100644 --- a/config/wordpress.php +++ b/config/wordpress.php @@ -16,4 +16,5 @@ return [ 'path_current_site' => env('PATH_CURRENT_SITE'), 'site_id_current_site' => env('SITE_ID_CURRENT_SITE'), 'blog_id_current_site' => env('BLOG_ID_CURRENT_SITE'), + 'caching' => env('DB_CACHE'), ]; diff --git a/src/Models/Comment.php b/src/Models/Comment.php index dde3450..9aa0037 100644 --- a/src/Models/Comment.php +++ b/src/Models/Comment.php @@ -5,6 +5,7 @@ use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; use Koselig\Support\Action; use Koselig\Support\Wordpress; +use Watson\Rememberable\Rememberable; /** * Table containing all the comments belonging to posts. @@ -33,12 +34,21 @@ use Koselig\Support\Wordpress; */ class Comment extends Model { + use Rememberable; + public $timestamps = false; protected $table = DB_PREFIX . 'comments'; protected $primaryKey = 'comment_ID'; protected $dates = ['comment_date', 'comment_date_gmt']; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** * Create a new Eloquent model instance. * * @param array $attributes @@ -53,6 +63,13 @@ class Comment extends Model if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) { $this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_comments'); } + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } } /** diff --git a/src/Models/Meta.php b/src/Models/Meta.php index 36dce4e..4c7644b 100644 --- a/src/Models/Meta.php +++ b/src/Models/Meta.php @@ -6,6 +6,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Koselig\Exceptions\UnsatisfiedDependencyException; use Koselig\Support\Action; use Koselig\Support\Wordpress; +use Watson\Rememberable\Rememberable; /** * Table containing all metadata about a post. @@ -14,9 +15,18 @@ use Koselig\Support\Wordpress; */ class Meta extends Model { + use Rememberable; + public $timestamps = false; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** * Cache for all meta values. * * @var array @@ -40,6 +50,13 @@ class Meta extends Model if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) { $this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_postmeta'); } + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } } /** diff --git a/src/Models/Option.php b/src/Models/Option.php index d891dd3..510774d 100644 --- a/src/Models/Option.php +++ b/src/Models/Option.php @@ -3,6 +3,7 @@ namespace Koselig\Models; use Illuminate\Database\Eloquent\Model; use Koselig\Support\Wordpress; +use Watson\Rememberable\Rememberable; /** * Table containing all Wordpress options. @@ -11,11 +12,20 @@ use Koselig\Support\Wordpress; */ class Option extends Model { + use Rememberable; + public $timestamps = false; protected $primaryKey = 'option_id'; protected $table = DB_PREFIX . 'options'; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** * Create a new Eloquent model instance. * * @param array $attributes @@ -30,6 +40,13 @@ class Option extends Model if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) { $this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_options'); } + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } } /** diff --git a/src/Models/Post.php b/src/Models/Post.php index 7a2bbf1..a4039bc 100644 --- a/src/Models/Post.php +++ b/src/Models/Post.php @@ -26,6 +26,13 @@ class Post extends Model protected $prefix = DB_PREFIX; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** * Create a new Eloquent model instance. * * @param array $attributes @@ -41,6 +48,13 @@ class Post extends Model $this->prefix = DB_PREFIX . Wordpress::getSiteId() . '_'; $this->setTable($this->prefix . 'posts'); } + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } } /** diff --git a/src/Models/Term.php b/src/Models/Term.php index b9e9e44..534a948 100644 --- a/src/Models/Term.php +++ b/src/Models/Term.php @@ -4,6 +4,7 @@ namespace Koselig\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Koselig\Support\Wordpress; +use Watson\Rememberable\Rememberable; /** * Table containing all terms used by Wordpress. @@ -12,11 +13,20 @@ use Koselig\Support\Wordpress; */ class Term extends Model { + use Rememberable; + public $timestamps = false; protected $primaryKey = 'term_id'; protected $table = DB_PREFIX . 'terms'; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** * Create a new Eloquent model instance. * * @param array $attributes @@ -31,6 +41,13 @@ class Term extends Model if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) { $this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_terms'); } + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } } /** diff --git a/src/Models/TermTaxonomy.php b/src/Models/TermTaxonomy.php index a1542b9..81237bc 100644 --- a/src/Models/TermTaxonomy.php +++ b/src/Models/TermTaxonomy.php @@ -3,6 +3,7 @@ namespace Koselig\Models; use Illuminate\Database\Eloquent\Model; use Koselig\Support\Wordpress; +use Watson\Rememberable\Rememberable; /** * Taxonomy for the terms in the CMS. @@ -11,11 +12,20 @@ use Koselig\Support\Wordpress; */ class TermTaxonomy extends Model { + use Rememberable; + public $timestamps = false; protected $primaryKey = 'term_taxonomy_id'; protected $table = DB_PREFIX . 'term_taxonomy'; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** * Create a new Eloquent model instance. * * @param array $attributes @@ -30,5 +40,12 @@ class TermTaxonomy extends Model if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) { $this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_term_taxonomy'); } + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } } } diff --git a/src/Models/User.php b/src/Models/User.php index 2c22a78..dd4e786 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -6,6 +6,7 @@ use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; use Koselig\Support\Wordpress; +use Watson\Rememberable\Rememberable; /** * Table containing all the users within the CMS. @@ -14,7 +15,8 @@ use Koselig\Support\Wordpress; */ class User extends Model implements AuthenticatableContract { - use Authenticatable; + use Authenticatable, Rememberable; + public $timestamps = false; protected $table = DB_PREFIX . 'users'; @@ -22,6 +24,13 @@ class User extends Model implements AuthenticatableContract protected $dates = ['user_registered']; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** * Create a new Eloquent model instance. * * @param array $attributes @@ -36,6 +45,13 @@ class User extends Model implements AuthenticatableContract if (Wordpress::multisite() && (Wordpress::getSiteId() !== 0 && Wordpress::getSiteId() !== 1)) { $this->setTable(DB_PREFIX . Wordpress::getSiteId() . '_users'); } + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } } /** diff --git a/src/Models/UserMeta.php b/src/Models/UserMeta.php index 7b1c9f8..0219cf4 100644 --- a/src/Models/UserMeta.php +++ b/src/Models/UserMeta.php @@ -3,6 +3,7 @@ namespace Koselig\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Watson\Rememberable\Rememberable; /** * Table containing the metadata about users in the CMS. @@ -11,6 +12,8 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; */ class UserMeta extends Model { + use Rememberable; + public $timestamps = false; protected $table = DB_PREFIX . 'usermeta'; protected $primaryKey = 'umeta_id'; @@ -23,6 +26,31 @@ class UserMeta extends Model private static $cache = []; /** + * Length of time to cache this model for. + * + * @var integer + */ + protected $rememberFor; + + /** + * Create a new Eloquent model instance. + * + * @param array $attributes + * @return void + */ + public function __construct(array $attributes = []) + { + parent::__construct($attributes); + + // enable caching if the user has opted for it in their configuration + if (config('wordpress.caching')) { + $this->rememberFor = config('wordpress.caching'); + } else { + unset($this->rememberFor); + } + } + + /** * Get metadata for a user. * * @param int|string|null $user user to get meta for (or name of the meta item to get -- libgit2 1.7.2