Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
waadmawlood committed Jun 5, 2023
0 parents commit 79013d3
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -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.
194 changes: 194 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Waad\Observer\HasObserver;

class Work extends Model
{
use HasObserver;


......
}
```

&nbsp;

- if use observer different name class used `$observer` property

```php
<?php

namespace App\Models;

use App\Observers\MyWorkObserver;
use Illuminate\Database\Eloquent\Model;
use Waad\Observer\HasObserver;

class Work extends Model
{
use HasObserver;

public static $observer = MyWorkObserver::class;
}
```

&nbsp;

- if use multi observer different names classes used `$observer` property

```php
<?php

namespace App\Models;

use App\Observers\MyWorkObserver;
use App\Observers\OurWorkObserver;
use Illuminate\Database\Eloquent\Model;
use Waad\Observer\HasObserver;

class Work extends Model
{
use HasObserver;

public static $observer = [MyWorkObserver::class, OurWorkObserver::class];
}
```

&nbsp;


## 🍔 Example Obsever

- to create observer use this command
```php
php artisan make:observer YourModelObserver --model=YourModel
```

⚠️ Such a file will be generated, If there is a shortage of functions, take copies of this example
```php
<?php

namespace App\Observers;

use App\Models\Work;

class WorkObserver
{

public function creating(Work $work)
{
// This function is called when a new model instance is being created.
$work->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');
}
}
```


&nbsp;

## 🚀 About Me
I'm a Back End developer...

- Author :[ Waad Mawlood](https://waad.netlify.app/)

- Email : [email protected]

&nbsp;

## ⚖️ License

[MIT](https://choosealicense.com/licenses/mit/)
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"homepage": "https://waad.netlify.app",
"role": "Developer"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"config": {
"extra": {
"sort-packages": true
}
}
}
47 changes: 47 additions & 0 deletions src/HasObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Waad\Observer;

trait HasObserver
{

/**
* Event , This function will boot the observer for the current model.
*/
public static function bootHasObserver()
{
// Check if the 'observer' property exists in the current class
if (property_exists(static::class, 'observer')) {

// Check if 'observer' property is an array, otherwise convert it into an array
$observers = is_array(static::$observer) ? static::$observer : [static::$observer];

// Create an empty array to store the valid observers
$observersTarget = array();

// Iterate over the observers and check if they are not empty and the class exists
foreach ($observers as $observer) {

if (!empty($observer) && class_exists($observer)) {

$observersTarget[] = $observer; // Add the valid observer to the target array
}
}
static::observe($observersTarget); // Assign the observer to the model

} else {

// Split the fully qualified class name into an array based on the backslash separator
$pathModel = explode("\\", static::class);

// Create the observer class name by combining the namespace and the observer naming convention
$observerClass = sprintf('%s\Observers\%sObserver', reset($pathModel), end($pathModel));

// Check if the observer class exists
if (class_exists($observerClass)) {

static::observe($observerClass); // Assign the observer to the model
}
}
}
}

0 comments on commit 79013d3

Please sign in to comment.