From 79013d3712d0c6003f798fb423277a484994ad0c Mon Sep 17 00:00:00 2001 From: Waad Mawlood Date: Mon, 5 Jun 2023 22:41:28 +0300 Subject: [PATCH] initial commit --- LICENSE.txt | 21 +++++ README.md | 194 ++++++++++++++++++++++++++++++++++++++++++++ composer.json | 40 +++++++++ src/HasObserver.php | 47 +++++++++++ 4 files changed, 302 insertions(+) create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/HasObserver.php diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..9379f9b --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 waadmawlood + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a589a27 --- /dev/null +++ b/README.md @@ -0,0 +1,194 @@ + +# 🎀 Laravel Dynamic Observer + +Call observer of the model from direct model by trait `HasObserver` without requiring in any provider, support multi observers. + +## 🤞Installation +Require this package with composer using the following command: + +```bash +waad/laravel-dynamic-observer +``` + + +  + +## 💯 Usage + +To properly use this package, follow the steps that meet your needs + +- will connect dynamic with observer named `WorksObserver` in `App\Observers` namespace + +```php +title = $work->title . "....."; + } + + public function created(Work $work) + { + // This function is called after a new model instance is successfully + // created and saved to the database. + + $work->users()->attach([1,2]); + } + + public function updating(Work $work) + { + // This function is called when an existing model instance is being updated. + + $work->status_color = $work->status ? 'green' : 'red'; + } + + public function updated(Work $work) + { + // This function is called after an existing model instance is successfully + // updated and saved to the database. + + $work->users()->sync([1,3]); + } + + public function saving(Work $work) + { + // This function is called when a model instance is being saved + // (either created or updated). + + $work->title = $work->title . "....."; + } + + public function saved(Work $work) + { + // This function is called after a model instance is successfully saved + // (either created or updated). + + $work->status_string = 'working'; + $work->save(); + } + + public function deleting(Work $work) + { + // This function is called when an existing model instance is being deleted. + + $work->users()->detach(); + } + + public function deleted(Work $work) + { + // This function is called after an existing model instance is successfully deleted + } + + public function restoring(Work $work) + { + // This function is called when a "soft-deleted" model instance is being restored. + } + + public function restored(Work $work) + { + // This function is called after a "soft-deleted" model instance is successfully restored. + } + + public function retrieved(Work $work) + { + // This function is called after a model instance is retrieved from the database. + + $work->increment('views'); + } +} +``` + + +  + +## 🚀 About Me +I'm a Back End developer... + +- Author :[ Waad Mawlood](https://waad.netlify.app/) + +- Email : waad_mawlood@outlook.com + +  + +## ⚖️ License + +[MIT](https://choosealicense.com/licenses/mit/) diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0b014ce --- /dev/null +++ b/composer.json @@ -0,0 +1,40 @@ +{ + "name": "waad/laravel-dynamic-observer", + "description": "Call observer of the model without requiring in any provider, support multi observers", + "type": "utility", + "keywords": [ + "Waad Mawlood", + "observer", + "laravel", + "laravel-observer", + "laravel-php", + "model", + "model observer", + "package", + "utility" + ], + "require": { + "php": ">=7.4" + }, + "autoload": { + "psr-4": { + "Waad\\Observer\\": "src/" + } + }, + "license": "MIT", + "authors": [ + { + "name": "Waad Mawlood", + "email": "waad_mawlood@outlook.com", + "homepage": "https://waad.netlify.app", + "role": "Developer" + } + ], + "minimum-stability": "dev", + "prefer-stable": true, + "config": { + "extra": { + "sort-packages": true + } + } +} diff --git a/src/HasObserver.php b/src/HasObserver.php new file mode 100644 index 0000000..b4355b3 --- /dev/null +++ b/src/HasObserver.php @@ -0,0 +1,47 @@ +