Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Improve caching, fix some minor issues and fix #148
Browse files Browse the repository at this point in the history
  • Loading branch information
JN-Jones committed May 22, 2015
1 parent 3d0da84 commit 353c13d
Show file tree
Hide file tree
Showing 24 changed files with 404 additions and 297 deletions.
57 changes: 57 additions & 0 deletions app/Database/Collections/TreeCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Tree collection class.
*
* @author MyBB Group
* @version 2.0.0
* @package mybb/core
* @license http://www.mybb.com/licenses/bsd3 BSD-3
*/

namespace MyBB\Core\Database\Collections;

use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class TreeCollection extends Collection
{
/**
* Fill `parent` and `children` relationships for every node in collection.
*
* This will overwrite any previously set relations.
*
* @return $this
*/
public function linkNodes()
{
if ($this->isEmpty()) {
return $this;
}
$groupedChildren = $this->groupBy('parent_id');
/** @var Model $node */
foreach ($this->items as $node) {
if (!isset($node->parent)) {
$node->setRelation('parent', null);
}
$children = $groupedChildren->get($node->getKey(), []);
/** @var Model $child */
foreach ($children as $child) {
$child->setRelation('parent', $node);
}
$node->setRelation('children', Collection::make($children));
}

return $this;
}

/**
* Build tree from node list. Each item will have set children relation.
*
* @return Collection
*/
public function toTree()
{
$this->linkNodes();
return $this;
}
}
10 changes: 5 additions & 5 deletions app/Database/Models/AbstractCachingModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function save(array $options = array())
$saved = parent::save($options);

if ($saved) {
static::$models[$this->getKey()] = $this;
static::$models[get_class($this)][$this->getKey()] = $this;
}

return $saved;
Expand All @@ -37,7 +37,7 @@ public function save(array $options = array())
public function delete()
{
parent::delete();
unset(static::$models[$this->getKey()]);
unset(static::$models[get_class($this)][$this->getKey()]);
}

