From 2a6672934dfeebf779df78ec799faf1bd1a8a6f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Mon, 14 Mar 2016 00:47:06 +0100 Subject: [PATCH] FEATURE: add automacally loaded settings into all views --- app/Models/Settings.php | 31 +++++++++++++ app/Providers/GlobalViewDataProvider.php | 59 +++++++++++++++--------- 2 files changed, 68 insertions(+), 22 deletions(-) diff --git a/app/Models/Settings.php b/app/Models/Settings.php index 0957e175..0b346d9e 100644 --- a/app/Models/Settings.php +++ b/app/Models/Settings.php @@ -10,11 +10,42 @@ class Settings extends Model public $timestamps = false; protected $fillable = ['key', 'value']; + /** + * Load all settings + * + * @return \stdClass + */ public function getAll() { $settings = Settings::select(['key', 'value']) ->get(); + return $this->returnPairs($settings); + } + + /** + * Load only config for views + * + * @return \stdClass + */ + public function getForView() + { + $settings = Settings::select(['key', 'value']) + ->where('load_into_views', 1) + ->get(); + + return $this->returnPairs($settings); + } + + /** + * From pair create object key => value + * + * @param $settings + * + * @return \stdClass + */ + private function returnPairs($settings) + { $data = new \stdClass(); foreach ($settings as $conf) { $data->{$conf->key} = $conf->value; diff --git a/app/Providers/GlobalViewDataProvider.php b/app/Providers/GlobalViewDataProvider.php index 290de0ba..1aa8c630 100644 --- a/app/Providers/GlobalViewDataProvider.php +++ b/app/Providers/GlobalViewDataProvider.php @@ -2,6 +2,7 @@ namespace App\Providers; +use App\Models\Settings; use Illuminate\Support\Facades\Blade; use Illuminate\Support\ServiceProvider; @@ -14,8 +15,44 @@ class GlobalViewDataProvider extends ServiceProvider */ public function boot() { + $settings = new Settings(); + + foreach ($settings->getForView() as $key => $value) { + view()->share($key, $value); + } + view()->share('vendorName', 'ja s.r.o.'); + $this->extendBlade(); + } + + /** + * Creates pattern for matching Blade directives + * + * @param $function + * + * @return string + */ + private function createPattern($function) + { + return '/(?createPattern('extendVisitor'); @@ -48,26 +85,4 @@ class GlobalViewDataProvider extends ServiceProvider return preg_replace($pattern, $code, $view); }); } - - /** - * Creates pattern for matching Blade directives - * - * @param $function - * - * @return string - */ - private function createPattern($function) - { - return '/(?