From 5257f66d86b8412c1b25ba6e3b1ce7be35878174 Mon Sep 17 00:00:00 2001 From: djl997 <27086857+djl997@users.noreply.github.com> Date: Fri, 9 Jun 2023 15:56:26 +0200 Subject: [PATCH] Added: made dateOrDiff value configurable + config publishable --- README.md | 17 +++++++++++------ config/blade-shortcuts.php | 22 ++++++++++++++++++++++ src/BladeShortcutsBladeDirectives.php | 2 +- src/BladeShortcutsServiceProvider.php | 8 +++++++- 4 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 config/blade-shortcuts.php diff --git a/README.md b/README.md index 92e8160..4b7a24a 100644 --- a/README.md +++ b/README.md @@ -4,25 +4,24 @@ [![Total Downloads](https://img.shields.io/packagist/dt/djl997/blade-shortcuts.svg?style=flat-square)](https://packagist.org/packages/djl997/blade-shortcuts) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE) -Blade Shortcuts is a library of handy Blade Directives as listed below. The goal is to have less repetitive (base) logic in views, shorter code and/or better readability. +Blade Shortcuts is a library of clever Blade Directives as listed below. The goal is to have less repetitive (base) logic in your Blade Views, overall shorter code and better readability. ## Requirements Blade Shortcuts requires PHP 8+ and Laravel 6+. ## Installation You can install the package via composer: -```bash +```sh composer require djl997/blade-shortcuts ``` ## Usage -After installation, all directives should be usable immediately. If you experience problems on first use or after updating the package, try running `php artisan view:clear`. +After installation, all directives should be usable immediately. If something goes wrong at first use or after an update, `php artisan view:clear` should clear the issue. -## All directives +## Contents - [App Name](#app-name) - [Boolean](#boolean) - [Config](#config) - - [Dates](#dates) - date - datetime @@ -73,8 +72,9 @@ Other options: @date(now()->subWeek()) @date(now()->subWeek(), 'dateOrDiff') ``` +> If the time difference is more than 23 hours, ‘dateOrDiff’ will automatically show the date in a localized format instead of ‘x time ago’ or ‘in x time’. You can adjust this threshold in the config file: `php artisan vendor:publish --tag=blade-shortcuts-config`. -Or try shortcuts for datetime, time, year, month or day (also in the correct localized format): +Try shortcuts for datetime, time, year, month or day (also in the correct localized format): ```blade @datetime(now()) @time(now()) @@ -146,6 +146,11 @@ laravel framework ``` Find all available methods in [Laravel Docs](https://laravel.com/docs/10.x/helpers#fluent-strings-method-list). +## Publish config +```sh +php artisan vendor:publish --tag=blade-shortcuts-config +``` + ## Changelog Please see [GitHubs releases section](https://github.com/djl997/blade-shortcuts/releases) for more information on what has changed recently. diff --git a/config/blade-shortcuts.php b/config/blade-shortcuts.php new file mode 100644 index 0000000..3a78eb4 --- /dev/null +++ b/config/blade-shortcuts.php @@ -0,0 +1,22 @@ +diffInHours() > YOUR-VALUE + | + */ + + 'dateOrDiff' => 23 + +]; \ No newline at end of file diff --git a/src/BladeShortcutsBladeDirectives.php b/src/BladeShortcutsBladeDirectives.php index 7340aad..6c457d7 100644 --- a/src/BladeShortcutsBladeDirectives.php +++ b/src/BladeShortcutsBladeDirectives.php @@ -78,7 +78,7 @@ public function date(string $expression): string if(count($arr) === 2) { switch ($arr[1]) { case "'dateOrDiff'": - return "diffInHours() > 23) { echo Carbon\Carbon::parse($arr[0])->translatedFormat(__('blade_directives::format.date')); } else { echo Carbon\Carbon::parse($arr[0])->diffForHumans(['options' => Carbon\Carbon::ONE_DAY_WORDS]); } } else { echo ''; } ?>"; + return "diffInHours() > config('blade-shortcuts.dateOrDiff')) { echo Carbon\Carbon::parse($arr[0])->translatedFormat(__('blade_directives::format.date')); } else { echo Carbon\Carbon::parse($arr[0])->diffForHumans(['options' => Carbon\Carbon::ONE_DAY_WORDS]); } } else { echo ''; } ?>"; break; default: diff --git a/src/BladeShortcutsServiceProvider.php b/src/BladeShortcutsServiceProvider.php index ac919b5..385fcb1 100644 --- a/src/BladeShortcutsServiceProvider.php +++ b/src/BladeShortcutsServiceProvider.php @@ -16,6 +16,10 @@ class BladeShortcutsServiceProvider extends ServiceProvider public function boot() { $this->loadTranslationsFrom(__DIR__.'/../lang', 'blade_directives'); + + $this->publishes([ + __DIR__.'/../config/blade-shortcuts.php' => config_path('blade-shortcuts.php'), + ], 'blade-shortcuts-config'); /** * Config @@ -117,6 +121,8 @@ public function boot() */ public function register() { - // .. + $this->mergeConfigFrom( + __DIR__.'/../config/blade-shortcuts.php', 'blade-shortcuts' + ); } } \ No newline at end of file