|
1
|
|
|
<?php namespace Arcanesoft\Blog\ViewComposers; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanesoft\Blog\Models\Category; |
|
4
|
|
|
use Arcanesoft\Blog\Models\Post; |
|
5
|
|
|
use Arcanesoft\Blog\Models\Tag; |
|
6
|
|
|
use Closure; |
|
7
|
|
|
use Illuminate\Support\Facades\Cache; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractComposer |
|
11
|
|
|
* |
|
12
|
|
|
* @package Arcanesoft\Blog\ViewComposers |
|
13
|
|
|
* @author ARCANEDEV <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractComposer |
|
16
|
|
|
{ |
|
17
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
18
|
|
|
| Properties |
|
19
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
20
|
|
|
*/ |
|
21
|
|
|
/** |
|
22
|
|
|
* Caching time. |
|
23
|
|
|
* |
|
24
|
|
|
* @var int |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cacheMinutes = 5; |
|
27
|
|
|
|
|
28
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
29
|
|
|
| Main Functions |
|
30
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
31
|
|
|
*/ |
|
32
|
|
|
/** |
|
33
|
|
|
* Get the cached posts. |
|
34
|
|
|
* |
|
35
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
36
|
|
|
*/ |
|
37
|
|
|
protected function cachedPosts() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->cacheResults('posts.all', function () { |
|
40
|
|
|
return Post::all(); |
|
41
|
|
|
}); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Get the cached categories. |
|
46
|
|
|
* |
|
47
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function cachedCategories() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->cacheResults('categories.all', function () { |
|
52
|
|
|
return Category::all(); |
|
53
|
|
|
}); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the cached tags. |
|
58
|
|
|
* |
|
59
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function cachedTags() |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->cacheResults('tags.all', function () { |
|
64
|
|
|
return Tag::all(); |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
69
|
|
|
| Other Functions |
|
70
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
71
|
|
|
*/ |
|
72
|
|
|
/** |
|
73
|
|
|
* Cache the results. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $name |
|
76
|
|
|
* @param \Closure $callback |
|
77
|
|
|
* |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function cacheResults($name, Closure $callback) |
|
81
|
|
|
{ |
|
82
|
|
|
return Cache::remember("blog::{$name}", $this->cacheMinutes, $callback); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|