1212namespace CachetHQ \Cachet \Foundation \Providers ;
1313
1414use CachetHQ \Cachet \Models \Setting as SettingModel ;
15+ use CachetHQ \Cachet \Settings \Cache ;
1516use CachetHQ \Cachet \Settings \Repository ;
1617use Exception ;
1718use Illuminate \Support \ServiceProvider ;
@@ -32,29 +33,24 @@ class ConfigServiceProvider extends ServiceProvider
3233 */
3334 public function boot ()
3435 {
35- $ path = $ this ->app ->bootstrapPath ().'/cache/cachet. ' .$ this ->app ->environment ().'.php ' ;
36-
37- try {
38- $ cache = $ this ->app ->files ->getRequire ($ path );
39- } catch (Exception $ e ) {
40- $ cache = false ;
41- }
42-
43- $ this ->app ->terminating (function () use ($ cache , $ path ) {
44- if ($ this ->app ->setting ->stale () || $ cache === false ) {
45- $ this ->app ->files ->put ($ path , '<?php return ' .var_export ($ this ->app ->setting ->all (), true ).'; ' .PHP_EOL );
36+ $ env = $ this ->app ->environment ();
37+ $ repo = $ this ->app ->make (Repository::class);
38+ $ cache = $ this ->app ->make (Cache::class);
39+ $ loaded = $ cache ->load ($ env );
40+
41+ $ this ->app ->terminating (function () use ($ repo , $ cache ) {
42+ if ($ repo ->stale ()) {
43+ $ cache ->clear ();
4644 }
4745 });
4846
4947 try {
50- // Get the default settings.
51- $ defaultSettings = $ this ->app ->config ->get ('setting ' );
52-
53- // Get the configured settings.
54- $ appSettings = $ cache === false ? $ this ->app ->setting ->all () : $ cache ;
48+ if ($ loaded === false ) {
49+ $ loaded = $ repo ->all ();
50+ $ cache ->store ($ env , $ loaded );
51+ }
5552
56- // Merge the settings
57- $ settings = array_merge ($ defaultSettings , $ appSettings );
53+ $ settings = array_merge ($ this ->app ->config ->get ('setting ' ), $ loaded );
5854
5955 $ this ->app ->config ->set ('setting ' , $ settings );
6056 } catch (Exception $ e ) {
@@ -95,10 +91,12 @@ public function boot()
9591 */
9692 public function register ()
9793 {
98- $ this ->app ->singleton (' setting ' , function () {
99- return new Repository ( new SettingModel () );
94+ $ this ->app ->singleton (Cache::class , function ($ app ) {
95+ return new Cache ( $ app -> files , $ app -> bootstrapPath (). ' /cachet ' );
10096 });
10197
102- $ this ->app ->alias ('setting ' , Repository::class);
98+ $ this ->app ->singleton (Repository::class, function () {
99+ return new Repository (new SettingModel ());
100+ });
103101 }
104102}
0 commit comments