Files
librenms-librenms/app/Observers/ModuleModelObserver.php
Zane C. Bowers-Hadley 2618a99be5 Application Soft Delete (#15270)
* add the softdeletes migrations for applications

* add working migration file

* add deleted_at to db schema.yaml for applications

* update includes/html/forms/application-update.inc.php to work with softdeletes

* update includes/html/pages/device/edit/apps.inc.php for softdelete

* update includes/discovery/applications.inc.php to work with softdelete

* minor updates to application-update.inc.php for disabling

* style cleanup

* set discovered when running discovery

* update application tests to include deleted_at

* add deleted_at to a missed test

* a few more tweaks for opensips

* add a missing deleted_at for linux_suricata_extract-v1

* fix fillable for Application model

* massive cleanup of the application update widget thingy

* improve the code for discovery and using Laravel

* add a missing line to app/Models/Application

* add a missing include to app/Models/Application.php

* record includes for Application model

* remove apps from the applications table when a device is deleted

* revert to using upcert and where for discovery to fix CI

* make discovered fillable and set it when running discovery... convert back to firstOrNew

* clean up application discovery a bit and use observer

* style fix

* spelling fix... disablaed -> disabled

* rever removal to just use where

* cleanup app removal on delete

* add restored to ModuleModelObserver

* delete -> forcedelete fix

* apply the suggested changes

* use murrants other suggestion

* style fix
2023-09-06 16:34:39 -05:00

91 lines
2.3 KiB
PHP

<?php
/*
* ModuleModelObserver.php
*
* Displays +,-,U,. while running discovery and adding,deleting,updating, and doing nothing.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
* @copyright 2020 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace App\Observers;
use Illuminate\Database\Eloquent\Model as Eloquent;
class ModuleModelObserver
{
/**
* Install observers to output +, -, U for models being created, deleted, and updated
*
* @param string|\Illuminate\Database\Eloquent\Model $model The model name including namespace
*/
public static function observe($model)
{
static $observed_models = []; // keep track of observed models so we don't duplicate output
$class = ltrim($model, '\\');
if (! in_array($class, $observed_models)) {
$model::observe(new static());
$observed_models[] = $class;
}
}
/**
* @param Eloquent $model
*/
public function saving($model)
{
if (! $model->isDirty()) {
echo '.';
}
}
/**
* @param Eloquent $model
*/
public function updated($model): void
{
d_echo('Updated data:', 'U');
d_echo($model->getDirty());
}
/**
* @param Eloquent $model
*/
public function restored($model)
{
d_echo('Restored data:', 'R');
d_echo($model->getDirty());
}
/**
* @param Eloquent $model
*/
public function created($model): void
{
echo '+';
}
/**
* @param Eloquent $model
*/
public function deleted($model): void
{
echo '-';
}
}