1<?php
2/*
3 * ModuleModelObserver.php
4 *
5 * Displays +,-,U,. while running discovery and adding,deleting,updating, and doing nothing.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 *
20 * @link       https://www.librenms.org
21 * @copyright  2020 Tony Murray
22 * @author     Tony Murray <murraytony@gmail.com>
23 */
24
25namespace App\Observers;
26
27use Illuminate\Database\Eloquent\Model as Eloquent;
28
29class ModuleModelObserver
30{
31    /**
32     * Install observers to output +, -, U for models being created, deleted, and updated
33     *
34     * @param string|\Illuminate\Database\Eloquent\Model $model The model name including namespace
35     */
36    public static function observe($model)
37    {
38        static $observed_models = []; // keep track of observed models so we don't duplicate output
39        $class = ltrim($model, '\\');
40
41        if (! in_array($class, $observed_models)) {
42            $model::observe(new static());
43            $observed_models[] = $class;
44        }
45    }
46
47    /**
48     * @param Eloquent $model
49     */
50    public function saving($model)
51    {
52        if (! $model->isDirty()) {
53            echo '.';
54        }
55    }
56
57    /**
58     * @param Eloquent $model
59     */
60    public function updated($model)
61    {
62        d_echo('Updated data:', 'U');
63        d_echo($model->getDirty());
64    }
65
66    /**
67     * @param Eloquent $model
68     */
69    public function created($model)
70    {
71        echo '+';
72    }
73
74    /**
75     * @param Eloquent $model
76     */
77    public function deleted($model)
78    {
79        echo '-';
80    }
81}
82