Skip to content

Commit

Permalink
Add global cache site settings
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesPong committed Jul 13, 2017
1 parent f7d9a32 commit cb78ed5
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 15 deletions.
23 changes: 23 additions & 0 deletions app/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,27 @@ class Setting extends Model
* @var array
*/
protected $fillable = ['key', 'value', 'tag'];

/**
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $tag
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeTag($query, $tag = null)
{
if (is_null($tag)) {
return $query;
}

return $query->where('tag', $tag);
}

/**
* @param null $tag
* @return mixed
*/
public function formatData($tag = null)
{
return $this->tag($tag)->pluck('value', 'key')->toArray();
}
}
21 changes: 21 additions & 0 deletions app/Observers/SettingObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Observers;

use App\Models\Setting;
use Illuminate\Support\Facades\Cache;

/**
* Class TagObserver
* @package App\Observers
*/
class SettingObserver extends BaseObserver
{
/**
* @param Setting $setting
*/
public function saved(Setting $setting)
{
Cache::forget($this->cacheHelper->keySiteSettings());
}
}
12 changes: 12 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

use App\Models\Category;
use App\Models\Post;
use App\Models\Setting;
use App\Models\Tag;
use App\Observers\CategoryObserver;
use App\Observers\PostObserver;
use App\Observers\SettingObserver;
use App\Observers\TagObserver;
use App\Repositories\Contracts\SettingRepository;
use App\Services\CacheHelper;
use Illuminate\Contracts\Container\Container;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\ServiceProvider;

Expand All @@ -27,6 +32,13 @@ public function boot()
Post::observe(PostObserver::class);
Tag::observe(TagObserver::class);
Category::observe(CategoryObserver::class);
Setting::observe(SettingObserver::class);

$this->app->singleton('settings', function (Container $app) {
return $app['cache']->rememberForever(CacheHelper::keySiteSettings(), function () use ($app) {
return $app->make(SettingRepository::class)->siteSettings();
});
});
}

/**
Expand Down
6 changes: 0 additions & 6 deletions app/Providers/ComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ public function boot()
View::composer('widgets.tag', TagsComposer::class);
View::composer('widgets.hot', HotPostsComposer::class);
View::composer('partials.comment', CommentComposer::class);
View::composer('layouts.app', function (\Illuminate\View\View $view) {
$view->with([
'keywords' => '',
'description' => ''
]);
});
}

/**
Expand Down
6 changes: 5 additions & 1 deletion app/Repositories/Contracts/SettingRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
*/
interface SettingRepository extends RepositoryInterface
{

/**
* @param null $tag
* @return mixed
*/
public function siteSettings($tag = null);
}
13 changes: 13 additions & 0 deletions app/Repositories/Eloquent/SettingRepositoryEloquent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ public function model()
{
return Setting::class;
}

/**
* @param null $tag
* @return array|mixed
*/
public function siteSettings($tag = null)
{
if (method_exists($this->model, $method = 'formatData')) {
return call_user_func_array([$this->model, $method], [$tag]);
}

return [];
}
}
8 changes: 8 additions & 0 deletions app/Services/CacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,12 @@ public function keyPaginate($table)
{
return sprintf(self::KEY_FORMAT, $table, 'paginate-' . request()->input('page', 1));
}

/**
* @return string
*/
public static function keySiteSettings()
{
return 'site.settings';
}
}
10 changes: 10 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,13 @@ function isAdmin()
return auth()->check() && auth()->user()->isAdmin();
}
}

if (!function_exists('setting')) {
/**
* @param $key
* @return mixed
*/
function setting($key) {
return array_get(app('settings'), $key);
}
}
4 changes: 2 additions & 2 deletions resources/views/categories/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{{ $category->name }} | @parent
@endsection

@section('keywords'){!! $category->name !!}@endsection
@section('description'){!! $category->description !!}@endsection
@section('keywords'){{ $category->name }}@endsection
@section('description'){{ $category->description }}@endsection

@section('content')
@component('components.header')
Expand Down
4 changes: 2 additions & 2 deletions resources/views/layouts/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

<title>@section('title'){{ config('app.name', 'Laravel') }}@show</title>

<meta name="keywords" content="@section('keywords'){{ $keywords or '' }}@show">
<meta name="description" content="@section('description'){{ $description or '' }}@show">
<meta name="keywords" content="@section('keywords'){{ setting('keywords') }}@show">
<meta name="description" content="@section('description'){{ setting('description') }}@show">

{{--<!--Import Google Icon Font-->--}}
{{--<link href="http://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">--}}
Expand Down
4 changes: 2 additions & 2 deletions resources/views/posts/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{{ $post->title }} | @parent
@endsection

@section('keywords'){!! $post->tags->implode('name', ',') !!}@endsection
@section('description'){!! $post->description !!}@endsection
@section('keywords'){{ $post->tags->implode('name', ',') }}@endsection
@section('description'){{ $post->description }}@endsection

@section('content')
@component('components.header')
Expand Down
4 changes: 2 additions & 2 deletions resources/views/tags/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{{ $tag->name }} | @parent
@endsection

@section('keywords'){!! $tag->name !!}@endsection
@section('description'){!! $tag->description !!}@endsection
@section('keywords'){{ $tag->name }}@endsection
@section('description'){{ $tag->description }}@endsection

@section('content')
@component('components.header')
Expand Down

0 comments on commit cb78ed5

Please sign in to comment.