Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesPong committed Dec 10, 2017
2 parents 5f4ff67 + 9d7f258 commit cc8f455
Show file tree
Hide file tree
Showing 104 changed files with 3,761 additions and 486 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,10 @@ PUSHER_APP_SECRET=

YOUDAO_APP_KEY=
YOUDAO_APP_SECRET=

GOOGLE_ANALYTICS_ID=
ENABLE_DATA_CACHE=
ENABLE_VISITOR_LOG=

COMMENT_DRIVER=
DISQUS_SHORT_NAME=
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,24 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Fixed

## 0.3.0 - 2017-12-10
### Added
- Visitor history and base counter. See branch `counter` and `visitor`
- Cache support. See branch `cacheable`
- Auto slug in ajax and Chinese support
- Base implement of disqus. See branch `comment`
- Hot posts widget
- Base feature image support
- Model `Setting` and base SEO support

### Changed
- Various CSS styles changes
- BaseRepository
- Side navigation bar style

### Fixed
- Fix Postable retrieve data bug (`890c082`)

## 0.2.0 - 2017-06-15
### Added
- Backend
Expand Down
11 changes: 6 additions & 5 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

- [x] Category
- [x] Tag
- [ ] Comment
- [ ] Visitor & view count(use Middleware)
- [ ] Cache
- [x] Comment
- [x] Visitor & view count(use Middleware)
- [x] Cache
- [ ] Email
- [ ] Notification
- [ ] Socialite
Expand All @@ -15,9 +15,9 @@
- [ ] I18N
- [ ] Highlight tab
- [ ] Links
- [ ] Settings
- [x] Settings
- [ ] File management
- [ ] Feature image
- [x] Feature image
- [ ] Flash session

## Frontend
Expand All @@ -33,3 +33,4 @@
- [ ] Package
- [ ] Unit test
- [ ] Pjax
- [ ] Upgrade to Laravel 5.5
126 changes: 126 additions & 0 deletions app/Console/Commands/SavePostViewCount.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

namespace App\Console\Commands;

use App\Models\Post;
use App\Repositories\Contracts\PostRepository;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class SavePostViewCount extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'view_count:save';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Save posts view count into DB and flush cache';

/**
* @var mixed
*/
protected $cacheKeyPrefix;

/**
* @var PostRepository
*/
protected $postRepo;

/**
* Create a new command instance.
*
* @param PostRepository $postRepo
* @return void
*/
public function __construct(PostRepository $postRepo)
{
parent::__construct();

$this->postRepo = $postRepo;

$this->cacheKeyPrefix = config('blog.counter.cache_key');
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$data = $this->savePostViewCount();

$this->info("Post view_count save successfully at " . Carbon::now()->toDateTimeString());
$this->table(['Post ID', 'Increase Count'], $data);
}

/**
* Retrieve all post view_count from cache and save into DB
*
* @return array
*/
protected function savePostViewCount()
{
$results = [];

// Retrieve all post id
$this->postRepo
->getModel()
->select('id')
->chunk(100, function ($posts) use (&$results) {
foreach ($posts as $post) {
if ($count = $this->getCacheCount($post->id)) {
$post->increment('view_count', $count);

array_push($results, [$post->id, $count]);

$this->flushCache($this->cacheKey($post->id));
}
}
});

return $results;
}

/**
* Get post view_count from cache
*
* @param $id
* @return mixed
*/
protected function getCacheCount($id)
{
return Cache::get($this->cacheKey($id));
}

/**
* Get cache key
*
* @param $id
* @return string
*/
protected function cacheKey($id)
{
return $this->cacheKeyPrefix . $id;
}

/**
* Flush post view_count in cache by key
*
* @param $key
* @return bool
*/
protected function flushCache($key)
{
return Cache::forget($key);
}
}
6 changes: 4 additions & 2 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Console;

use App\Console\Commands\SavePostViewCount;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -13,19 +14,20 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
//
SavePostViewCount::class
];

/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
// $schedule->command('inspire')
// ->hourly();
$schedule->command('view_count:save')->hourly()->appendOutputTo(storage_path() . '/logs/cron.log');
}

/**
Expand Down
24 changes: 24 additions & 0 deletions app/Contracts/ContentableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Contracts;

/**
* Interface ContentableInterface
* @package App\Contracts
*/
interface ContentableInterface
{
/**
* Get raw content in markdown syntax.
*
* @return string
*/
public function getRawContent();

/**
* Get primary key in 'contents' table.
*
* @return int
*/
public function getContentId();
}
44 changes: 44 additions & 0 deletions app/Events/PostViewEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

/**
* Class PostViewEvent
* @package App\Events
*/
class PostViewEvent
{
use Dispatchable, InteractsWithSockets, SerializesModels;

/**
* @var
*/
public $postId;

/**
* PostViewEvent constructor.
* @param $postId
*/
public function __construct($postId)
{
$this->postId = $postId;
}

/**
* Get the channels the event should broadcast on.
*
* @return Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
16 changes: 16 additions & 0 deletions app/Http/Controllers/Backend/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,24 @@

class DashboardController extends Controller
{
/**
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function index()
{
return view('admin.home');
}

/**
* @param Request $request
* @return \Illuminate\Foundation\Application|\JellyBool\Translug\Translation|mixed|string|\translug
*/
public function autoSlug(Request $request)
{
$this->validate($request, [
'text' => 'required',
]);

return str_slug_with_cn($request->input('text'));
}
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Backend/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public function create()
*/
public function store(StoreUpdatePostRequest $request)
{
$this->postRepo->createPost($request->all());
$this->postRepo->createPost($request->except('_token'));

return redirect()->route('admin.posts.index')->withSuccess('Create post successfully!');
}
Expand Down
Loading

0 comments on commit cc8f455

Please sign in to comment.