/**
Expand All @@ -49,10 +49,10 @@ public static function find($id, $columns = array('*'))
return parent::find($id, $columns);
}

if (!isset(static::$models[$id])) {
static::$models[$id] = parent::find($id);
if (!isset(static::$models[get_called_class()][$id])) {
static::$models[get_called_class()][$id] = parent::find($id);
}

return static::$models[$id];
return static::$models[get_called_class()][$id];
}
}
2 changes: 1 addition & 1 deletion app/Database/Models/Conversation.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @property ConversationMessage lastMessage
* @property Collection participants
*/
class Conversation extends Model implements HasPresenter
class Conversation extends AbstractCachingModel implements HasPresenter
{
/**
* The database table used by the model.
Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/ConversationMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @property Conversation conversation
* @property User author
*/
class ConversationMessage extends Model implements HasPresenter
class ConversationMessage extends AbstractCachingModel implements HasPresenter
{
/**
* The database table used by the model.
Expand Down
50 changes: 41 additions & 9 deletions app/Database/Models/Forum.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,13 @@

use MyBB\Core\Permissions\Interfaces\InheritPermissionInterface;
use MyBB\Core\Permissions\Traits\InheritPermissionableTrait;
use Kalnoy\Nestedset\Node;
use McCool\LaravelAutoPresenter\HasPresenter;
use MyBB\Core\Database\Collections\TreeCollection;

class Forum extends Node implements HasPresenter, InheritPermissionInterface
class Forum extends AbstractCachingModel implements HasPresenter, InheritPermissionInterface
{
use InheritPermissionableTrait;

/**
* Nested set column IDs.
*/
const LFT = 'left_id';
const RGT = 'right_id';
const PARENT_ID = 'parent_id';

// @codingStandardsIgnoreStart
/**
* Indicates if the model should be timestamped.
Expand Down Expand Up @@ -65,6 +58,18 @@ public function getPresenterClass()
return 'MyBB\Core\Presenters\Forum';
}

/**
* @return InheritPermissionableTrait
*/
public function getParent()
{
if ($this->parent_id === null) {
return null;
}

return $this->find($this->parent_id);
}

/**
* A forum contains many threads.
*
Expand Down Expand Up @@ -104,4 +109,31 @@ public function lastPostAuthor()
{
return $this->hasOne('MyBB\\Core\\Database\\Models\\User', 'id', 'last_post_user_id');
}

/**
* Relation to the parent.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function parent()
{
return $this->belongsTo(get_class($this), 'parent_id');
}
/**
* Relation to children.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function children()
{
return $this->hasMany(get_class($this), 'parent_id');
}

/**
* {@inheritdoc}
*/
public function newCollection(array $models = array())
{
return new TreeCollection($models);
}
}
2 changes: 1 addition & 1 deletion app/Database/Models/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Illuminate\Database\Eloquent\Model;

class Permission extends Model
class Permission extends AbstractCachingModel
{

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Illuminate\Database\Eloquent\Model;
use McCool\LaravelAutoPresenter\HasPresenter;

class Poll extends Model implements HasPresenter
class Poll extends AbstractCachingModel implements HasPresenter
{
// @codingStandardsIgnoreStart

Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/PollVote.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Illuminate\Database\Eloquent\Model;

class PollVote extends Model
class PollVote extends AbstractCachingModel
{
// @codingStandardsIgnoreStart

Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use McCool\LaravelAutoPresenter\HasPresenter;
use MyBB\Core\Likes\Traits\LikeableTrait;

class Post extends Model implements HasPresenter
class Post extends AbstractCachingModel implements HasPresenter
{
use SoftDeletes;
use LikeableTrait;
Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/ProfileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @property string validation_rules
* @property string name
*/
class ProfileField extends Model implements HasPresenter
class ProfileField extends AbstractCachingModel implements HasPresenter
{
/**
* @var string
Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/ProfileFieldGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* @property int id
*/
class ProfileFieldGroup extends Model implements HasPresenter
class ProfileFieldGroup extends AbstractCachingModel implements HasPresenter
{
const ABOUT_YOU = 'about-you';
const CONTACT_DETAILS = 'contact-details';
Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/ProfileFieldOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @property string name
* @property string value
*/
class ProfileFieldOption extends Model
class ProfileFieldOption extends AbstractCachingModel
{
/**
* @var string
Expand Down
59 changes: 56 additions & 3 deletions app/Database/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

namespace MyBB\Core\Database\Models;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
class Role extends AbstractCachingModel
{
/**
* @var array
*/
protected static $slugCache;

/**
* The database table used by the model.
Expand All @@ -35,4 +37,55 @@ public function permissions()
{
return $this->belongsToMany('MyBB\Core\Database\Models\Permission');
}

/**
* @param string $slug
*
* @return Role
*/
public static function whereSlug($slug)
{
if (!isset(static::$slugCache[$slug])) {
static::$slugCache[$slug] = static::where('role_slug', '=', $slug)->first();
}
//dd(static::$slugCache);
return static::$slugCache[$slug];
}

/**
* {@inheritdoc}
*/
public function save(array $options = array())
{
$saved = parent::save($options);

if ($saved) {
static::$slugCache[$this->role_slug] = $this;
}

return $saved;
}

/**
* {@inheritdoc}
*/
public function delete()
{
parent::delete();
unset(static::$slugCache[$this->role_slug]);
}

/**
* {@inheritdoc}
*/
public static function find($id, $columns = array('*'))
{
$model = parent::find($id, $columns);

if ($columns == array('*')) {
static::$slugCache[$model->role_slug] = $model;
}

return $model;
}
}
2 changes: 1 addition & 1 deletion app/Database/Models/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Illuminate\Database\Eloquent\Model;

class Search extends Model
class Search extends AbstractCachingModel
{
// @codingStandardsIgnoreStart

Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;

class Topic extends Model implements HasPresenter
class Topic extends AbstractCachingModel implements HasPresenter
{
use SoftDeletes;

Expand Down
4 changes: 2 additions & 2 deletions app/Database/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
/**
* @property string id
*/
class User extends Model implements AuthenticatableContract, CanResetPasswordContract, HasPresenter, PermissionInterface
class User extends AbstractCachingModel implements AuthenticatableContract, CanResetPasswordContract, HasPresenter, PermissionInterface
{
use Authenticatable;
use CanResetPassword;
Expand Down Expand Up @@ -109,7 +109,7 @@ public function displayRole()
if ($this->displayRole == null) {
// Do we have a guest?
if ($this->id <= 0) {
$this->displayRole = Role::where('role_slug', 'guest')->first();
$this->displayRole = Role::whereSlug('guest');
} else {
$this->displayRole = $this->roles->whereLoose('pivot.is_display', true)->first();
}
Expand Down
2 changes: 1 addition & 1 deletion app/Database/Models/UserProfileField.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* @property string value
*/
class UserProfileField extends Model
class UserProfileField extends AbstractCachingModel
{
/**
* @var string
Expand Down
6 changes: 3 additions & 3 deletions app/Permissions/PermissionChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,13 +163,13 @@ public function hasPermission($content, $contentID, $permission, User $user = nu
if ($roles->count() == 0) {
if ($user->exists) {
// User saved? Something is wrong, attach the registered role
$registeredRole = Role::where('role_slug', '=', 'user')->first();
$registeredRole = Role::whereSlug('user');
$user->roles()->attach($registeredRole->id, ['is_display' => 1]);
$roles = [$registeredRole];
} else {
// Guest
if ($this->guestRole == null) {
$this->guestRole = Role::where('role_slug', '=', 'guest')->first();
$this->guestRole = Role::whereSlug('guest');
}
$roles = [$this->guestRole];
}
Expand Down Expand Up @@ -285,7 +285,7 @@ public function getPermissionForRole(Role $role, $permission, $content = null, $
*/
private function hasCache(Role $role, $permission, $content, $contentID)
{
return $this->getCache($role, $permission, $content, $contentID) != null;
return $this->getCache($role, $permission, $content, $contentID) !== null;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions app/Permissions/Traits/InheritPermissionableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ trait InheritPermissionableTrait
*/
public function getParent()
{
if ($this->parent_id === null) {
return null;
}

return $this->parent;
}

Expand Down
Loading

0 comments on commit 353c13d

Please sign in to comment.