Toggle navigation
Toggle navigation
This project
Loading...
Sign in
Зуев Егор
/
wiki.dev
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Authored by
Dan Brown
2015-09-06 15:26:31 +0100
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Commit
b61c1d8df0171deb4198f81c7227bed728cb0c3f
b61c1d8d
1 parent
2acbe0f0
Added caching to settings
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
58 additions
and
10 deletions
app/Providers/CustomFacadeProvider.php
app/Services/SettingService.php
app/Providers/CustomFacadeProvider.php
View file @
b61c1d8
...
...
@@ -30,7 +30,10 @@ class CustomFacadeProvider extends ServiceProvider
});
$this
->
app
->
bind
(
'setting'
,
function
()
{
return
new
SettingService
(
$this
->
app
->
make
(
'Oxbow\Setting'
));
return
new
SettingService
(
$this
->
app
->
make
(
'Oxbow\Setting'
),
$this
->
app
->
make
(
'Illuminate\Contracts\Cache\Repository'
)
);
});
}
}
...
...
app/Services/SettingService.php
View file @
b61c1d8
<?php
namespace
Oxbow\Services
;
use
Oxbow\Setting
;
use
Illuminate\Contracts\Cache\Repository
as
Cache
;
/**
* Class SettingService
...
...
@@ -13,14 +14,19 @@ class SettingService
{
protected
$setting
;
protected
$cache
;
protected
$cachePrefix
=
'setting-'
;
/**
* SettingService constructor.
* @param $setting
* @param Setting $setting
* @param Cache $cache
*/
public
function
__construct
(
Setting
$setting
)
public
function
__construct
(
Setting
$setting
,
Cache
$cache
)
{
$this
->
setting
=
$setting
;
$this
->
cache
=
$cache
;
}
/**
...
...
@@ -32,17 +38,54 @@ class SettingService
*/
public
function
get
(
$key
,
$default
=
false
)
{
$setting
=
$this
->
getSettingObjectByKey
(
$key
);
$value
=
$setting
===
null
?
null
:
$setting
->
value
;
$value
=
$this
->
getValueFromStore
(
$key
,
$default
);
return
$this
->
formatValue
(
$value
,
$default
);
}
/**
* Gets a setting value from the cache or database.
* @param $key
* @param $default
* @return mixed
*/
protected
function
getValueFromStore
(
$key
,
$default
)
{
$cacheKey
=
$this
->
cachePrefix
.
$key
;
if
(
$this
->
cache
->
has
(
$cacheKey
))
{
return
$this
->
cache
->
get
(
$cacheKey
);
}
$settingObject
=
$this
->
getSettingObjectByKey
(
$key
);
if
(
$settingObject
!==
null
)
{
$value
=
$settingObject
->
value
;
$this
->
cache
->
forever
(
$cacheKey
,
$value
);
return
$value
;
}
return
$default
;
}
protected
function
clearFromCache
(
$key
)
{
$cacheKey
=
$this
->
cachePrefix
.
$key
;
$this
->
cache
->
forget
(
$cacheKey
);
}
/**
* Format a settings value
* @param $value
* @param $default
* @return mixed
*/
protected
function
formatValue
(
$value
,
$default
)
{
// Change string booleans to actual booleans
if
(
$value
===
'true'
)
$value
=
true
;
if
(
$value
===
'false'
)
$value
=
false
;
if
(
$value
===
'true'
)
$value
=
true
;
if
(
$value
===
'false'
)
$value
=
false
;
// Set to default if empty
if
(
$value
===
''
)
$value
=
$default
;
return
$value
===
null
?
$default
:
$value
;
if
(
$value
===
''
)
$value
=
$default
;
return
$value
;
}
/**
...
...
@@ -69,6 +112,7 @@ class SettingService
]);
$setting
->
value
=
$value
;
$setting
->
save
();
$this
->
clearFromCache
(
$key
);
return
true
;
}
...
...
@@ -83,6 +127,7 @@ class SettingService
if
(
$setting
)
{
$setting
->
delete
();
}
$this
->
clearFromCache
(
$key
);
return
true
;
}
...
...
Please
register
or
sign in
to post a comment