diff --git a/config/snippet-manager.php b/config/snippet-manager.php index f1580d4..c037c71 100644 --- a/config/snippet-manager.php +++ b/config/snippet-manager.php @@ -1,5 +1,6 @@ 'snippets', 'middleware' => 'auth', ], -); +]; diff --git a/database/migrations/2017_07_28_162237_create_snippets_table.php b/database/migrations/2017_07_28_162237_create_snippets_table.php index fba3ca1..3cad8e3 100644 --- a/database/migrations/2017_07_28_162237_create_snippets_table.php +++ b/database/migrations/2017_07_28_162237_create_snippets_table.php @@ -1,17 +1,16 @@ increments('id'); $table->string('locale'); $table->string('namespace'); @@ -20,10 +19,9 @@ public function up() $table->timestamps(); }); } + /** * Reverse the migrations. - * - * @return void */ public function down() { diff --git a/src/Controller.php b/src/Controller.php index af942a5..1289daa 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -2,19 +2,18 @@ namespace Moonshiner\SnippetManager; +use Artisan; +use Cache; use Illuminate\Http\Request; use Illuminate\Routing\Controller as BaseController; - use Illuminate\Support\Collection; use Moonshiner\SnippetManager\Models\Snippet; -use Artisan; -use App; -use Cache; class Controller extends BaseController { - /** @var \Moonshiner\SnippetManager\SnippetManager */ + /** @var \Moonshiner\SnippetManager\SnippetManager */ protected $manager; + public function __construct(SnippetManager $manager) { $this->manager = $manager; @@ -24,6 +23,7 @@ public function getView() { return view('snippet-manager::index'); } + public function index() { $allTranslations = Snippet::take(10)->get(); @@ -37,6 +37,7 @@ public function search() return $allTranslations; } + public function groups() { $namespaces = Snippet::select('namespace')->distinct()->get(); @@ -48,7 +49,7 @@ public function groups() public function update(Request $request, Snippet $snippet) { $snippet->value = $request->input('value', ''); - $path = [$snippet->locale,$snippet->namespace, $snippet->key]; + $path = [$snippet->locale, $snippet->namespace, $snippet->key]; $storeKey = implode('/', $path); Cache::flush($storeKey); Cache::put($storeKey, $snippet->value); @@ -56,6 +57,7 @@ public function update(Request $request, Snippet $snippet) $snippet->save(); } + protected function loadLocales() { //Set the default locale as the first one. @@ -67,6 +69,7 @@ protected function loadLocales() $locales = $locales->all(); } $locales = array_merge([config('app.locale')], $locales); + return array_unique($locales); } diff --git a/src/Models/Snippet.php b/src/Models/Snippet.php index fdf2f62..78c5fe7 100644 --- a/src/Models/Snippet.php +++ b/src/Models/Snippet.php @@ -6,9 +6,7 @@ class Snippet extends Model { - protected $table = 'ms_snippets'; - protected $guarded = array('id', 'created_at', 'updated_at'); - + protected $guarded = ['id', 'created_at', 'updated_at']; } diff --git a/src/SnippetManager.php b/src/SnippetManager.php index ba1661d..e4e5567 100644 --- a/src/SnippetManager.php +++ b/src/SnippetManager.php @@ -1,19 +1,17 @@ app = $app; @@ -29,7 +27,7 @@ public function setNamespace($namespace) public function get($key, $default = '', $namespace = null) { - if ($namespace !== null) { + if (null !== $namespace) { $this->namespace = $namespace; } @@ -37,10 +35,10 @@ public function get($key, $default = '', $namespace = null) $storeKey = implode('/', $path); $manager = $this; $namespace = $this->namespace; - xdebug_break(); $snippet = Cache::rememberForever($storeKey, function () use ($namespace, $key, $default, $manager) { return $manager->fetch($namespace, $key, $default); }); + return $snippet; } @@ -50,29 +48,32 @@ public function fetch($namespace, $key = null, $default = '') if ($key) { $query->where('key', $key); } - if ($namespace != '') { + if ('' != $namespace) { $query->where('namespace', $namespace); } $query->where('locale', $this->app['config']['app.locale']); if ($key) { $snippetValue = $query->pluck('value')->first(); - if (!$snippetValue) { + if (! $snippetValue) { return $this->missingSnippet($namespace, $key, $default); } + return $snippetValue; } $valuesByNamespace = [$namespace => $query->pluck('value', 'key')->toArray()]; return $valuesByNamespace; } + public function missingSnippet($namespace, $key, $value) { - Snippet::firstOrCreate(array( + Snippet::firstOrCreate([ 'locale' => $this->app['config']['app.locale'], 'namespace' => $namespace, 'key' => $key, - 'value' => $value - )); + 'value' => $value, + ]); + return $value; } } diff --git a/src/SnippetManagerServiceProvider.php b/src/SnippetManagerServiceProvider.php index f38ffb8..1526d36 100644 --- a/src/SnippetManagerServiceProvider.php +++ b/src/SnippetManagerServiceProvider.php @@ -2,30 +2,25 @@ namespace Moonshiner\SnippetManager; +use Blade; use Illuminate\Routing\Router; use Illuminate\Support\ServiceProvider; -use Blade; -use App; - class SnippetManagerServiceProvider extends ServiceProvider { protected $defer = false; /** * Register any package services. - * - * @return void */ public function register() { - $configPath = __DIR__ . '/../config/snippet-manager.php'; + $configPath = __DIR__.'/../config/snippet-manager.php'; $this->mergeConfigFrom($configPath, 'snippet-manager'); $this->publishes([$configPath => config_path('snippet-manager.php')], 'config'); - $this->app->singleton('snippet.manager', function ($app) { return new SnippetManager($app); }); @@ -65,10 +60,11 @@ public function boot(Router $router) $router->post('clearCache', 'Controller@clearCache'); }); } + public function provides() { return [ - 'snippet.manager' + 'snippet.manager', ]; } }