Skip to content

A free open source IT asset/license management system

Use reflection to get models with HasCalendarEvents

sniped2cd0ae1f1e9
diff --git a/app/Models/CalendarEvent.php b/app/Models/CalendarEvent.php
new file mode 100644
index 0000000000..d35beed795
--- /dev/null
+++ b/app/Models/CalendarEvent.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace App\Models;
+
+use App\Models\Traits\HasCalendarEvents;
+use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Relations\MorphTo;
+use Illuminate\Database\Eloquent\SoftDeletes;
+
+/**
+ * Row in the calendar_events index table. One instance per
+ * (source_type, source_id, source_field) triple - a single source
+ * model can publish multiple rows if its calendarEventDefinitions()
+ * declares multiple fields.
+ *
+ * Deliberately thin. This model exists for the observer's upsert
+ * path and for the read-path range/type filter query; consumers that
+ * want to render events read the source model live via ->source
+ * rather than pulling values off this row.
+ */
+class CalendarEvent extends Model
+{
+ use SoftDeletes;
+
+ protected $fillable = [
+ 'source_type',
+ 'source_id',
+ 'source_field',
+ 'event_type',
+ 'start',
+ 'end',
+ ];
+
+ protected $casts = [
+ 'start' => 'datetime',
+ 'end' => 'datetime',
+ ];
+
+ /**
+ * Polymorphic parent. Loaded eagerly (batched, per-source-type)
+ * by the calendar events endpoint so a single fetch resolves
+ * names / urls / colors live from the source models rather than
+ * from any denormalized copy on this table.
+ */
+ public function source(): MorphTo
+ {
+ return $this->morphTo();
+ }
+
+ /**
+ * Every eligible App\Models\* class that uses the HasCalendarEvents
+ * trait. Consolidated here (instead of hardcoded per call-site) so
+ * adding a new source model is a one-line change - just adopt the
+ * trait and this discovery picks it up automatically.
+ *
+ * Callers today:
+ * - Api\CalendarEventsController::index base permission gate
+ * (viewer must be able to view at least one source type)
+ * - Console\Commands\ReconcileCalendarEvents
+ * - The two backfill migrations
+ *
+ * Discovery walks app/Models/*.php via glob, filters to
+ * non-abstract Model subclasses that use the trait. Cached at the
+ * static-property level so the reflection cost is amortized over
+ * a full request cycle (glob is cheap but not free, and the count
+ * queries in the controller run alongside other work anyway).
+ *
+ * @return array<int, class-string>
+ */
+ public static function sourceModels(): array
+ {
+ static $cache = null;
+ if ($cache !== null) {
+ return $cache;
+ }
+
+ $sources = [];
+ foreach (glob(app_path('Models').'/*.php') as $file) {
+ $class = 'App\\Models\\'.pathinfo($file, PATHINFO_FILENAME);
+ if (! class_exists($class)) {
+ continue;
+ }
+
+ $reflection = new \ReflectionClass($class);
+ if ($reflection->isAbstract() || $reflection->isInterface() || $reflection->isTrait()) {
+ continue;
+ }
+ if (! is_subclass_of($class, Model::class)) {
+ continue;
+ }
+ if (in_array(HasCalendarEvents::class, class_uses_recursive($class), true)) {
+ $sources[] = $class;
+ }
+ }
+
+ return $cache = $sources;
+ }
+}
diff --git a/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php b/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php
index 0048058fc1..38e669408e 100644
--- a/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php
+++ b/database/migrations/2026_08_13_160100_backfill_calendar_events_from_existing_sources.php
@@ -1,7 +1,6 @@
<?php
-use App\Models\Traits\HasCalendarEvents;
-use Illuminate\Database\Eloquent\Model;
+use App\Models\CalendarEvent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Migrations\Migration;
@@ -57,27 +56,10 @@ return new class extends Migration
protected function discoverSources(): array
{
- $sources = [];
- $modelDir = app_path('Models');
- $files = glob($modelDir.'/*.php');
- foreach ($files as $file) {
- $class = 'App\\Models\\'.pathinfo($file, PATHINFO_FILENAME);
- if (! class_exists($class)) {
- continue;
- }
-
- $reflection = new \ReflectionClass($class);
- if ($reflection->isAbstract() || $reflection->isInterface() || $reflection->isTrait()) {
- continue;
- }
- if (! is_subclass_of($class, Model::class)) {
- continue;
- }
- if (in_array(HasCalendarEvents::class, class_uses_recursive($class), true)) {
- $sources[] = $class;
- }
- }
-
- return $sources;
+ // Single source of truth for the HasCalendarEvents adopter
+ // list. Lives on CalendarEvent so the controller, the
+ // reconcile command, and both backfill migrations all resolve
+ // "which models publish calendar events" the same way.
+ return CalendarEvent::sourceModels();
}
};
diff --git a/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php b/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php
index c174d59c92..78972f7006 100644
--- a/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php
+++ b/database/migrations/2026_08_13_170000_backfill_calendar_events_for_new_sources.php
@@ -1,7 +1,6 @@
<?php
-use App\Models\Traits\HasCalendarEvents;
-use Illuminate\Database\Eloquent\Model;
+use App\Models\CalendarEvent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Migrations\Migration;
@@ -48,27 +47,8 @@ return new class extends Migration
protected function discoverSources(): array
{
- $sources = [];
- $modelDir = app_path('Models');
- $files = glob($modelDir.'/*.php');
- foreach ($files as $file) {
- $class = 'App\\Models\\'.pathinfo($file, PATHINFO_FILENAME);
- if (! class_exists($class)) {
- continue;
- }
-
- $reflection = new \ReflectionClass($class);
- if ($reflection->isAbstract() || $reflection->isInterface() || $reflection->isTrait()) {
- continue;
- }
- if (! is_subclass_of($class, Model::class)) {
- continue;
- }
- if (in_array(HasCalendarEvents::class, class_uses_recursive($class), true)) {
- $sources[] = $class;
- }
- }
-
- return $sources;
+ // See CalendarEvent::sourceModels() - single source of truth
+ // for the HasCalendarEvents adopter list.
+ return CalendarEvent::sourceModels();
}
};