From cf05d96cf63911fea54630a1d23966bb559542c6 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Thu, 27 Apr 2023 14:26:23 +0100 Subject: [PATCH 01/58] add pest test --- composer.json | 62 +++++++++--- phpunit.xml | 34 ++++--- src/Digitlimit/Alert/AlertServiceProvider.php | 97 +++++++++++-------- tests/Feature/ExampleTest.php | 5 + tests/Pest.php | 45 +++++++++ tests/TestCase.php | 10 ++ tests/Unit/ExampleTest.php | 5 + 7 files changed, 184 insertions(+), 74 deletions(-) create mode 100644 tests/Feature/ExampleTest.php create mode 100644 tests/Pest.php create mode 100644 tests/TestCase.php create mode 100644 tests/Unit/ExampleTest.php diff --git a/composer.json b/composer.json index df6749c8..565a14a6 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,15 @@ { "name": "digitlimit/alert", - "description": "An easy way of flashing messages in Laravel Application ", + "description": "An easy way of flashing messages in Laravel Application", + "license": "MIT", + "authors": [ + { + "name": "Emeka Mbah", + "email": "frankemeks77@yahoo.com", + "homepage": "https://emekambah.medium.com" + } + ], + "homepage": "https://github.com/digitlimit/alert", "keywords": [ "laravel", "views", @@ -8,25 +17,48 @@ "messages", "withErrors" ], - "license": "MIT", - "authors": [ - { - "name": "Mbah Emeka", - "email": "frankemeks77@yahoo.com" - } - ], "require": { - "php": ">=5.4.0", - "illuminate/support": "~5.0|^6.0" + "ext-json": "*", + "php": "^7.4|^8.0", + "illuminate/contracts": "^7.20|^8.19|^9.0|^10.0", + "illuminate/support": "^7.20|^8.19|^9.0|^10.0" }, "require-dev": { - "mockery/mockery": "dev-master", - "phpunit/phpunit": "^9.0" + "guzzlehttp/guzzle": "^7.3", + "laravel/framework": "^7.20|^8.19|^9.0|^10.0", + "phpunit/phpunit": "^10.0", + "orchestra/testbench": "^8.5", + "nunomaduro/collision": "^7.5", + "pestphp/pest": "^2.5" }, "autoload": { - "psr-0": { - "Digitlimit\\Alert": "src/" + "psr-4": { + "Digitlimit\\Alert\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "Digitlimit\\Alert\\Tests\\": "tests" } }, - "minimum-stability": "stable" + "extra": { + "laravel": { + "providers": [ + "Digitlimit\\Alert\\AlertServiceProvider" + ], + "aliases": { + "Alert": "Digitlimit\\Alert\\Facades\\Alert" + } + } + }, + "scripts": { + "post-autoload-dump": [ + "@php vendor/bin/testbench package:discover --ansi" + ] + }, + "config": { + "allow-plugins": { + "pestphp/pest-plugin": true + } + } } diff --git a/phpunit.xml b/phpunit.xml index 3347b75b..c8a3a8b1 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,18 +1,20 @@ - - - - ./tests/ - - + + + + src/ + + + + + ./tests/Unit + + + ./tests/Feature + + + + + + diff --git a/src/Digitlimit/Alert/AlertServiceProvider.php b/src/Digitlimit/Alert/AlertServiceProvider.php index 6ca3d138..d32455bc 100644 --- a/src/Digitlimit/Alert/AlertServiceProvider.php +++ b/src/Digitlimit/Alert/AlertServiceProvider.php @@ -1,73 +1,84 @@ loadViewsFrom(__DIR__.'/../resources/views', 'digitlimit'); + // $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'digitlimit'); + + // Publishing is only necessary when using the CLI. + // if ($this->app->runningInConsole()) { + // $this->bootForConsole(); + // } + } /** - * Register the service provider. + * Register any package services. * * @return void */ - public function register() + public function register(): void { - $this->app->singleton('digitlimit.alert', function ($app) { - return new Alert($app['session.store']); - }); + // $this->app->singleton('alert', function ($app) { + // return new Alert($app['session.store']); + // }); } /** - * Bootstrap the application events. + * Get the services provided by the provider. * - * @return void + * @return array */ - public function boot() + public function provides() { - $this->loadViewsFrom(__DIR__.'/views', 'alert'); - $this->publishes([ - __DIR__.'/views' => base_path('resources/views/vendor/alert'), - ]); - - Blade::directive('alertHasSuccess', function () { - return ""; - }); + return ['alert']; + } - Blade::directive('endAlertHasSuccess', function () { - return ''; - }); + /** + * Console-specific booting. + * + * @return void + */ + protected function bootForConsole(): void + { + // $this->publishes([ + // __DIR__.'/../migrations' => database_path('migrations'), + // ], 'alert.migrations'); - Blade::directive('alertHasNoSuccess', function () { - return ""; - }); + // $this->publishes([ + // __DIR__.'/../seeders' => database_path('seeders'), + // ], 'alert.seeders'); - Blade::directive('endAlertHasNoSuccess', function () { - return ''; - }); + // Publishing the configuration file. + /*$this->publishes([ + __DIR__.'/../config/alert.php' => config_path('alert.php'), + ], 'alert.config');*/ - Blade::directive('alertHasError', function () { - return ""; - }); + // Publishing the views. + /*$this->publishes([ + __DIR__.'/../resources/views' => base_path('resources/views/vendor/digitlimit'), + ], 'alert.views');*/ - Blade::directive('endAlertHasError', function () { - return ''; - }); + // Publishing assets. + /*$this->publishes([ + __DIR__.'/../resources/assets' => public_path('vendor/digitlimit'), + ], 'alert.views');*/ - Blade::directive('alertHasNoError', function () { - return ""; - }); + // Publishing the translation files. + /*$this->publishes([ + __DIR__.'/../resources/lang' => resource_path('lang/vendor/digitlimit'), + ], 'alert.views');*/ - Blade::directive('endAlertHasNoError', function () { - return ''; - }); + // Registering package commands. + // $this->commands([]); } -} +} \ No newline at end of file diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php new file mode 100644 index 00000000..61cd84c3 --- /dev/null +++ b/tests/Feature/ExampleTest.php @@ -0,0 +1,5 @@ +toBeTrue(); +}); diff --git a/tests/Pest.php b/tests/Pest.php new file mode 100644 index 00000000..5949c617 --- /dev/null +++ b/tests/Pest.php @@ -0,0 +1,45 @@ +in('Feature'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +expect()->extend('toBeOne', function () { + return $this->toBe(1); +}); + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ + +function something() +{ + // .. +} diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 00000000..cfb05b6d --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,10 @@ +toBeTrue(); +}); From 3b6611bb7addac74abee7bde1ff9afe7c7e05202 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sat, 29 Apr 2023 12:22:52 +0100 Subject: [PATCH 02/58] bug: fix namespace issues --- TODO | 3 + phpunit.xml | 13 +- src/Alert.php | 15 + src/AlertServiceProvider.php | 47 +++ src/Digitlimit/Alert/Alert.php | 317 ------------------ src/Digitlimit/Alert/AlertServiceProvider.php | 84 ----- src/Digitlimit/Alert/views/field.blade.php | 9 - src/Digitlimit/Alert/views/form.blade.php | 14 - src/Digitlimit/Alert/views/modal.blade.php | 44 --- src/Digitlimit/Alert/views/notify.blade.php | 11 - src/Digitlimit/Alert/views/sticky.blade.php | 18 - src/{Digitlimit/Alert => }/Facades/Alert.php | 6 +- src/Message.php | 45 +++ src/Types/Field.php | 8 + src/Types/Modal.php | 8 + src/Types/Notify.php | 8 + src/Types/Sticky.php | 8 + tests/Unit/ExampleTest.php | 5 - tests/Unit/MessageTest.php | 10 + 19 files changed, 162 insertions(+), 511 deletions(-) create mode 100644 TODO create mode 100644 src/Alert.php create mode 100644 src/AlertServiceProvider.php delete mode 100644 src/Digitlimit/Alert/Alert.php delete mode 100644 src/Digitlimit/Alert/AlertServiceProvider.php delete mode 100644 src/Digitlimit/Alert/views/field.blade.php delete mode 100644 src/Digitlimit/Alert/views/form.blade.php delete mode 100644 src/Digitlimit/Alert/views/modal.blade.php delete mode 100644 src/Digitlimit/Alert/views/notify.blade.php delete mode 100644 src/Digitlimit/Alert/views/sticky.blade.php rename src/{Digitlimit/Alert => }/Facades/Alert.php (56%) create mode 100644 src/Message.php create mode 100644 src/Types/Field.php create mode 100644 src/Types/Modal.php create mode 100644 src/Types/Notify.php create mode 100644 src/Types/Sticky.php delete mode 100644 tests/Unit/ExampleTest.php create mode 100644 tests/Unit/MessageTest.php diff --git a/TODO b/TODO new file mode 100644 index 00000000..a0814dfd --- /dev/null +++ b/TODO @@ -0,0 +1,3 @@ + +Todo: + ☐ Item diff --git a/phpunit.xml b/phpunit.xml index c8a3a8b1..01bf269d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,10 +1,6 @@ - - - - src/ - - + + ./tests/Unit @@ -17,4 +13,9 @@ + + + src/ + + diff --git a/src/Alert.php b/src/Alert.php new file mode 100644 index 00000000..a5af1789 --- /dev/null +++ b/src/Alert.php @@ -0,0 +1,15 @@ +session = $session; - } - - protected function flash($type = 'flash') - { - $this->alert = [ - $this->type => true, - 'type' => $this->type, - 'title' => $this->title, - 'message' => $this->message, - 'status' => $this->status, - 'icon' => $this->icon, - - 'closable' => $this->closable, - 'un_closable' => $this->un_closable, - 'un_closable_strict' => $this->un_closable_strict, - 'self_destroy' => $this->self_destroy, - 'persist' => $this->persist, - - 'sticky_title' => $this->sticky_title, - 'sticky_message' => $this->sticky_message, - - 'modal_size' => $this->modal_size, - 'modal_view' => $this->modal_view, - - 'action_button_label' => $this->action_button_label, - 'action_button_url' => $this->action_button_url, - 'close_button_label' => $this->close_button_label, - 'close_button_url' => $this->close_button_url, - 'close_button_attributes' => $this->close_button_attributes, - - 'tag' => $this->tag, - ]; - - $this->session->$type('alert_message', $this->alert); - - return $this; - } - - protected function alert() - { - return $this->session->get('alert_message'); - } - - public function __call($method, $param) - { - $alert = $this->alert(); - if (is_array($alert) && property_exists($this, $method) && isset($alert[$method])) { - return $alert[$method]; - } - } - - //Actions - public function persist() - { - return $this->flash($type = 'put'); - } - - public function destroy() - { - return $this->flash($type = 'pull'); //retrieve an item and forget it - } - - public function unClosable() - { - $this->closable = false; - $this->un_closable = true; - - return $this->flash(); - } - - public function unClosableStrict() - { - $this->closable = false; - $this->un_closable = true; - $this->un_closable_strict = true; - - return $this->flash(); - } - - public function selfDestroy() - { - $this->self_destroy = true; - - return $this->flash(); - } - - public function setIcon($icon = '') - { - $this->icon = $icon; - - return $this->flash(); - } - - public function setActionButton($label = '', $url = '') - { - $this->action_button_label = $label; - $this->action_button_url = $url; - - return $this->flash(); - } - - public function setCloseButton($label = '', $url = '', $attributes = []) - { - $this->close_button_label = $label; - $this->close_button_url = $url; - $this->close_button_attributes = $attributes; - - return $this->flash(); - } - - public function large() - { - $this->modal_size = 'modal-lg'; - - return $this->flash(); - } - - public function small() - { - $this->modal_size = 'modal-sm'; - - return $this->flash(); - } - - //Alert status - public function success($class_name = 'success') - { - $this->status = $class_name; - $this->icon = 'fa fa-check-circle'; - - return $this->flash(); - } - - public function info($class_name = 'info') - { - $this->status = $class_name; - $this->icon = 'fa fa-info'; - - return $this->flash(); - } - - public function warning($class_name = 'warning') - { - $this->status = $class_name; - $this->icon = 'fa fa-warning'; - - return $this->flash(); - } - - public function error($class_name = 'danger') - { - $this->status = $class_name; - $this->icon = 'fa fa-times-circle'; - - return $this->flash(); - } - - public function royal($class_name = 'royal') - { - $this->status = $class_name; - $this->icon = 'fa fa-bullhorn'; - - return $this->flash(); - } - - public function primary($class_name = 'primary') - { - $this->status = $class_name; - $this->icon = 'fa fa-comments-o'; - - return $this->flash(); - } - - //Alert Types - public function modal($message, $title = '', $view = '') - { - $this->type = 'alert_modal_message'; - $this->title = $title; - $this->message = $message; - $this->modal_view = $view; - - return $this->flash(); - } - - public function form($message, $title = '') - { - $this->type = 'alert_form_message'; - $this->title = $title; - $this->message = $message; - - return $this->flash(); - } - - public function notify($message, $title = '') - { - $this->type = 'alert_notify_message'; - $this->title = $title; - $this->message = $message; - - return $this->flash(); - } - - public function sticky($message, $title = '') - { - $this->type = 'alert_sticky_message'; - $this->sticky_title = $title; - $this->sticky_message = $message; - - return $this->flash(); - } - - /** - * Check if given alert exists. - * - * @param $alert_type - * - * @return bool - */ - public function has($alert_type) - { - $alert = $this->alert(); - - $alert_type = "alert_{$alert_type}_message"; - - return is_array($alert) ? isset($alert[$alert_type]) : false; - } - - public function tag($name) - { - $this->tag = $name; - - return $this->flash(); - } - - /** - * Tag an alert. - * - * @param $name - * - * @return bool - */ - public function tagged($name) - { - $alert = $this->alert(); - - return $alert['tag'] == $name; - } - - /** - * Check if alert for success. - * - * @return bool - */ - public function hasSuccess() - { - return $this->message && $this->status == 'success'; - } - - /** - * Check if alert for error. - * - * @return bool - */ - public function hasError() - { - return $this->message && $this->status == 'error'; - } - - /** - * Check if alert for info. - * - * @return bool - */ - public function hasInfo() - { - return $this->message && $this->status == 'info'; - } -} diff --git a/src/Digitlimit/Alert/AlertServiceProvider.php b/src/Digitlimit/Alert/AlertServiceProvider.php deleted file mode 100644 index d32455bc..00000000 --- a/src/Digitlimit/Alert/AlertServiceProvider.php +++ /dev/null @@ -1,84 +0,0 @@ -loadViewsFrom(__DIR__.'/../resources/views', 'digitlimit'); - // $this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'digitlimit'); - - // Publishing is only necessary when using the CLI. - // if ($this->app->runningInConsole()) { - // $this->bootForConsole(); - // } - } - - /** - * Register any package services. - * - * @return void - */ - public function register(): void - { - // $this->app->singleton('alert', function ($app) { - // return new Alert($app['session.store']); - // }); - } - - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return ['alert']; - } - - /** - * Console-specific booting. - * - * @return void - */ - protected function bootForConsole(): void - { - // $this->publishes([ - // __DIR__.'/../migrations' => database_path('migrations'), - // ], 'alert.migrations'); - - // $this->publishes([ - // __DIR__.'/../seeders' => database_path('seeders'), - // ], 'alert.seeders'); - - // Publishing the configuration file. - /*$this->publishes([ - __DIR__.'/../config/alert.php' => config_path('alert.php'), - ], 'alert.config');*/ - - // Publishing the views. - /*$this->publishes([ - __DIR__.'/../resources/views' => base_path('resources/views/vendor/digitlimit'), - ], 'alert.views');*/ - - // Publishing assets. - /*$this->publishes([ - __DIR__.'/../resources/assets' => public_path('vendor/digitlimit'), - ], 'alert.views');*/ - - // Publishing the translation files. - /*$this->publishes([ - __DIR__.'/../resources/lang' => resource_path('lang/vendor/digitlimit'), - ], 'alert.views');*/ - - // Registering package commands. - // $this->commands([]); - } -} \ No newline at end of file diff --git a/src/Digitlimit/Alert/views/field.blade.php b/src/Digitlimit/Alert/views/field.blade.php deleted file mode 100644 index 396c8241..00000000 --- a/src/Digitlimit/Alert/views/field.blade.php +++ /dev/null @@ -1,9 +0,0 @@ -@if($errors->has($field)) -

- @if(isset($tag) && $tag) - {{ $errors->${$tag}->first($field) }} - @else - {{ $errors->first($field) }} - @endif -

-@endif \ No newline at end of file diff --git a/src/Digitlimit/Alert/views/form.blade.php b/src/Digitlimit/Alert/views/form.blade.php deleted file mode 100644 index 7b682311..00000000 --- a/src/Digitlimit/Alert/views/form.blade.php +++ /dev/null @@ -1,14 +0,0 @@ -@if(Alert::has('form')) - -@endif diff --git a/src/Digitlimit/Alert/views/modal.blade.php b/src/Digitlimit/Alert/views/modal.blade.php deleted file mode 100644 index a9465e9c..00000000 --- a/src/Digitlimit/Alert/views/modal.blade.php +++ /dev/null @@ -1,44 +0,0 @@ -@if ( Alert::has('modal') ) - - - - -@endif \ No newline at end of file diff --git a/src/Digitlimit/Alert/views/notify.blade.php b/src/Digitlimit/Alert/views/notify.blade.php deleted file mode 100644 index ccebecc1..00000000 --- a/src/Digitlimit/Alert/views/notify.blade.php +++ /dev/null @@ -1,11 +0,0 @@ -@if(Alert::has('notify')) - -@endif \ No newline at end of file diff --git a/src/Digitlimit/Alert/views/sticky.blade.php b/src/Digitlimit/Alert/views/sticky.blade.php deleted file mode 100644 index 85076de7..00000000 --- a/src/Digitlimit/Alert/views/sticky.blade.php +++ /dev/null @@ -1,18 +0,0 @@ -@if (Alert::has('sticky')) -
- - @if(Alert::closable()) - - @endif - - @if(Alert::title()) - - @if(Alert::icon())@endif - {{Alert::title()}} - - @endif - - {!!Alert::sticky_message()!!} - -
-@endif \ No newline at end of file diff --git a/src/Digitlimit/Alert/Facades/Alert.php b/src/Facades/Alert.php similarity index 56% rename from src/Digitlimit/Alert/Facades/Alert.php rename to src/Facades/Alert.php index 9c5b1c0d..bd65eb84 100644 --- a/src/Digitlimit/Alert/Facades/Alert.php +++ b/src/Facades/Alert.php @@ -7,12 +7,12 @@ class Alert extends Facade { /** - * Get the binding in the IoC container. + * Get the registered name of the component. * * @return string */ - protected static function getFacadeAccessor() + protected static function getFacadeAccessor(): string { - return 'digitlimit.alert'; + return 'alert'; } } diff --git a/src/Message.php b/src/Message.php new file mode 100644 index 00000000..8bc3a60b --- /dev/null +++ b/src/Message.php @@ -0,0 +1,45 @@ +title = $title; + + return $this; + } + + public function setContent(string $content) : self + { + $this->content = $content; + + return $this; + } + + public function getTitle() : string + { + return $this->title; + } + + public function getContent() : string + { + return $this->content; + } + + public function hasTitle() : bool + { + return !empty($this->title); + } + + public function hasContent() : bool + { + return !empty($this->content); + } +} \ No newline at end of file diff --git a/src/Types/Field.php b/src/Types/Field.php new file mode 100644 index 00000000..a4189ab5 --- /dev/null +++ b/src/Types/Field.php @@ -0,0 +1,8 @@ +toBeTrue(); -}); diff --git a/tests/Unit/MessageTest.php b/tests/Unit/MessageTest.php new file mode 100644 index 00000000..f17512d0 --- /dev/null +++ b/tests/Unit/MessageTest.php @@ -0,0 +1,10 @@ +setTitle('Congratualtions!'); + + expect($message->getTitle())->toBe('Congratualtions!'); + +}); From 05e780f416df2d22552407ca008b5501b2b0adb6 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sat, 29 Apr 2023 13:24:12 +0100 Subject: [PATCH 03/58] bug: fix ci tests --- .github/workflows/alert.yml | 14 ++------------ src/Button.php | 10 ++++++++++ src/Concerns/WithCloseButton.php | 8 ++++++++ tests/.gitkeep | 0 tests/AlertTest.php | 12 ------------ tests/Unit/MessageTest.php | 12 ++++++++++-- 6 files changed, 30 insertions(+), 26 deletions(-) create mode 100644 src/Button.php create mode 100644 src/Concerns/WithCloseButton.php delete mode 100644 tests/.gitkeep delete mode 100644 tests/AlertTest.php diff --git a/.github/workflows/alert.yml b/.github/workflows/alert.yml index a5f2bc58..3a3b3c85 100644 --- a/.github/workflows/alert.yml +++ b/.github/workflows/alert.yml @@ -16,20 +16,10 @@ jobs: with: php-version: '8.0' - uses: actions/checkout@v3 - - name: Copy .env - run: php -r "file_exists('.env') || copy('.env.example', '.env');" - name: Install Dependencies run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist - - name: Generate key - run: php artisan key:generate - - name: Directory Permissions - run: chmod -R 777 storage bootstrap/cache - - name: Create Database - run: | - mkdir -p database - touch database/database.sqlite - - name: Execute tests (Unit and Feature tests) via PHPUnit + - name: Execute tests env: DB_CONNECTION: sqlite DB_DATABASE: database/database.sqlite - run: vendor/bin/phpunit + run: php vendor/bin/testbench package:test diff --git a/src/Button.php b/src/Button.php new file mode 100644 index 00000000..056afb3f --- /dev/null +++ b/src/Button.php @@ -0,0 +1,10 @@ +setTitle('Congratualtions!'); + expect($message->hasTitle())->toBe(true); expect($message->getTitle())->toBe('Congratualtions!'); - }); + +it('can set content', function () { + $message = new Message(); + $message->setContent('This is a content'); + + expect($message->hasContent())->toBe(true); + expect($message->getContent())->toBe('This is a content'); +}); \ No newline at end of file From 48480bf09ac775ecb61371513e0e0393e9a0a4cf Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sat, 29 Apr 2023 13:38:51 +0100 Subject: [PATCH 04/58] bug: fix ci tests --- .github/workflows/alert.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/alert.yml b/.github/workflows/alert.yml index 3a3b3c85..ed0e2e58 100644 --- a/.github/workflows/alert.yml +++ b/.github/workflows/alert.yml @@ -2,7 +2,7 @@ name: Laravel on: push: - branches: [ "master" ] + branches: [ "master", "feature/laravel-10" ] pull_request: branches: [ "master" ] From 34618d0f68851aa3a0cbdd9170172c59cdfd4000 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sat, 29 Apr 2023 13:44:07 +0100 Subject: [PATCH 05/58] bug: fix ci tests --- .github/workflows/alert.yml | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/.github/workflows/alert.yml b/.github/workflows/alert.yml index ed0e2e58..7667504b 100644 --- a/.github/workflows/alert.yml +++ b/.github/workflows/alert.yml @@ -1,4 +1,4 @@ -name: Laravel +name: Testing on: push: @@ -6,18 +6,32 @@ on: pull_request: branches: [ "master" ] +permissions: + contents: read + jobs: - laravel-tests: + build: runs-on: ubuntu-latest steps: - - uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e - with: - php-version: '8.0' - uses: actions/checkout@v3 - - name: Install Dependencies - run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist + + - name: Validate composer.json and composer.lock + run: composer validate --strict + + - name: Cache Composer packages + id: composer-cache + uses: actions/cache@v3 + with: + path: vendor + key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} + restore-keys: | + ${{ runner.os }}-php- + + - name: Install dependencies + run: composer install --prefer-dist --no-progress + - name: Execute tests env: DB_CONNECTION: sqlite From 4ff1a797046097e81a0a8e5ccd9e4cde5700eeb2 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sat, 29 Apr 2023 20:24:23 +0100 Subject: [PATCH 06/58] create alert methods --- composer.json | 5 +++- src/Alert.php | 9 +++--- src/AlertFactory.php | 23 ++++++++++++++++ src/AlertServiceProvider.php | 10 +++++-- src/AlertStore.php | 53 ++++++++++++++++++++++++++++++++++++ src/Alerter.php | 8 ++++++ src/Helpers.php | 18 ++++++++++++ 7 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 src/AlertFactory.php create mode 100644 src/AlertStore.php create mode 100644 src/Alerter.php create mode 100644 src/Helpers.php diff --git a/composer.json b/composer.json index 565a14a6..10ef9b2e 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,10 @@ "autoload": { "psr-4": { "Digitlimit\\Alert\\": "src/" - } + }, + "files": [ + "src/Laracasts/Flash/functions.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/Alert.php b/src/Alert.php index a5af1789..f6bf3979 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -2,14 +2,13 @@ namespace Digitlimit\Alert; -use Illuminate\Session\Store; - class Alert { - protected Message $message; + protected AlertStore $store; - public function __construct($store) + public function __construct(AlertStore $store) { - + } + } diff --git a/src/AlertFactory.php b/src/AlertFactory.php new file mode 100644 index 00000000..53958576 --- /dev/null +++ b/src/AlertFactory.php @@ -0,0 +1,23 @@ +message = $message; + return $this; + } + + public function getMessage() : Message + { + return $this->message; + } + + +} diff --git a/src/AlertServiceProvider.php b/src/AlertServiceProvider.php index b45223fb..4ba94a0a 100644 --- a/src/AlertServiceProvider.php +++ b/src/AlertServiceProvider.php @@ -12,7 +12,7 @@ class AlertServiceProvider extends ServiceProvider */ public function boot(): void { - + $this->loadViewsFrom(__DIR__.'/../resources/views', 'alert'); } /** @@ -22,7 +22,9 @@ public function boot(): void */ public function register(): void { - + $this->app->singleton('alert', function ($app) { + return new Alert(new AlertStore($app['session.store'])); + }); } /** @@ -42,6 +44,8 @@ public function provides() */ protected function bootForConsole(): void { - + $this->publishes([ + __DIR__.'/../resources/views' => base_path('resources/views/vendor/digitlimit'), + ], 'alert.views'); } } \ No newline at end of file diff --git a/src/AlertStore.php b/src/AlertStore.php new file mode 100644 index 00000000..8ebe48f7 --- /dev/null +++ b/src/AlertStore.php @@ -0,0 +1,53 @@ +store = $store; + } + + public function getKey() : string + { + if($this->tag) { + return "$this->key.$this->tag"; + } + + return $this->key; + } + + public function tag(string $tag) : self + { + if($tag) { + $this->tag = $tag; + } + + return $this; + } + + public function store(Alerter $alerter) : self + { + $this + ->store + ->put($this->getKey(), $alerter); + + return $this; + } + + public function fetch() : Alerter + { + return $this + ->store + ->get($this->getKey()) ?? new Alerter(); + } +} diff --git a/src/Alerter.php b/src/Alerter.php new file mode 100644 index 00000000..6965b955 --- /dev/null +++ b/src/Alerter.php @@ -0,0 +1,8 @@ +message($message, $title); + } + + return $alert; + } + +} From 1db26aff3faae810da2b98618789dd7fee22f65f Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sun, 30 Apr 2023 01:57:29 +0100 Subject: [PATCH 07/58] fix tests --- src/Alert.php | 57 +++++++++++++++++- src/AlertFactory.php | 23 ------- src/AlertServiceProvider.php | 3 +- src/AlertStore.php | 4 +- src/Alerter.php | 60 ++++++++++++++++++- src/Button.php | 10 ---- src/Concerns/WithCloseButton.php | 8 --- src/Concerns/WithLevelable.php | 14 +++++ src/Enums/LevelType.php | 11 ++++ src/Level.php | 40 +++++++++++++ src/Message.php | 4 +- tests/Pest.php | 14 ++++- tests/Unit/AlertUnitTest.php | 18 ++++++ .../{MessageTest.php => MessageUnitTest.php} | 0 14 files changed, 214 insertions(+), 52 deletions(-) delete mode 100644 src/AlertFactory.php delete mode 100644 src/Button.php delete mode 100644 src/Concerns/WithCloseButton.php create mode 100644 src/Concerns/WithLevelable.php create mode 100644 src/Enums/LevelType.php create mode 100644 src/Level.php create mode 100644 tests/Unit/AlertUnitTest.php rename tests/Unit/{MessageTest.php => MessageUnitTest.php} (100%) diff --git a/src/Alert.php b/src/Alert.php index f6bf3979..c4e6922d 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -2,13 +2,64 @@ namespace Digitlimit\Alert; -class Alert +use Digitlimit\Alert\Level; +use Digitlimit\Alert\Concerns\WithLevelable; + +class Alert implements WithLevelable { - protected AlertStore $store; + protected Alerter $alerter; + + protected Level $level; - public function __construct(AlertStore $store) + public function __construct(Alerter $alerter) { + $this->alerter = $alerter; + $this->level = new Level(); + } + public function tag(string $tag) : self + { + $this->alerter->setTag($tag); + return $this; } + public function message(string $content, string $title='') : self + { + $message = new Message($content, $title); + $this->alerter->setMessage($message); + return $this; + } + + public function success() : self + { + $this->level->success(); + $this->alerter->setLevel($this->level); + return $this; + } + + public function info() : self + { + $this->level->info(); + $this->alerter->setLevel($this->level); + return $this; + } + + public function error() : self + { + $this->level->error(); + $this->alerter->setLevel($this->level); + return $this; + } + + public function warning() : self + { + $this->level->warning(); + $this->alerter->setLevel($this->level); + return $this; + } + + public function alerter() : Alerter + { + return $this->alerter; + } } diff --git a/src/AlertFactory.php b/src/AlertFactory.php deleted file mode 100644 index 53958576..00000000 --- a/src/AlertFactory.php +++ /dev/null @@ -1,23 +0,0 @@ -message = $message; - return $this; - } - - public function getMessage() : Message - { - return $this->message; - } - - -} diff --git a/src/AlertServiceProvider.php b/src/AlertServiceProvider.php index 4ba94a0a..13b638a1 100644 --- a/src/AlertServiceProvider.php +++ b/src/AlertServiceProvider.php @@ -23,7 +23,8 @@ public function boot(): void public function register(): void { $this->app->singleton('alert', function ($app) { - return new Alert(new AlertStore($app['session.store'])); + $store = new AlertStore($app['session.store']); + return new Alert(new Alerter($store)); }); } diff --git a/src/AlertStore.php b/src/AlertStore.php index 8ebe48f7..aefbe3a7 100644 --- a/src/AlertStore.php +++ b/src/AlertStore.php @@ -10,7 +10,7 @@ class AlertStore protected string $key = 'digitlimit.alert'; - protected string $tag = ''; + protected string $tag = 'default'; public function __construct(Store $store) { @@ -48,6 +48,6 @@ public function fetch() : Alerter { return $this ->store - ->get($this->getKey()) ?? new Alerter(); + ->get($this->getKey()) ?? new Alerter($this); } } diff --git a/src/Alerter.php b/src/Alerter.php index 6965b955..525739de 100644 --- a/src/Alerter.php +++ b/src/Alerter.php @@ -2,7 +2,65 @@ namespace Digitlimit\Alert; +use Illuminate\Support\Str; + class Alerter { - + private AlertStore $store; + + private Level $level; + + private Message $message; + + private ?string $tag = null; + + public function __construct(AlertStore $store) + { + $this->store = $store; + } + + protected function restore() : void + { + if($this->tag){ + $this->store->tag($this->tag); + } + + $this->store->store($this); + } + + public function setTag(string $tag) : self + { + $this->tag = Str::slug($tag); + $this->restore(); + return $this; + } + + public function setMessage(Message $message) : self + { + $this->message = $message; + $this->restore(); + return $this; + } + + public function setLevel(Level $level) : self + { + $this->level = $level; + $this->restore(); + return $this; + } + + public function getMessage() : Message + { + return $this->message; + } + + public function getLevel() : Level + { + return $this->level; + } + + public function getKey() : string + { + return $this->store->getKey(); + } } diff --git a/src/Button.php b/src/Button.php deleted file mode 100644 index 056afb3f..00000000 --- a/src/Button.php +++ /dev/null @@ -1,10 +0,0 @@ -success(); + } + + public function type() : LevelType + { + return $this->levelType; + } + + public function success() : void + { + $this->levelType = LevelType::SUCCESS; + } + + public function info() : void + { + $this->levelType = LevelType::INFO; + } + + public function error() : void + { + $this->levelType = LevelType::ERROR; + } + + public function warning() : void + { + $this->levelType = LevelType::WARNING; + } +} \ No newline at end of file diff --git a/src/Message.php b/src/Message.php index 8bc3a60b..e7e73bd8 100644 --- a/src/Message.php +++ b/src/Message.php @@ -5,8 +5,8 @@ class Message { public function __construct( - protected string $title = '', - protected string $content = '' + protected string $content = '', + protected string $title = '' ){} public function setTitle(string $title) : self diff --git a/tests/Pest.php b/tests/Pest.php index 5949c617..e1c34c1b 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,4 +1,8 @@ tag('page footer') + ->info() + ->message('Thank you!'); + + $alerter = $alert->alerter(); + + expect($alerter->getKey())->toBe('digitlimit.alert.page-footer'); + expect($alerter->getLevel()->type())->toEqual(LevelType::INFO); + expect($alerter->getMessage())->toEqual(new Message('Thank you!')); +}); diff --git a/tests/Unit/MessageTest.php b/tests/Unit/MessageUnitTest.php similarity index 100% rename from tests/Unit/MessageTest.php rename to tests/Unit/MessageUnitTest.php From 7aa67defa69a20e7088b9457434e261f22020f0d Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sun, 30 Apr 2023 02:05:26 +0100 Subject: [PATCH 08/58] fix tests --- tests/Unit/AlertUnitTest.php | 53 +++++++++++++++++++++++++++++++----- 1 file changed, 46 insertions(+), 7 deletions(-) diff --git a/tests/Unit/AlertUnitTest.php b/tests/Unit/AlertUnitTest.php index d03defe3..f15c5765 100644 --- a/tests/Unit/AlertUnitTest.php +++ b/tests/Unit/AlertUnitTest.php @@ -2,17 +2,56 @@ use Digitlimit\Alert\Message; use Digitlimit\Alert\Enums\LevelType; -it('can alert', function () +it('has alert with given message', function () { $alert = alert(); - $alert - ->tag('page footer') - ->info() - ->message('Thank you!'); + $alert->message('Thank you!'); + + $alerter = $alert->alerter(); + expect($alerter->getMessage())->toEqual(new Message('Thank you!')); +}); + +it('has alert with given tag', function () +{ + $alert = alert(); + $alert->tag('page footer'); $alerter = $alert->alerter(); - expect($alerter->getKey())->toBe('digitlimit.alert.page-footer'); +}); + +it('has alert with info level', function () +{ + $alert = alert(); + $alert->info(); + + $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(LevelType::INFO); - expect($alerter->getMessage())->toEqual(new Message('Thank you!')); +}); + +it('has alert with success level', function () +{ + $alert = alert(); + $alert->success(); + + $alerter = $alert->alerter(); + expect($alerter->getLevel()->type())->toEqual(LevelType::SUCCESS); +}); + +it('has alert with error level', function () +{ + $alert = alert(); + $alert->error(); + + $alerter = $alert->alerter(); + expect($alerter->getLevel()->type())->toEqual(LevelType::ERROR); +}); + +it('has alert with warning level', function () +{ + $alert = alert(); + $alert->warning(); + + $alerter = $alert->alerter(); + expect($alerter->getLevel()->type())->toEqual(LevelType::WARNING); }); From 6f17ecd15ca0c6e3b22020a0e6527f8552507aa1 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sun, 30 Apr 2023 02:15:49 +0100 Subject: [PATCH 09/58] fix tests --- src/Alert.php | 4 +++- tests/Unit/AlertUnitTest.php | 32 ++++++++++++++++++++------------ tests/Unit/MessageUnitTest.php | 8 ++++---- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/Alert.php b/src/Alert.php index c4e6922d..a69b23bf 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -13,8 +13,10 @@ class Alert implements WithLevelable public function __construct(Alerter $alerter) { - $this->alerter = $alerter; $this->level = new Level(); + + $this->alerter = $alerter; + $this->alerter->setLevel($this->level); } public function tag(string $tag) : self diff --git a/tests/Unit/AlertUnitTest.php b/tests/Unit/AlertUnitTest.php index f15c5765..31d3614c 100644 --- a/tests/Unit/AlertUnitTest.php +++ b/tests/Unit/AlertUnitTest.php @@ -2,56 +2,64 @@ use Digitlimit\Alert\Message; use Digitlimit\Alert\Enums\LevelType; -it('has alert with given message', function () +it('has alert with the given message', function () { $alert = alert(); $alert->message('Thank you!'); $alerter = $alert->alerter(); expect($alerter->getMessage())->toEqual(new Message('Thank you!')); -}); +})->name('alert', 'alert-message'); -it('has alert with given tag', function () +it('has alert with the given tag', function () { $alert = alert(); $alert->tag('page footer'); $alerter = $alert->alerter(); expect($alerter->getKey())->toBe('digitlimit.alert.page-footer'); -}); +})->name('alert', 'alert-tag'); -it('has alert with info level', function () +it('has alert with the given info level', function () { $alert = alert(); $alert->info(); $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(LevelType::INFO); -}); +})->name('alert', 'alert-info-level'); -it('has alert with success level', function () +it('has alert with the given success level', function () { $alert = alert(); $alert->success(); $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(LevelType::SUCCESS); -}); +})->name('alert', 'alert-success-level'); -it('has alert with error level', function () +it('has alert with the given error level', function () { $alert = alert(); $alert->error(); $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(LevelType::ERROR); -}); +})->name('alert', 'alert-error-level'); -it('has alert with warning level', function () +it('has alert with the given warning level', function () { $alert = alert(); $alert->warning(); $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(LevelType::WARNING); -}); +})->name('alert', 'alert-warning-level'); + +it('has alert with the given success level as deafult', function () +{ + $alert = alert(); + + $alerter = $alert->alerter(); + expect($alerter->getLevel()->type())->toEqual(LevelType::SUCCESS); +})->name('alert', 'alert-success-level-default'); \ No newline at end of file diff --git a/tests/Unit/MessageUnitTest.php b/tests/Unit/MessageUnitTest.php index 6bd83a52..fa995e1a 100644 --- a/tests/Unit/MessageUnitTest.php +++ b/tests/Unit/MessageUnitTest.php @@ -1,18 +1,18 @@ setTitle('Congratualtions!'); expect($message->hasTitle())->toBe(true); expect($message->getTitle())->toBe('Congratualtions!'); -}); +})->name('message', 'message-title'); -it('can set content', function () { +it('has message with the given content', function () { $message = new Message(); $message->setContent('This is a content'); expect($message->hasContent())->toBe(true); expect($message->getContent())->toBe('This is a content'); -}); \ No newline at end of file +})->name('message', 'message-content'); \ No newline at end of file From 7216d978125be7412609c372d8d6a83ec01a4707 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sun, 30 Apr 2023 02:44:33 +0100 Subject: [PATCH 10/58] add alert type --- src/Alert.php | 6 ++-- src/AlertLevel.php | 40 +++++++++++++++++++++++ src/AlertType.php | 45 ++++++++++++++++++++++++++ src/Alerter.php | 6 ++-- src/Concerns/WithClosable.php | 14 ++++++++ src/Enums/{LevelType.php => Level.php} | 2 +- src/Enums/Type.php | 12 +++++++ src/Level.php | 40 ----------------------- src/Types/Field.php | 8 ----- src/Types/Modal.php | 8 ----- src/Types/Notify.php | 8 ----- src/Types/Sticky.php | 8 ----- tests/Unit/AlertUnitTest.php | 12 +++---- 13 files changed, 124 insertions(+), 85 deletions(-) create mode 100644 src/AlertLevel.php create mode 100644 src/AlertType.php create mode 100644 src/Concerns/WithClosable.php rename src/Enums/{LevelType.php => Level.php} (87%) create mode 100644 src/Enums/Type.php delete mode 100644 src/Level.php delete mode 100644 src/Types/Field.php delete mode 100644 src/Types/Modal.php delete mode 100644 src/Types/Notify.php delete mode 100644 src/Types/Sticky.php diff --git a/src/Alert.php b/src/Alert.php index a69b23bf..f226fde3 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -2,18 +2,18 @@ namespace Digitlimit\Alert; -use Digitlimit\Alert\Level; +use Digitlimit\Alert\AlertLevel; use Digitlimit\Alert\Concerns\WithLevelable; class Alert implements WithLevelable { protected Alerter $alerter; - protected Level $level; + protected AlertLevel $level; public function __construct(Alerter $alerter) { - $this->level = new Level(); + $this->level = new AlertLevel(); $this->alerter = $alerter; $this->alerter->setLevel($this->level); diff --git a/src/AlertLevel.php b/src/AlertLevel.php new file mode 100644 index 00000000..125b8919 --- /dev/null +++ b/src/AlertLevel.php @@ -0,0 +1,40 @@ +success(); + } + + public function type() : Level + { + return $this->level; + } + + public function success() : void + { + $this->level = Level::SUCCESS; + } + + public function info() : void + { + $this->level = Level::INFO; + } + + public function error() : void + { + $this->level = Level::ERROR; + } + + public function warning() : void + { + $this->level = Level::WARNING; + } +} \ No newline at end of file diff --git a/src/AlertType.php b/src/AlertType.php new file mode 100644 index 00000000..5333046b --- /dev/null +++ b/src/AlertType.php @@ -0,0 +1,45 @@ +bar(); + } + + public function name() : Type + { + return $this->type; + } + + public function bar() : void + { + $this->type = Type::BAR; + } + + public function field() : void + { + $this->type = Type::FIELD; + } + + public function modal() : void + { + $this->type = Type::MODAL; + } + + public function notify() : void + { + $this->type = Type::NOTIFY; + } + + public function sticky() : void + { + $this->type = Type::STICKY; + } +} \ No newline at end of file diff --git a/src/Alerter.php b/src/Alerter.php index 525739de..91a97bca 100644 --- a/src/Alerter.php +++ b/src/Alerter.php @@ -8,7 +8,7 @@ class Alerter { private AlertStore $store; - private Level $level; + private AlertLevel $level; private Message $message; @@ -42,7 +42,7 @@ public function setMessage(Message $message) : self return $this; } - public function setLevel(Level $level) : self + public function setLevel(AlertLevel $level) : self { $this->level = $level; $this->restore(); @@ -54,7 +54,7 @@ public function getMessage() : Message return $this->message; } - public function getLevel() : Level + public function getLevel() : AlertLevel { return $this->level; } diff --git a/src/Concerns/WithClosable.php b/src/Concerns/WithClosable.php new file mode 100644 index 00000000..461c752e --- /dev/null +++ b/src/Concerns/WithClosable.php @@ -0,0 +1,14 @@ +success(); - } - - public function type() : LevelType - { - return $this->levelType; - } - - public function success() : void - { - $this->levelType = LevelType::SUCCESS; - } - - public function info() : void - { - $this->levelType = LevelType::INFO; - } - - public function error() : void - { - $this->levelType = LevelType::ERROR; - } - - public function warning() : void - { - $this->levelType = LevelType::WARNING; - } -} \ No newline at end of file diff --git a/src/Types/Field.php b/src/Types/Field.php deleted file mode 100644 index a4189ab5..00000000 --- a/src/Types/Field.php +++ /dev/null @@ -1,8 +0,0 @@ -info(); $alerter = $alert->alerter(); - expect($alerter->getLevel()->type())->toEqual(LevelType::INFO); + expect($alerter->getLevel()->type())->toEqual(Level::INFO); })->name('alert', 'alert-info-level'); it('has alert with the given success level', function () @@ -35,7 +35,7 @@ $alert->success(); $alerter = $alert->alerter(); - expect($alerter->getLevel()->type())->toEqual(LevelType::SUCCESS); + expect($alerter->getLevel()->type())->toEqual(Level::SUCCESS); })->name('alert', 'alert-success-level'); it('has alert with the given error level', function () @@ -44,7 +44,7 @@ $alert->error(); $alerter = $alert->alerter(); - expect($alerter->getLevel()->type())->toEqual(LevelType::ERROR); + expect($alerter->getLevel()->type())->toEqual(Level::ERROR); })->name('alert', 'alert-error-level'); it('has alert with the given warning level', function () @@ -53,7 +53,7 @@ $alert->warning(); $alerter = $alert->alerter(); - expect($alerter->getLevel()->type())->toEqual(LevelType::WARNING); + expect($alerter->getLevel()->type())->toEqual(Level::WARNING); })->name('alert', 'alert-warning-level'); it('has alert with the given success level as deafult', function () @@ -61,5 +61,5 @@ $alert = alert(); $alerter = $alert->alerter(); - expect($alerter->getLevel()->type())->toEqual(LevelType::SUCCESS); + expect($alerter->getLevel()->type())->toEqual(Level::SUCCESS); })->name('alert', 'alert-success-level-default'); \ No newline at end of file From 89a6c7e863725cae71d7c92d87453a62d77df7af Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sun, 30 Apr 2023 09:41:15 +0100 Subject: [PATCH 11/58] add type tests --- src/Alert.php | 44 ++++++--------- src/Alerter.php | 14 +++++ .../{WithLevelable.php => WithLevels.php} | 6 +- src/Concerns/WithTypes.php | 16 ++++++ src/Traits/LevelsTrait.php | 34 ++++++++++++ src/Traits/TypesTrait.php | 41 ++++++++++++++ .../AlertLevelTest.php} | 29 ++-------- tests/Feature/AlertMessageTest.php | 29 ++++++++++ tests/Feature/AlertTagTest.php | 10 ++++ tests/Feature/AlertTypeTest.php | 55 +++++++++++++++++++ tests/Feature/ExampleTest.php | 5 -- .../{MessageUnitTest.php => MessageTest.php} | 0 12 files changed, 224 insertions(+), 59 deletions(-) rename src/Concerns/{WithLevelable.php => WithLevels.php} (56%) create mode 100644 src/Concerns/WithTypes.php create mode 100644 src/Traits/LevelsTrait.php create mode 100644 src/Traits/TypesTrait.php rename tests/{Unit/AlertUnitTest.php => Feature/AlertLevelTest.php} (58%) create mode 100644 tests/Feature/AlertMessageTest.php create mode 100644 tests/Feature/AlertTagTest.php create mode 100644 tests/Feature/AlertTypeTest.php delete mode 100644 tests/Feature/ExampleTest.php rename tests/Unit/{MessageUnitTest.php => MessageTest.php} (100%) diff --git a/src/Alert.php b/src/Alert.php index f226fde3..5e27dace 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -3,20 +3,32 @@ namespace Digitlimit\Alert; use Digitlimit\Alert\AlertLevel; -use Digitlimit\Alert\Concerns\WithLevelable; +use Digitlimit\Alert\AlertType; -class Alert implements WithLevelable +use Digitlimit\Alert\Concerns\WithLevels; +use Digitlimit\Alert\Concerns\WithTypes; + +use Digitlimit\Alert\Traits\LevelsTrait; +use Digitlimit\Alert\Traits\TypesTrait; + +class Alert implements WithLevels, WithTypes { + use LevelsTrait, TypesTrait; + protected Alerter $alerter; protected AlertLevel $level; + protected AlertType $type; + public function __construct(Alerter $alerter) { $this->level = new AlertLevel(); - + $this->type = new AlertType(); $this->alerter = $alerter; + $this->alerter->setLevel($this->level); + $this->alerter->setType($this->type); } public function tag(string $tag) : self @@ -32,31 +44,9 @@ public function message(string $content, string $title='') : self return $this; } - public function success() : self + public function view(string $name, array $data=[]) : self { - $this->level->success(); - $this->alerter->setLevel($this->level); - return $this; - } - - public function info() : self - { - $this->level->info(); - $this->alerter->setLevel($this->level); - return $this; - } - - public function error() : self - { - $this->level->error(); - $this->alerter->setLevel($this->level); - return $this; - } - - public function warning() : self - { - $this->level->warning(); - $this->alerter->setLevel($this->level); + //@todo return $this; } diff --git a/src/Alerter.php b/src/Alerter.php index 91a97bca..9f5c678b 100644 --- a/src/Alerter.php +++ b/src/Alerter.php @@ -10,6 +10,8 @@ class Alerter private AlertLevel $level; + private AlertType $type; + private Message $message; private ?string $tag = null; @@ -49,6 +51,13 @@ public function setLevel(AlertLevel $level) : self return $this; } + public function setType(AlertType $type) : self + { + $this->type = $type; + $this->restore(); + return $this; + } + public function getMessage() : Message { return $this->message; @@ -59,6 +68,11 @@ public function getLevel() : AlertLevel return $this->level; } + public function getType() : AlertType + { + return $this->type; + } + public function getKey() : string { return $this->store->getKey(); diff --git a/src/Concerns/WithLevelable.php b/src/Concerns/WithLevels.php similarity index 56% rename from src/Concerns/WithLevelable.php rename to src/Concerns/WithLevels.php index 6966cd89..b8ee5d41 100644 --- a/src/Concerns/WithLevelable.php +++ b/src/Concerns/WithLevels.php @@ -2,13 +2,13 @@ namespace Digitlimit\Alert\Concerns; -interface WithLevelable +interface WithLevels { public function success() : self; public function info() : self; - public function error(); + public function error() : self; - public function warning(); + public function warning() : self; } \ No newline at end of file diff --git a/src/Concerns/WithTypes.php b/src/Concerns/WithTypes.php new file mode 100644 index 00000000..2a10627f --- /dev/null +++ b/src/Concerns/WithTypes.php @@ -0,0 +1,16 @@ +level->success(); + $this->alerter->setLevel($this->level); + return $this; + } + + public function info() : self + { + $this->level->info(); + $this->alerter->setLevel($this->level); + return $this; + } + + public function error() : self + { + $this->level->error(); + $this->alerter->setLevel($this->level); + return $this; + } + + public function warning() : self + { + $this->level->warning(); + $this->alerter->setLevel($this->level); + return $this; + } +} diff --git a/src/Traits/TypesTrait.php b/src/Traits/TypesTrait.php new file mode 100644 index 00000000..2ab80d18 --- /dev/null +++ b/src/Traits/TypesTrait.php @@ -0,0 +1,41 @@ +type->bar(); + $this->alerter->setType($this->type); + return $this; + } + + public function field() : self + { + $this->type->field(); + $this->alerter->setType($this->type); + return $this; + } + + public function modal() : self + { + $this->type->modal(); + $this->alerter->setType($this->type); + return $this; + } + + public function notify() : self + { + $this->type->notify(); + $this->alerter->setType($this->type); + return $this; + } + + public function sticky() : self + { + $this->type->sticky(); + $this->alerter->setType($this->type); + return $this; + } +} diff --git a/tests/Unit/AlertUnitTest.php b/tests/Feature/AlertLevelTest.php similarity index 58% rename from tests/Unit/AlertUnitTest.php rename to tests/Feature/AlertLevelTest.php index 8396033c..a7b6b2e6 100644 --- a/tests/Unit/AlertUnitTest.php +++ b/tests/Feature/AlertLevelTest.php @@ -1,25 +1,6 @@ message('Thank you!'); - - $alerter = $alert->alerter(); - expect($alerter->getMessage())->toEqual(new Message('Thank you!')); -})->name('alert', 'alert-message'); - -it('has alert with the given tag', function () -{ - $alert = alert(); - $alert->tag('page footer'); - - $alerter = $alert->alerter(); - expect($alerter->getKey())->toBe('digitlimit.alert.page-footer'); -})->name('alert', 'alert-tag'); - it('has alert with the given info level', function () { $alert = alert(); @@ -27,7 +8,7 @@ $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(Level::INFO); -})->name('alert', 'alert-info-level'); +})->name('alert-level', 'alert-info-level'); it('has alert with the given success level', function () { @@ -36,7 +17,7 @@ $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(Level::SUCCESS); -})->name('alert', 'alert-success-level'); +})->name('alert-level', 'alert-success-level'); it('has alert with the given error level', function () { @@ -45,7 +26,7 @@ $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(Level::ERROR); -})->name('alert', 'alert-error-level'); +})->name('alert-level', 'alert-error-level'); it('has alert with the given warning level', function () { @@ -54,7 +35,7 @@ $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(Level::WARNING); -})->name('alert', 'alert-warning-level'); +})->name('alert-level', 'alert-warning-level'); it('has alert with the given success level as deafult', function () { @@ -62,4 +43,4 @@ $alerter = $alert->alerter(); expect($alerter->getLevel()->type())->toEqual(Level::SUCCESS); -})->name('alert', 'alert-success-level-default'); \ No newline at end of file +})->name('alert-level', 'alert-success-level-default'); \ No newline at end of file diff --git a/tests/Feature/AlertMessageTest.php b/tests/Feature/AlertMessageTest.php new file mode 100644 index 00000000..d3d0ca81 --- /dev/null +++ b/tests/Feature/AlertMessageTest.php @@ -0,0 +1,29 @@ +message('Thank you!'); + + $alerter = $alert->alerter(); + expect($alerter->getMessage()->getContent())->toEqual('Thank you!'); +})->name('alert-message', 'alert-message-content'); + +it('has alert with the given message title', function () +{ + $alert = alert(); + $alert->message('Thank you!', 'Title 1'); + + $alerter = $alert->alerter(); + expect($alerter->getMessage()->getTitle())->toEqual('Title 1'); +})->name('alert-message', 'alert-message-title'); + +it('has alert with the given message object', function () +{ + $alert = alert(); + $alert->message('Thank you!', 'Title 2'); + + $alerter = $alert->alerter(); + expect($alerter->getMessage())->toEqual(new Message('Thank you!', 'Title 2')); +})->name('alert-message', 'alert-message-complete'); diff --git a/tests/Feature/AlertTagTest.php b/tests/Feature/AlertTagTest.php new file mode 100644 index 00000000..71c3789e --- /dev/null +++ b/tests/Feature/AlertTagTest.php @@ -0,0 +1,10 @@ +tag('page footer'); + + $alerter = $alert->alerter(); + expect($alerter->getKey())->toBe('digitlimit.alert.page-footer'); +})->name('alert', 'alert-tag'); \ No newline at end of file diff --git a/tests/Feature/AlertTypeTest.php b/tests/Feature/AlertTypeTest.php new file mode 100644 index 00000000..492e3cf1 --- /dev/null +++ b/tests/Feature/AlertTypeTest.php @@ -0,0 +1,55 @@ +bar(); + + $alerter = $alert->alerter(); + expect($alerter->getType()->name())->toEqual(Type::BAR); +})->name('alert-type', 'alert-bar-type'); + +it('has alert with the given field type', function () +{ + $alert = alert(); + $alert->field(); + + $alerter = $alert->alerter(); + expect($alerter->getType()->name())->toEqual(Type::FIELD); +})->name('alert-type', 'alert-field-type'); + +it('has alert with the given modal type', function () +{ + $alert = alert(); + $alert->modal(); + + $alerter = $alert->alerter(); + expect($alerter->getType()->name())->toEqual(Type::MODAL); +})->name('alert-type', 'alert-modal-type'); + +it('has alert with the given notify type', function () +{ + $alert = alert(); + $alert->notify(); + + $alerter = $alert->alerter(); + expect($alerter->getType()->name())->toEqual(Type::NOTIFY); +})->name('alert-type', 'alert-notify-type'); + +it('has alert with the given sticky type', function () +{ + $alert = alert(); + $alert->sticky(); + + $alerter = $alert->alerter(); + expect($alerter->getType()->name())->toEqual(Type::STICKY); +})->name('alert-type', 'alert-sticky-type'); + +it('has alert with the given bar type as deafult', function () +{ + $alert = alert(); + + $alerter = $alert->alerter(); + expect($alerter->getType()->name())->toEqual(Type::BAR); +})->name('alert-type', 'alert-bar-type-default'); \ No newline at end of file diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php deleted file mode 100644 index 61cd84c3..00000000 --- a/tests/Feature/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/Unit/MessageUnitTest.php b/tests/Unit/MessageTest.php similarity index 100% rename from tests/Unit/MessageUnitTest.php rename to tests/Unit/MessageTest.php From 5b2b28e0dd487c9daa60129ab367451f7a76ac7f Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sun, 30 Apr 2023 21:18:25 +0100 Subject: [PATCH 12/58] update view --- resources/views/components/bar.blade.php | 4 +++ resources/views/components/field.blade.php | 4 +++ resources/views/components/modal.blade.php | 17 ++++++++++++ resources/views/components/notify.blade.php | 11 ++++++++ resources/views/components/sticky.blade.php | 4 +++ src/AlertServiceProvider.php | 29 ++++++++++++++------- src/View/Components/Bar.php | 26 ++++++++++++++++++ src/View/Components/Field.php | 26 ++++++++++++++++++ src/View/Components/Modal.php | 26 ++++++++++++++++++ src/View/Components/Notify.php | 26 ++++++++++++++++++ src/View/Components/Sticky.php | 26 ++++++++++++++++++ 11 files changed, 190 insertions(+), 9 deletions(-) create mode 100644 resources/views/components/bar.blade.php create mode 100644 resources/views/components/field.blade.php create mode 100644 resources/views/components/modal.blade.php create mode 100644 resources/views/components/notify.blade.php create mode 100644 resources/views/components/sticky.blade.php create mode 100644 src/View/Components/Bar.php create mode 100644 src/View/Components/Field.php create mode 100644 src/View/Components/Modal.php create mode 100644 src/View/Components/Notify.php create mode 100644 src/View/Components/Sticky.php diff --git a/resources/views/components/bar.blade.php b/resources/views/components/bar.blade.php new file mode 100644 index 00000000..11e33a07 --- /dev/null +++ b/resources/views/components/bar.blade.php @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/resources/views/components/field.blade.php b/resources/views/components/field.blade.php new file mode 100644 index 00000000..11e33a07 --- /dev/null +++ b/resources/views/components/field.blade.php @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/resources/views/components/modal.blade.php b/resources/views/components/modal.blade.php new file mode 100644 index 00000000..267f4d90 --- /dev/null +++ b/resources/views/components/modal.blade.php @@ -0,0 +1,17 @@ + \ No newline at end of file diff --git a/resources/views/components/notify.blade.php b/resources/views/components/notify.blade.php new file mode 100644 index 00000000..30cdb6b7 --- /dev/null +++ b/resources/views/components/notify.blade.php @@ -0,0 +1,11 @@ + \ No newline at end of file diff --git a/resources/views/components/sticky.blade.php b/resources/views/components/sticky.blade.php new file mode 100644 index 00000000..11e33a07 --- /dev/null +++ b/resources/views/components/sticky.blade.php @@ -0,0 +1,4 @@ + \ No newline at end of file diff --git a/src/AlertServiceProvider.php b/src/AlertServiceProvider.php index 13b638a1..5fb9bfc4 100644 --- a/src/AlertServiceProvider.php +++ b/src/AlertServiceProvider.php @@ -2,23 +2,24 @@ namespace Digitlimit\Alert; use Illuminate\Support\ServiceProvider; +use Illuminate\Support\Facades\Blade; + +use Digitlimit\View\Components; class AlertServiceProvider extends ServiceProvider { /** * Perform post-registration booting of services. - * - * @return void */ public function boot(): void { $this->loadViewsFrom(__DIR__.'/../resources/views', 'alert'); + + $this->registerComponents(); } /** * Register any package services. - * - * @return void */ public function register(): void { @@ -30,18 +31,14 @@ public function register(): void /** * Get the services provided by the provider. - * - * @return array */ - public function provides() + public function provides() : array { return ['alert']; } /** * Console-specific booting. - * - * @return void */ protected function bootForConsole(): void { @@ -49,4 +46,18 @@ protected function bootForConsole(): void __DIR__.'/../resources/views' => base_path('resources/views/vendor/digitlimit'), ], 'alert.views'); } + + /** + * Register alert components + */ + protected function registerComponents() : void + { + Blade::componentNamespace('Digitlimit\\Views\\Components', 'alert'); + + Blade::component('alert-bar', Components\Bar::class); + Blade::component('alert-field', Components\Field::class); + Blade::component('alert-modal', Components\Modal::class); + Blade::component('alert-notify', Components\Notify::class); + Blade::component('alert-sticky', Components\Sticky::class); + } } \ No newline at end of file diff --git a/src/View/Components/Bar.php b/src/View/Components/Bar.php new file mode 100644 index 00000000..8fc45c7c --- /dev/null +++ b/src/View/Components/Bar.php @@ -0,0 +1,26 @@ + Date: Sun, 30 Apr 2023 21:31:24 +0100 Subject: [PATCH 13/58] update composer --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 10ef9b2e..e198c81d 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,7 @@ "Digitlimit\\Alert\\": "src/" }, "files": [ - "src/Laracasts/Flash/functions.php" + "src/Helpers.php" ] }, "autoload-dev": { From 0a68db37fb29352d14fe023cdce9d38e9ec791f9 Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sun, 30 Apr 2023 23:05:50 +0100 Subject: [PATCH 14/58] fix component namespace --- TODO | 2 +- src/AlertServiceProvider.php | 4 ++-- src/View/Components/Bar.php | 2 +- src/View/Components/Field.php | 2 +- src/View/Components/Modal.php | 2 +- src/View/Components/Notify.php | 2 +- src/View/Components/Sticky.php | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/TODO b/TODO index a0814dfd..ac169bc9 100644 --- a/TODO +++ b/TODO @@ -1,3 +1,3 @@ Todo: - ☐ Item + ☐ composer require "digitlimit/alert":"dev-feature/laravel-10" \ No newline at end of file diff --git a/src/AlertServiceProvider.php b/src/AlertServiceProvider.php index 5fb9bfc4..f3619853 100644 --- a/src/AlertServiceProvider.php +++ b/src/AlertServiceProvider.php @@ -4,7 +4,7 @@ use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Blade; -use Digitlimit\View\Components; +use Digitlimit\Alert\View\Components; class AlertServiceProvider extends ServiceProvider { @@ -52,7 +52,7 @@ protected function bootForConsole(): void */ protected function registerComponents() : void { - Blade::componentNamespace('Digitlimit\\Views\\Components', 'alert'); + Blade::componentNamespace('Digitlimit\\Alert\View\\Components', 'alert'); Blade::component('alert-bar', Components\Bar::class); Blade::component('alert-field', Components\Field::class); diff --git a/src/View/Components/Bar.php b/src/View/Components/Bar.php index 8fc45c7c..8c9c3d57 100644 --- a/src/View/Components/Bar.php +++ b/src/View/Components/Bar.php @@ -1,6 +1,6 @@ Date: Sun, 30 Apr 2023 23:25:16 +0100 Subject: [PATCH 15/58] fix component view namesapce --- src/View/Components/Bar.php | 2 +- src/View/Components/Field.php | 2 +- src/View/Components/Modal.php | 2 +- src/View/Components/Notify.php | 2 +- src/View/Components/Sticky.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/View/Components/Bar.php b/src/View/Components/Bar.php index 8c9c3d57..508f0e77 100644 --- a/src/View/Components/Bar.php +++ b/src/View/Components/Bar.php @@ -21,6 +21,6 @@ public function __construct() */ public function render(): View|Closure|string { - return view('components.bar'); + return view('alert::components.bar'); } } diff --git a/src/View/Components/Field.php b/src/View/Components/Field.php index b99768e1..69e10b0e 100644 --- a/src/View/Components/Field.php +++ b/src/View/Components/Field.php @@ -21,6 +21,6 @@ public function __construct() */ public function render(): View|Closure|string { - return view('components.field'); + return view('alert::components.field'); } } diff --git a/src/View/Components/Modal.php b/src/View/Components/Modal.php index c6f008e7..fea8ee9d 100644 --- a/src/View/Components/Modal.php +++ b/src/View/Components/Modal.php @@ -21,6 +21,6 @@ public function __construct() */ public function render(): View|Closure|string { - return view('components.modal'); + return view('alert::components.modal'); } } diff --git a/src/View/Components/Notify.php b/src/View/Components/Notify.php index aa51571c..c8e0a027 100644 --- a/src/View/Components/Notify.php +++ b/src/View/Components/Notify.php @@ -21,6 +21,6 @@ public function __construct() */ public function render(): View|Closure|string { - return view('components.notify'); + return view('alert::components.notify'); } } diff --git a/src/View/Components/Sticky.php b/src/View/Components/Sticky.php index af669afb..183f045a 100644 --- a/src/View/Components/Sticky.php +++ b/src/View/Components/Sticky.php @@ -21,6 +21,6 @@ public function __construct() */ public function render(): View|Closure|string { - return view('components.sticky'); + return view('alert::components.sticky'); } } From c9a675966b7dc3f159857b0da69d821248effbab Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Mon, 1 May 2023 00:24:34 +0100 Subject: [PATCH 16/58] update bar component --- resources/views/components/bar.blade.php | 11 +++++++++-- src/AlertStore.php | 2 +- src/View/Components/Bar.php | 11 ++++++++--- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/resources/views/components/bar.blade.php b/resources/views/components/bar.blade.php index 11e33a07..cc48c150 100644 --- a/resources/views/components/bar.blade.php +++ b/resources/views/components/bar.blade.php @@ -1,4 +1,11 @@ +@php + $tag = $attributes->get('tag', $defaultTag); +@endphp \ No newline at end of file diff --git a/src/AlertStore.php b/src/AlertStore.php index aefbe3a7..4635351d 100644 --- a/src/AlertStore.php +++ b/src/AlertStore.php @@ -39,7 +39,7 @@ public function store(Alerter $alerter) : self { $this ->store - ->put($this->getKey(), $alerter); + ->flash($this->getKey(), $alerter); return $this; } diff --git a/src/View/Components/Bar.php b/src/View/Components/Bar.php index 508f0e77..c257b5a9 100644 --- a/src/View/Components/Bar.php +++ b/src/View/Components/Bar.php @@ -5,15 +5,20 @@ use Closure; use Illuminate\Contracts\View\View; use Illuminate\View\Component; +use Digitlimit\Alert\Alert; class Bar extends Component { + public string $defaultTag = 'default'; + + public Alert $alert; + /** * Create a new component instance. */ - public function __construct() + public function __construct(Alert $alert) { - // + $this->alert = $alert; } /** @@ -23,4 +28,4 @@ public function render(): View|Closure|string { return view('alert::components.bar'); } -} +} \ No newline at end of file From 565582474a86cec3d4a4b97eaa2f20d8e81695da Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 2 May 2023 12:45:09 +0100 Subject: [PATCH 17/58] refactor alert classes --- config/alert.php | 45 ++++++++ resources/views/components/bar.blade.php | 26 +++-- src/Alert.php | 134 ++++++++++++++++++----- src/AlertLevel.php | 40 ------- src/AlertServiceProvider.php | 23 ++-- src/AlertStore.php | 53 --------- src/AlertType.php | 45 -------- src/Alerter.php | 80 -------------- src/Concerns/WithClosable.php | 14 --- src/Concerns/WithLevels.php | 14 --- src/Concerns/WithTypes.php | 16 --- src/Enums/Level.php | 6 + src/Enums/Type.php | 12 -- src/Helpers.php | 1 - src/Helpers/SessionKey.php | 15 +++ src/Helpers/TypeHelper.php | 41 +++++++ src/Message.php | 45 -------- src/Message/AbstractMessage.php | 50 +++++++++ src/Message/LevelInterface.php | 8 ++ src/Message/MessageFactory.php | 32 ++++++ src/Message/MessageInterface.php | 18 +++ src/Session.php | 25 +++++ src/SessionInterface.php | 8 ++ src/Traits/EnumFinders.php | 39 +++++++ src/Traits/EnumValues.php | 14 +++ src/Traits/LevelsTrait.php | 34 ------ src/Traits/TypesTrait.php | 41 ------- src/Types/Bar.php | 15 +++ src/Types/Field.php | 15 +++ src/Types/Form.php | 15 +++ src/Types/Modal.php | 15 +++ src/Types/Notify.php | 15 +++ src/Types/Sticky.php | 15 +++ src/View/Components/Bar.php | 8 ++ 34 files changed, 539 insertions(+), 438 deletions(-) create mode 100644 config/alert.php delete mode 100644 src/AlertLevel.php delete mode 100644 src/AlertStore.php delete mode 100644 src/AlertType.php delete mode 100644 src/Alerter.php delete mode 100644 src/Concerns/WithClosable.php delete mode 100644 src/Concerns/WithLevels.php delete mode 100644 src/Concerns/WithTypes.php delete mode 100644 src/Enums/Type.php create mode 100644 src/Helpers/SessionKey.php create mode 100644 src/Helpers/TypeHelper.php delete mode 100644 src/Message.php create mode 100644 src/Message/AbstractMessage.php create mode 100644 src/Message/LevelInterface.php create mode 100644 src/Message/MessageFactory.php create mode 100644 src/Message/MessageInterface.php create mode 100644 src/Session.php create mode 100644 src/SessionInterface.php create mode 100644 src/Traits/EnumFinders.php create mode 100644 src/Traits/EnumValues.php delete mode 100644 src/Traits/LevelsTrait.php delete mode 100644 src/Traits/TypesTrait.php create mode 100644 src/Types/Bar.php create mode 100644 src/Types/Field.php create mode 100644 src/Types/Form.php create mode 100644 src/Types/Modal.php create mode 100644 src/Types/Notify.php create mode 100644 src/Types/Sticky.php diff --git a/config/alert.php b/config/alert.php new file mode 100644 index 00000000..39b71863 --- /dev/null +++ b/config/alert.php @@ -0,0 +1,45 @@ + [ + 'alert-bar' => [ + 'alert' => \Digitlimit\Alert\Types\Bar::class, + 'component' => \Digitlimit\Alert\View\Components\Bar::class + ], + + 'alert-field' => [ + 'alert' => \Digitlimit\Alert\Types\Field::class, + 'component' => \Digitlimit\Alert\View\Components\Field::class + ], + + 'alert-form' => [ + 'alert' => \Digitlimit\Alert\Types\Form::class, + 'component' => \Digitlimit\Alert\View\Components\Form::class + ], + + 'alert-modal' => [ + 'alert' => \Digitlimit\Alert\Types\Modal::class, + 'component' => \Digitlimit\Alert\View\Components\Modal::class + ], + + 'alert-notify' => [ + 'alert' => \Digitlimit\Alert\Types\Notify::class, + 'component' => \Digitlimit\Alert\View\Components\Notify::class + ], + + 'alert-sticky' => [ + 'alert' => \Digitlimit\Alert\Types\Sticky::class, + 'component' => \Digitlimit\Alert\View\Components\Sticky::class + ] + ] +]; diff --git a/resources/views/components/bar.blade.php b/resources/views/components/bar.blade.php index cc48c150..691165d3 100644 --- a/resources/views/components/bar.blade.php +++ b/resources/views/components/bar.blade.php @@ -1,11 +1,19 @@ @php - $tag = $attributes->get('tag', $defaultTag); + $tag = $attributes->get('tag', $defaultTag); + $alerter = $alert->tagged('contact'); @endphp - \ No newline at end of file +@if($alerter) + @php + $level = $alerter->getLevel()->name()->value; + $title = $alerter->getMessage()->getTitle(); + $content = $alerter->getMessage()->getContent(); + @endphp +
merge(['class' => 'alert alert-'.$level]) }} role="alert"> + @if ($slot->isNotEmpty()) + {{ $slot }} + @else + @if($title){{ $title }}@endif {{ $content }} + + @endif +
+@endif \ No newline at end of file diff --git a/src/Alert.php b/src/Alert.php index 5e27dace..cb33706e 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -2,56 +2,136 @@ namespace Digitlimit\Alert; -use Digitlimit\Alert\AlertLevel; -use Digitlimit\Alert\AlertType; +use Digitlimit\Alert\Enums\Level; +use Digitlimit\Alert\Message\MessageInterface; +use Digitlimit\Alert\Message\MessageFactory; +use Digitlimit\Alert\Helpers\SessionKey; +use Digitlimit\Alert\Helpers\TypeHelper; +use Exception; -use Digitlimit\Alert\Concerns\WithLevels; -use Digitlimit\Alert\Concerns\WithTypes; +class Alert +{ + protected Level $level; -use Digitlimit\Alert\Traits\LevelsTrait; -use Digitlimit\Alert\Traits\TypesTrait; + protected string $title = ''; + protected string $message = ''; + protected string $tag = ''; + protected string $type = 'bar'; + protected string $defaultTag = 'default'; + + public function __construct( + protected Session $session + ){ + $this->level = Level::INFO; + } -class Alert implements WithLevels, WithTypes -{ - use LevelsTrait, TypesTrait; + public function title(string $title) : self + { + + $this->title = $title; + return $this; + } - protected Alerter $alerter; + public function message(string $message) : self + { + $this->message = $message; + return $this; + } - protected AlertLevel $level; + public function tag(string $tag) : self + { + $this->tag = $tag; + return $this; + } - protected AlertType $type; + public function success() : self + { + $this->level = Level::SUCCESS; + return $this; + } - public function __construct(Alerter $alerter) + public function info() : self { - $this->level = new AlertLevel(); - $this->type = new AlertType(); - $this->alerter = $alerter; + $this->level = Level::INFO; + return $this; + } - $this->alerter->setLevel($this->level); - $this->alerter->setType($this->type); + public function error() : self + { + $this->level = Level::ERROR; + return $this; } - public function tag(string $tag) : self + public function warning() : self { - $this->alerter->setTag($tag); + $this->level = Level::WARNING; return $this; } - public function message(string $content, string $title='') : self + public function type(string $type) : self { - $message = new Message($content, $title); - $this->alerter->setMessage($message); + if(!TypeHelper::exists($type)) { + throw new Exception("Invalid alert type '$type'. Check the alert config"); + } + + $this->type = $type; + return $this; } - public function view(string $name, array $data=[]) : self + public function level(string $name) : self { - //@todo + $level = Level::fromValue($name); + + if(!$level) { + throw new Exception("Invalid level name: $name"); + } + + $this->level = $level; + return $this; } - public function alerter() : Alerter + public function default(string $type) : MessageInterface|null + { + return self::tagged($type, $this->defaultTag); + } + + public function tagged(string $type, string $tag='') : MessageInterface|null { - return $this->alerter; + $taggedKey = $tag ?? $this->defaultTag; + $sessionKey = SessionKey::key($type, $taggedKey); + + return null; + } + + public function flash( + string $message='', + string $level='', + string $type='' + ) : ?MessageInterface { + + if($message) { + self::message($message); + } + + if(empty($this->message)) { + throw new Exception("Message cannot be empty string"); + } + + if($level) { + self::level($level); + } + + if($type) { + self::type($type); + } + + return MessageFactory::make( + $this->message, + $this->level, + $this->type, + $this->title + ); } } diff --git a/src/AlertLevel.php b/src/AlertLevel.php deleted file mode 100644 index 125b8919..00000000 --- a/src/AlertLevel.php +++ /dev/null @@ -1,40 +0,0 @@ -success(); - } - - public function type() : Level - { - return $this->level; - } - - public function success() : void - { - $this->level = Level::SUCCESS; - } - - public function info() : void - { - $this->level = Level::INFO; - } - - public function error() : void - { - $this->level = Level::ERROR; - } - - public function warning() : void - { - $this->level = Level::WARNING; - } -} \ No newline at end of file diff --git a/src/AlertServiceProvider.php b/src/AlertServiceProvider.php index f3619853..9aad2a46 100644 --- a/src/AlertServiceProvider.php +++ b/src/AlertServiceProvider.php @@ -23,10 +23,15 @@ public function boot(): void */ public function register(): void { + $this->app->bind(SessionInterface::class, Session::class); + $this->app->singleton('alert', function ($app) { - $store = new AlertStore($app['session.store']); - return new Alert(new Alerter($store)); + return $app->make(Alert::class); }); + + $this->mergeConfigFrom( + __DIR__.'/../config/alert.php', 'alert' + ); } /** @@ -45,6 +50,10 @@ protected function bootForConsole(): void $this->publishes([ __DIR__.'/../resources/views' => base_path('resources/views/vendor/digitlimit'), ], 'alert.views'); + + $this->publishes([ + __DIR__.'/../config/alert.php' => config_path('alert.php'), + ], 'alert.config'); } /** @@ -54,10 +63,10 @@ protected function registerComponents() : void { Blade::componentNamespace('Digitlimit\\Alert\View\\Components', 'alert'); - Blade::component('alert-bar', Components\Bar::class); - Blade::component('alert-field', Components\Field::class); - Blade::component('alert-modal', Components\Modal::class); - Blade::component('alert-notify', Components\Notify::class); - Blade::component('alert-sticky', Components\Sticky::class); + $types = config('alert.types'); + + foreach($types as $name => $type) { + Blade::component($name, $type['component']); + } } } \ No newline at end of file diff --git a/src/AlertStore.php b/src/AlertStore.php deleted file mode 100644 index 4635351d..00000000 --- a/src/AlertStore.php +++ /dev/null @@ -1,53 +0,0 @@ -store = $store; - } - - public function getKey() : string - { - if($this->tag) { - return "$this->key.$this->tag"; - } - - return $this->key; - } - - public function tag(string $tag) : self - { - if($tag) { - $this->tag = $tag; - } - - return $this; - } - - public function store(Alerter $alerter) : self - { - $this - ->store - ->flash($this->getKey(), $alerter); - - return $this; - } - - public function fetch() : Alerter - { - return $this - ->store - ->get($this->getKey()) ?? new Alerter($this); - } -} diff --git a/src/AlertType.php b/src/AlertType.php deleted file mode 100644 index 5333046b..00000000 --- a/src/AlertType.php +++ /dev/null @@ -1,45 +0,0 @@ -bar(); - } - - public function name() : Type - { - return $this->type; - } - - public function bar() : void - { - $this->type = Type::BAR; - } - - public function field() : void - { - $this->type = Type::FIELD; - } - - public function modal() : void - { - $this->type = Type::MODAL; - } - - public function notify() : void - { - $this->type = Type::NOTIFY; - } - - public function sticky() : void - { - $this->type = Type::STICKY; - } -} \ No newline at end of file diff --git a/src/Alerter.php b/src/Alerter.php deleted file mode 100644 index 9f5c678b..00000000 --- a/src/Alerter.php +++ /dev/null @@ -1,80 +0,0 @@ -store = $store; - } - - protected function restore() : void - { - if($this->tag){ - $this->store->tag($this->tag); - } - - $this->store->store($this); - } - - public function setTag(string $tag) : self - { - $this->tag = Str::slug($tag); - $this->restore(); - return $this; - } - - public function setMessage(Message $message) : self - { - $this->message = $message; - $this->restore(); - return $this; - } - - public function setLevel(AlertLevel $level) : self - { - $this->level = $level; - $this->restore(); - return $this; - } - - public function setType(AlertType $type) : self - { - $this->type = $type; - $this->restore(); - return $this; - } - - public function getMessage() : Message - { - return $this->message; - } - - public function getLevel() : AlertLevel - { - return $this->level; - } - - public function getType() : AlertType - { - return $this->type; - } - - public function getKey() : string - { - return $this->store->getKey(); - } -} diff --git a/src/Concerns/WithClosable.php b/src/Concerns/WithClosable.php deleted file mode 100644 index 461c752e..00000000 --- a/src/Concerns/WithClosable.php +++ /dev/null @@ -1,14 +0,0 @@ -title = $title; - - return $this; - } - - public function setContent(string $content) : self - { - $this->content = $content; - - return $this; - } - - public function getTitle() : string - { - return $this->title; - } - - public function getContent() : string - { - return $this->content; - } - - public function hasTitle() : bool - { - return !empty($this->title); - } - - public function hasContent() : bool - { - return !empty($this->content); - } -} \ No newline at end of file diff --git a/src/Message/AbstractMessage.php b/src/Message/AbstractMessage.php new file mode 100644 index 00000000..6ba11746 --- /dev/null +++ b/src/Message/AbstractMessage.php @@ -0,0 +1,50 @@ +level = $level; + return $this; + } + + public function setMessage(string $message) : self + { + $this->message = $message; + return $this; + } + + public function setTitle(string $title) : self + { + $this->title = $title; + return $this; + } + + abstract public function name() : string; + + public function level() : string + { + return $this->level->value; + } + + public function message() : string + { + return $this->message; + } + + public function title() : string + { + return $this->title; + } +} \ No newline at end of file diff --git a/src/Message/LevelInterface.php b/src/Message/LevelInterface.php new file mode 100644 index 00000000..dc55aa2d --- /dev/null +++ b/src/Message/LevelInterface.php @@ -0,0 +1,8 @@ +setLevel($level) + ->setTitle($title); + + return new $class($message); + } +} \ No newline at end of file diff --git a/src/Message/MessageInterface.php b/src/Message/MessageInterface.php new file mode 100644 index 00000000..03d41b24 --- /dev/null +++ b/src/Message/MessageInterface.php @@ -0,0 +1,18 @@ +session = $session; + } + + public function flash(string $key, mixed $value) : void + { + $this->session->flash($key, $value); + } + + public function get(string $key, mixed $default=null) : mixed + { + return $this->session->get($key, $default); + } +} diff --git a/src/SessionInterface.php b/src/SessionInterface.php new file mode 100644 index 00000000..9fb09c89 --- /dev/null +++ b/src/SessionInterface.php @@ -0,0 +1,8 @@ +value == $value) return $case; + } + } + + public static function fromName($name) + { + foreach(static::cases() as $case){ + if(strtolower($case->name) == strtolower($name)) return $case; + } + } + + public static function fromEnumName($enum) + { + foreach(static::cases() as $case){ + if($case->name == $enum->name) return $case; + } + } + + public static function fromEnumValue($enum) + { + foreach(static::cases() as $case){ + if($case->value == $enum->value) return $case; + } + } + + public static function valueIs($value, $case) + { + return self::fromValue($value) == $case; + } +} \ No newline at end of file diff --git a/src/Traits/EnumValues.php b/src/Traits/EnumValues.php new file mode 100644 index 00000000..7eac6df5 --- /dev/null +++ b/src/Traits/EnumValues.php @@ -0,0 +1,14 @@ +level->success(); - $this->alerter->setLevel($this->level); - return $this; - } - - public function info() : self - { - $this->level->info(); - $this->alerter->setLevel($this->level); - return $this; - } - - public function error() : self - { - $this->level->error(); - $this->alerter->setLevel($this->level); - return $this; - } - - public function warning() : self - { - $this->level->warning(); - $this->alerter->setLevel($this->level); - return $this; - } -} diff --git a/src/Traits/TypesTrait.php b/src/Traits/TypesTrait.php deleted file mode 100644 index 2ab80d18..00000000 --- a/src/Traits/TypesTrait.php +++ /dev/null @@ -1,41 +0,0 @@ -type->bar(); - $this->alerter->setType($this->type); - return $this; - } - - public function field() : self - { - $this->type->field(); - $this->alerter->setType($this->type); - return $this; - } - - public function modal() : self - { - $this->type->modal(); - $this->alerter->setType($this->type); - return $this; - } - - public function notify() : self - { - $this->type->notify(); - $this->alerter->setType($this->type); - return $this; - } - - public function sticky() : self - { - $this->type->sticky(); - $this->alerter->setType($this->type); - return $this; - } -} diff --git a/src/Types/Bar.php b/src/Types/Bar.php new file mode 100644 index 00000000..87547cb9 --- /dev/null +++ b/src/Types/Bar.php @@ -0,0 +1,15 @@ +alert = $alert; } + /** + * Whether the component should be rendered + */ + // public function shouldRender(): bool + // { + + // } + /** * Get the view / contents that represent the component. */ From f87add7e53fe88c75901aa6fe02feca33423938b Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Tue, 2 May 2023 19:48:06 +0100 Subject: [PATCH 18/58] fix session flash --- resources/views/components/bar.blade.php | 15 +++------ resources/views/components/form.blade.php | 14 ++++++++ src/Alert.php | 36 +++++++++++++++++---- src/Enums/Level.php | 13 +++++--- src/Message/MessageFactory.php | 2 +- src/SessionInterface.php | 2 ++ src/View/Components/Form.php | 39 +++++++++++++++++++++++ 7 files changed, 99 insertions(+), 22 deletions(-) create mode 100644 resources/views/components/form.blade.php create mode 100644 src/View/Components/Form.php diff --git a/resources/views/components/bar.blade.php b/resources/views/components/bar.blade.php index 691165d3..6844f32f 100644 --- a/resources/views/components/bar.blade.php +++ b/resources/views/components/bar.blade.php @@ -1,18 +1,13 @@ @php - $tag = $attributes->get('tag', $defaultTag); - $alerter = $alert->tagged('contact'); + $tag = $attributes->get('tag', $defaultTag); + $bar = $alert->tagged('bar', $tag); @endphp -@if($alerter) - @php - $level = $alerter->getLevel()->name()->value; - $title = $alerter->getMessage()->getTitle(); - $content = $alerter->getMessage()->getContent(); - @endphp -
merge(['class' => 'alert alert-'.$level]) }} role="alert"> +@if($bar) +
merge(['class' => 'alert alert-'.$bar->level()]) }} role="alert"> @if ($slot->isNotEmpty()) {{ $slot }} @else - @if($title){{ $title }}@endif {{ $content }} + @if($bar->title()){{ $bar->title() }}@endif {{ $bar->message() }} @endif
diff --git a/resources/views/components/form.blade.php b/resources/views/components/form.blade.php new file mode 100644 index 00000000..f61a80a5 --- /dev/null +++ b/resources/views/components/form.blade.php @@ -0,0 +1,14 @@ +@php + $tag = $attributes->get('tag', $defaultTag); + $form = $alert->tagged('form', $tag); +@endphp +@if($form) +
merge(['class' => 'alert alert-'.$form->level()]) }} role="alert"> + @if ($slot->isNotEmpty()) + {{ $slot }} + @else + @if($form->title()){{ $form->title() }}@endif {{ $form->message() }} + + @endif +
+@endif \ No newline at end of file diff --git a/src/Alert.php b/src/Alert.php index cb33706e..3b27aed5 100644 --- a/src/Alert.php +++ b/src/Alert.php @@ -27,7 +27,6 @@ public function __construct( public function title(string $title) : self { - $this->title = $title; return $this; } @@ -58,7 +57,7 @@ public function info() : self public function error() : self { - $this->level = Level::ERROR; + $this->level = Level::DANGER; return $this; } @@ -92,17 +91,34 @@ public function level(string $name) : self return $this; } + public function fresh() : self + { + return new self($this->session); + } + public function default(string $type) : MessageInterface|null { return self::tagged($type, $this->defaultTag); } - public function tagged(string $type, string $tag='') : MessageInterface|null + public function tagged(string $type, string $tag) : MessageInterface|null + { + if(!TypeHelper::exists($type)) { + throw new Exception("Invalid alert type '$type'. Check the alert config"); + } + + $sessionKey = SessionKey::key($type, $tag); + + return $this->session->get($sessionKey); + } + + public function getTag() : string { - $taggedKey = $tag ?? $this->defaultTag; - $sessionKey = SessionKey::key($type, $taggedKey); + if($this->tag) { + return $this->tag; + } - return null; + return $this->defaultTag; } public function flash( @@ -127,11 +143,17 @@ public function flash( self::type($type); } - return MessageFactory::make( + $alert = MessageFactory::make( $this->message, $this->level, $this->type, $this->title ); + + // flash in session + $sessionKey = SessionKey::key($this->type, $this->getTag()); + $this->session->flash($sessionKey, $alert); + + return $alert; } } diff --git a/src/Enums/Level.php b/src/Enums/Level.php index a3f901b9..7d146206 100644 --- a/src/Enums/Level.php +++ b/src/Enums/Level.php @@ -10,8 +10,13 @@ enum Level: string use EnumValues; use EnumFinders; - case SUCCESS = 'success'; - case INFO = 'info'; - case ERROR = 'error'; - case WARNING = 'warning'; + case PRIMARY = 'primary'; + case SECONDARY = 'secondary'; + case SUCCESS = 'success'; + case INFO = 'info'; + case ERROR = 'error'; + case WARNING = 'warning'; + case DANGER = 'danger'; + case LIGHT = 'light'; + case DARK = 'dark'; } \ No newline at end of file diff --git a/src/Message/MessageFactory.php b/src/Message/MessageFactory.php index 8939b0cf..410db16a 100644 --- a/src/Message/MessageFactory.php +++ b/src/Message/MessageFactory.php @@ -27,6 +27,6 @@ public static function make( ->setLevel($level) ->setTitle($title); - return new $class($message); + return $alert; } } \ No newline at end of file diff --git a/src/SessionInterface.php b/src/SessionInterface.php index 9fb09c89..99757bf8 100644 --- a/src/SessionInterface.php +++ b/src/SessionInterface.php @@ -5,4 +5,6 @@ interface SessionInterface { public function flash(string $name, mixed $value) : void; + + public function get(string $key, mixed $default=null) : mixed; } diff --git a/src/View/Components/Form.php b/src/View/Components/Form.php new file mode 100644 index 00000000..8e920314 --- /dev/null +++ b/src/View/Components/Form.php @@ -0,0 +1,39 @@ +alert = $alert; + } + + /** + * Whether the component should be rendered + */ + // public function shouldRender(): bool + // { + + // } + + /** + * Get the view / contents that represent the component. + */ + public function render(): View|Closure|string + { + return view('alert::components.form'); + } +} \ No newline at end of file From 36f15c10cd37a978f5fea8086a36cac57a14a52c Mon Sep 17 00:00:00 2001 From: Emeka Mbah Date: Sat, 6 May 2023 22:20:37 +0100 Subject: [PATCH 19/58] refactor alert classes --- resources/views/components/bar.blade.php | 4 +- resources/views/components/field.blade.php | 16 +- resources/views/components/form.blade.php | 4 +- resources/views/components/modal.blade.php | 90 +++++++++-- resources/views/components/notify.blade.php | 23 +-- resources/views/components/sticky.blade.php | 18 ++- src/Alert copy.php.txt | 167 ++++++++++++++++++++ src/Alert.php | 141 ++++------------- src/Enums/Level.php | 22 --- src/Message/AbstractMessage.php | 42 ++--- src/Message/LevelInterface.php | 8 - src/Message/MessageFactory.php | 23 +-- src/Message/MessageInterface.php | 10 +- src/Traits/EnumFinders.php | 39 ----- src/Traits/EnumValues.php | 14 -- src/Traits/Levelable.php | 54 +++++++ src/Types/Bar.php | 9 +- src/Types/Field.php | 19 ++- src/Types/Form.php | 9 +- src/Types/Modal.php | 25 ++- src/Types/Notify.php | 9 +- src/Types/Sticky.php | 9 +- src/Values/Button.php | 29 ++++ src/View/Components/Field.php | 9 +- src/View/Components/Modal.php | 66 +++++++- src/View/Components/Notify.php | 9 +- src/View/Components/Sticky.php | 9 +- 27 files changed, 577 insertions(+), 300 deletions(-) create mode 100644 src/Alert copy.php.txt delete mode 100644 src/Enums/Level.php delete mode 100644 src/Message/LevelInterface.php delete mode 100644 src/Traits/EnumFinders.php delete mode 100644 src/Traits/EnumValues.php create mode 100644 src/Traits/Levelable.php create mode 100644 src/Values/Button.php diff --git a/resources/views/components/bar.blade.php b/resources/views/components/bar.blade.php index 6844f32f..bc6cb4bb 100644 --- a/resources/views/components/bar.blade.php +++ b/resources/views/components/bar.blade.php @@ -3,11 +3,11 @@ $bar = $alert->tagged('bar', $tag); @endphp @if($bar) -
merge(['class' => 'alert alert-'.$bar->level()]) }} role="alert"> +
merge(['class' => 'alert alert-'.$bar->level]) }} role="alert"> @if ($slot->isNotEmpty()) {{ $slot }} @else - @if($bar->title()){{ $bar->title() }}@endif {{ $bar->message() }} + @if($bar->title){{ $bar->title }}@endif {{ $bar->message }} @endif
diff --git a/resources/views/components/field.blade.php b/resources/views/components/field.blade.php index 11e33a07..95ea77e3 100644 --- a/resources/views/components/field.blade.php +++ b/resources/views/components/field.blade.php @@ -1,4 +1,12 @@ - \ No newline at end of file +@php + $tag = $attributes->get('tag', $defaultTag); + $field = $alert->tagged('field', $tag); +@endphp + +@if($slot->isNotEmpty()) + {{ $slot }} +@elseif($field) +
merge(['class' => 'form-text text-'.$field->level]) }}> + {{ $field->message }} +
+@endif \ No newline at end of file diff --git a/resources/views/components/form.blade.php b/resources/views/components/form.blade.php index f61a80a5..66a992e2 100644 --- a/resources/views/components/form.blade.php +++ b/resources/views/components/form.blade.php @@ -3,11 +3,11 @@ $form = $alert->tagged('form', $tag); @endphp @if($form) -
merge(['class' => 'alert alert-'.$form->level()]) }} role="alert"> +
merge(['class' => 'alert alert-'.$form->level]) }} role="alert"> @if ($slot->isNotEmpty()) {{ $slot }} @else - @if($form->title()){{ $form->title() }}@endif {{ $form->message() }} + @if($form->title){{ $form->title }}@endif {{ $form->message }} @endif
diff --git a/resources/views/components/modal.blade.php b/resources/views/components/modal.blade.php index 267f4d90..9ae9f1d5 100644 --- a/resources/views/components/modal.blade.php +++ b/resources/views/components/modal.blade.php @@ -1,17 +1,77 @@ -