2015-06-01 11:41:18 -05:00
|
|
|
<?php
|
2016-05-20 05:41:23 -05:00
|
|
|
/**
|
|
|
|
* CacheProperties.php
|
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-05-20 05:41:23 -05:00
|
|
|
*/
|
|
|
|
|
2017-04-09 00:44:22 -05:00
|
|
|
declare(strict_types=1);
|
2015-06-01 11:41:18 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
use Cache;
|
2015-06-01 11:41:18 -05:00
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
|
|
|
|
use Illuminate\Support\Collection;
|
2016-12-16 01:07:31 -06:00
|
|
|
use Log;
|
2015-06-02 10:58:30 -05:00
|
|
|
use Preferences as Prefs;
|
2015-06-01 11:41:18 -05:00
|
|
|
|
|
|
|
/**
|
2015-06-03 11:22:47 -05:00
|
|
|
* Class CacheProperties
|
2015-06-01 11:41:18 -05:00
|
|
|
*
|
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
2015-06-03 11:22:47 -05:00
|
|
|
class CacheProperties
|
2015-06-01 11:41:18 -05:00
|
|
|
{
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
/** @var string */
|
|
|
|
protected $md5 = '';
|
2015-06-01 11:41:18 -05:00
|
|
|
/** @var Collection */
|
|
|
|
protected $properties;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->properties = new Collection;
|
2016-09-16 05:07:45 -05:00
|
|
|
if (auth()->check()) {
|
2016-09-16 05:15:58 -05:00
|
|
|
$this->addProperty(auth()->user()->id);
|
2016-03-03 01:31:18 -06:00
|
|
|
$this->addProperty(Prefs::lastActivity());
|
|
|
|
}
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $property
|
|
|
|
*/
|
|
|
|
public function addProperty($property)
|
|
|
|
{
|
|
|
|
$this->properties->push($property);
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get()
|
|
|
|
{
|
|
|
|
return Cache::get($this->md5);
|
|
|
|
}
|
2015-06-01 11:41:18 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-02-06 08:01:26 -06:00
|
|
|
public function getMd5(): string
|
2015-06-03 14:15:52 -05:00
|
|
|
{
|
|
|
|
return $this->md5;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-02-06 08:01:26 -06:00
|
|
|
public function has(): bool
|
2015-06-03 14:15:52 -05:00
|
|
|
{
|
2015-06-06 16:09:12 -05:00
|
|
|
if (getenv('APP_ENV') == 'testing') {
|
2015-06-06 08:36:12 -05:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5();
|
|
|
|
|
|
|
|
return Cache::has($this->md5);
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
*/
|
|
|
|
public function store($data)
|
|
|
|
{
|
|
|
|
Cache::forever($this->md5, $data);
|
|
|
|
}
|
|
|
|
|
2015-06-03 14:15:52 -05:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function md5()
|
2015-06-01 11:41:18 -05:00
|
|
|
{
|
2016-12-15 06:47:28 -06:00
|
|
|
$this->md5 = '';
|
2015-06-01 11:41:18 -05:00
|
|
|
foreach ($this->properties as $property) {
|
|
|
|
|
|
|
|
if ($property instanceof Collection || $property instanceof EloquentCollection) {
|
2015-06-05 06:39:24 -05:00
|
|
|
$this->md5 .= json_encode($property->toArray());
|
2015-06-01 11:41:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($property instanceof Carbon) {
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5 .= $property->toRfc3339String();
|
2015-06-01 11:41:18 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_object($property)) {
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5 .= $property->__toString();
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
2016-12-15 06:47:28 -06:00
|
|
|
if (is_bool($property) && $property === false) {
|
|
|
|
$this->md5 .= 'false';
|
|
|
|
}
|
|
|
|
if (is_bool($property) && $property === true) {
|
|
|
|
$this->md5 .= 'true';
|
|
|
|
}
|
2015-06-05 06:39:24 -05:00
|
|
|
|
|
|
|
$this->md5 .= json_encode($property);
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
2016-12-16 01:07:31 -06:00
|
|
|
Log::debug(sprintf('Cache string is %s', $this->md5));
|
2015-06-03 14:15:52 -05:00
|
|
|
$this->md5 = md5($this->md5);
|
2016-12-16 01:07:31 -06:00
|
|
|
Log::debug(sprintf('Cache MD5 is %s', $this->md5));
|
2015-06-01 11:41:18 -05:00
|
|
|
}
|
2015-06-05 06:39:24 -05:00
|
|
|
}
|