Skip to content

A free open source IT asset/license management system

Added typeSlug() on the SyncAdapter instance

snipe647140531c62
diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php
index 73dbe1308a..b9f52bc436 100644
--- a/app/Http/Controllers/SettingsController.php
+++ b/app/Http/Controllers/SettingsController.php
@@ -625,7 +625,7 @@ class SettingsController extends Controller
$setting->label2_2d_target = $request->input('label2_2d_target');
$setting->label2_fields = $request->input('label2_fields');
$setting->label2_empty_row_count = $request->input('label2_empty_row_count');
- if (!$wasLabel2Enabled && !$request->boolean('label2_enable')) {
+ if (! $wasLabel2Enabled && ! $request->boolean('label2_enable')) {
$setting->labels_per_page = $request->input('labels_per_page');
$setting->labels_width = $request->input('labels_width');
$setting->labels_height = $request->input('labels_height');
@@ -873,7 +873,7 @@ class SettingsController extends Controller
*/
public function getAdapters(Request $request): View|RedirectResponse
{
- $adapterTypes = \App\SyncAdapters\AdapterRegistry::typeLabels();
+ $adapterTypes = \App\SyncAdapters\SyncAdapter::typeLabels();
// The filter is for UX when per-company adapters exist. Gate on whether the
// install has any companies at all. 'Shared' means "no company_id" (built-ins and adapters available to all).
@@ -899,7 +899,7 @@ class SettingsController extends Controller
// database ORDER BY label.
$readinessRank = ['active' => 0, 'partial' => 1, 'inactive' => 2];
$adapters = $instanceQuery->get()
- ->map(fn ($i) => \App\SyncAdapters\AdapterRegistry::hydrate($i))
+ ->map(fn ($i) => \App\SyncAdapters\SyncAdapter::factory($i))
->filter()
->sortBy(fn ($a) => $readinessRank[$a->readinessStatus()] ?? 99)
->values()
@@ -939,7 +939,7 @@ class SettingsController extends Controller
}
$validated = $request->validate([
- 'adapter_type' => 'required|string|in:'.implode(',', \App\SyncAdapters\AdapterRegistry::typeNames()),
+ 'adapter_type' => 'required|string|in:'.implode(',', \App\SyncAdapters\SyncAdapter::typeNames()),
'label' => 'required|string|max:191|unique:sync_adapter_instances,label',
'company_id' => 'nullable|integer|exists:companies,id',
]);
diff --git a/app/Models/SyncAdapterInstance.php b/app/Models/SyncAdapterInstance.php
index 3e3e1d3fcc..bfc3e8c1bd 100644
--- a/app/Models/SyncAdapterInstance.php
+++ b/app/Models/SyncAdapterInstance.php
@@ -2,7 +2,6 @@
namespace App\Models;
-use App\SyncAdapters\AdapterRegistry;
use App\SyncAdapters\SyncAdapter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
@@ -56,7 +55,7 @@ class SyncAdapterInstance extends Model
*/
public function adapter(): ?SyncAdapter
{
- return AdapterRegistry::hydrate($this);
+ return SyncAdapter::factory($this);
}
private static function generateUniqueSlug(string $label): string
diff --git a/app/SyncAdapters/Addigy/AddigyAdapter.php b/app/SyncAdapters/Addigy/AddigyAdapter.php
index 62fdd05809..8c28e1c6ac 100644
--- a/app/SyncAdapters/Addigy/AddigyAdapter.php
+++ b/app/SyncAdapters/Addigy/AddigyAdapter.php
@@ -19,6 +19,11 @@ class AddigyAdapter extends SyncAdapter
return 'Addigy';
}
+ public static function typeSlug(): string
+ {
+ return 'addigy';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://prod.addigy.com';
diff --git a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php
index 4df8366ac7..1e0a8e49de 100644
--- a/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php
+++ b/app/SyncAdapters/AppleBusinessManager/AppleBusinessManagerAdapter.php
@@ -76,6 +76,11 @@ class AppleBusinessManagerAdapter extends SyncAdapter
return 'Apple Business Manager';
}
+ public static function typeSlug(): string
+ {
+ return 'abm';
+ }
+
/**
* ABM/ASM base URLs are host-fixed per mode (business or school),
* so the admin never types a URL. Mode selection in the
diff --git a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
index 4783a79a9b..87020ec619 100644
--- a/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
+++ b/app/SyncAdapters/CustomHttp/CustomHttpAdapter.php
@@ -77,6 +77,11 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
return 'Custom HTTP';
}
+ public static function typeSlug(): string
+ {
+ return 'custom_http';
+ }
+
public function settingsSchema(): array
{
return [
@@ -354,7 +359,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
public function validationRules(): array
{
$rules = parent::validationRules();
- $rules[$this->instance->slug . '_extras_definition'] = [
+ $rules[$this->instance->slug.'_extras_definition'] = [
'nullable',
'string',
new \App\Rules\CustomHttpExtrasJsonRule,
@@ -421,7 +426,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
public function push(Asset $asset, array $changedFields = []): void
{
$externalSource = $this->pushPrologue($asset, $changedFields);
- if ($externalSource === null || !$this->canPush()) {
+ if ($externalSource === null || ! $this->canPush()) {
return;
}
@@ -434,7 +439,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
// URL it targets that base URL exactly, which some APIs
// accept for record-scoped writes when the id is inside the
// request body rather than the path.
- $endpoint = rtrim($this->url(), '/') . $this->resolvedPushPath($this->safeCredential('push_path'), (string) $externalSource->external_id);
+ $endpoint = rtrim($this->url(), '/').$this->resolvedPushPath($this->safeCredential('push_path'), (string) $externalSource->external_id);
$method = $this->resolvedPushMethod();
if ($this->isPushDryRun()) {
@@ -470,7 +475,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
$touched = [];
$fieldPaths = $this->fieldPathMap();
foreach ($this->pushDirectedFields() as $field) {
- if (!in_array($field, self::PUSHABLE_STANDARD_FIELDS, true)) {
+ if (! in_array($field, self::PUSHABLE_STANDARD_FIELDS, true)) {
continue;
}
$path = $fieldPaths[$field] ?? '';
@@ -563,8 +568,8 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
return '';
}
$path = str_replace('{external_id}', rawurlencode($externalId), $template);
- if (!str_starts_with($path, '/')) {
- $path = '/' . $path;
+ if (! str_starts_with($path, '/')) {
+ $path = '/'.$path;
}
return $path;
@@ -609,7 +614,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
$globalIndex = 0;
foreach ($this->paginate() as $records) {
foreach ($records as $record) {
- if (!is_array($record)) {
+ if (! is_array($record)) {
$globalIndex++;
continue;
@@ -681,7 +686,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
}
$records = self::dotPathGet($body, $recordsPath);
- if (!is_array($records)) {
+ if (! is_array($records)) {
Log::channel('sync-adapters')->warning(sprintf(
'%s pull expected an array at records path "%s", got %s',
$this->name(),
@@ -770,10 +775,10 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
}
$pullPath = $this->safeCredential('pull_path');
- if ($pullPath !== '' && !str_starts_with($pullPath, '/')) {
- $pullPath = '/' . $pullPath;
+ if ($pullPath !== '' && ! str_starts_with($pullPath, '/')) {
+ $pullPath = '/'.$pullPath;
}
- $endpoint = $baseUrl . $pullPath;
+ $endpoint = $baseUrl.$pullPath;
}
$request = Http::acceptJson()->timeout(30);
@@ -896,7 +901,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
$current = $data;
foreach (explode('.', $path) as $segment) {
- if (!is_array($current)) {
+ if (! is_array($current)) {
return null;
}
if (array_key_exists($segment, $current)) {
@@ -921,7 +926,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
if ($value === null) {
return null;
}
- if (!is_scalar($value)) {
+ if (! is_scalar($value)) {
return null;
}
@@ -975,7 +980,7 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
}
$decoded = json_decode($raw, true);
- if (!is_array($decoded)) {
+ if (! is_array($decoded)) {
return $baseline;
}
@@ -1003,13 +1008,13 @@ class CustomHttpAdapter extends SyncAdapter implements PushableAdapter
}
$decoded = json_decode($raw, true);
- if (!is_array($decoded)) {
+ if (! is_array($decoded)) {
return [];
}
$out = [];
foreach ($decoded as $entry) {
- if (!is_array($entry) || !isset($entry['key'], $entry['path'])) {
+ if (! is_array($entry) || ! isset($entry['key'], $entry['path'])) {
continue;
}
$out[] = [
diff --git a/app/SyncAdapters/Fleet/FleetAdapter.php b/app/SyncAdapters/Fleet/FleetAdapter.php
index 2926348d22..8c9a2dadaa 100644
--- a/app/SyncAdapters/Fleet/FleetAdapter.php
+++ b/app/SyncAdapters/Fleet/FleetAdapter.php
@@ -26,6 +26,11 @@ class FleetAdapter extends SyncAdapter
return 'Fleet';
}
+ public static function typeSlug(): string
+ {
+ return 'fleet';
+ }
+
public function settingsSchema(): array
{
return [
diff --git a/app/SyncAdapters/Intune/IntuneAdapter.php b/app/SyncAdapters/Intune/IntuneAdapter.php
index fd4df06032..ace0dc404e 100644
--- a/app/SyncAdapters/Intune/IntuneAdapter.php
+++ b/app/SyncAdapters/Intune/IntuneAdapter.php
@@ -35,6 +35,11 @@ class IntuneAdapter extends SyncAdapter implements PushableAdapter
return 'Microsoft Intune';
}
+ public static function typeSlug(): string
+ {
+ return 'intune';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://graph.microsoft.com';
diff --git a/app/SyncAdapters/Jamf/JamfAdapter.php b/app/SyncAdapters/Jamf/JamfAdapter.php
index 2b4e100452..ff6a92767c 100644
--- a/app/SyncAdapters/Jamf/JamfAdapter.php
+++ b/app/SyncAdapters/Jamf/JamfAdapter.php
@@ -24,6 +24,11 @@ class JamfAdapter extends SyncAdapter implements PushableAdapter
return 'Jamf Pro';
}
+ public static function typeSlug(): string
+ {
+ return 'jamf';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://your-subdomain.jamfcloud.com';
diff --git a/app/SyncAdapters/JamfSchool/JamfSchoolAdapter.php b/app/SyncAdapters/JamfSchool/JamfSchoolAdapter.php
index 562a6a8b3e..583a7e3dd8 100644
--- a/app/SyncAdapters/JamfSchool/JamfSchoolAdapter.php
+++ b/app/SyncAdapters/JamfSchool/JamfSchoolAdapter.php
@@ -21,6 +21,11 @@ class JamfSchoolAdapter extends SyncAdapter
return 'Jamf School';
}
+ public static function typeSlug(): string
+ {
+ return 'jamf_school';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://your-subdomain.jamfcloud.com/api';
diff --git a/app/SyncAdapters/JumpCloud/JumpCloudAdapter.php b/app/SyncAdapters/JumpCloud/JumpCloudAdapter.php
index a54ae978f5..d75713640e 100644
--- a/app/SyncAdapters/JumpCloud/JumpCloudAdapter.php
+++ b/app/SyncAdapters/JumpCloud/JumpCloudAdapter.php
@@ -20,6 +20,11 @@ class JumpCloudAdapter extends SyncAdapter
return 'JumpCloud';
}
+ public static function typeSlug(): string
+ {
+ return 'jumpcloud';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://console.jumpcloud.com/api';
diff --git a/app/SyncAdapters/Kandji/KandjiAdapter.php b/app/SyncAdapters/Kandji/KandjiAdapter.php
index 112db2c4f8..5c8fa7d6c6 100644
--- a/app/SyncAdapters/Kandji/KandjiAdapter.php
+++ b/app/SyncAdapters/Kandji/KandjiAdapter.php
@@ -23,6 +23,11 @@ class KandjiAdapter extends SyncAdapter implements PushableAdapter
return 'Kandji';
}
+ public static function typeSlug(): string
+ {
+ return 'kandji';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://your-subdomain.api.kandji.io';
diff --git a/app/SyncAdapters/KaseyaVsa10/KaseyaVsa10Adapter.php b/app/SyncAdapters/KaseyaVsa10/KaseyaVsa10Adapter.php
index da955df476..1b811bac4a 100644
--- a/app/SyncAdapters/KaseyaVsa10/KaseyaVsa10Adapter.php
+++ b/app/SyncAdapters/KaseyaVsa10/KaseyaVsa10Adapter.php
@@ -40,6 +40,11 @@ class KaseyaVsa10Adapter extends SyncAdapter
return 'Kaseya VSA 10';
}
+ public static function typeSlug(): string
+ {
+ return 'kaseya_vsa10';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://your-tenant.vsax.net';
diff --git a/app/SyncAdapters/MerakiSystemsManager/MerakiSystemsManagerAdapter.php b/app/SyncAdapters/MerakiSystemsManager/MerakiSystemsManagerAdapter.php
index 4d1be3a8fa..a7863ef383 100644
--- a/app/SyncAdapters/MerakiSystemsManager/MerakiSystemsManagerAdapter.php
+++ b/app/SyncAdapters/MerakiSystemsManager/MerakiSystemsManagerAdapter.php
@@ -24,6 +24,11 @@ class MerakiSystemsManagerAdapter extends SyncAdapter
return 'Meraki Systems Manager';
}
+ public static function typeSlug(): string
+ {
+ return 'meraki_sm';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://api.meraki.com/api/v1';
diff --git a/app/SyncAdapters/Mosyle/MosyleAdapter.php b/app/SyncAdapters/Mosyle/MosyleAdapter.php
index 69cd8eb36d..b971c9c487 100644
--- a/app/SyncAdapters/Mosyle/MosyleAdapter.php
+++ b/app/SyncAdapters/Mosyle/MosyleAdapter.php
@@ -25,6 +25,11 @@ class MosyleAdapter extends SyncAdapter implements PushableAdapter
return 'Mosyle';
}
+ public static function typeSlug(): string
+ {
+ return 'mosyle';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://managerapi.mosyle.com/v2';
diff --git a/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php b/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php
index 8b6f0c36e4..0480ff161a 100644
--- a/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php
+++ b/app/SyncAdapters/NinjaOne/NinjaOneAdapter.php
@@ -38,6 +38,11 @@ class NinjaOneAdapter extends SyncAdapter implements PushableAdapter
return 'NinjaOne';
}
+ public static function typeSlug(): string
+ {
+ return 'ninjaone';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://app.ninjarmm.com';
diff --git a/app/SyncAdapters/Osctrl/OsctrlAdapter.php b/app/SyncAdapters/Osctrl/OsctrlAdapter.php
index d92f892dbe..f5ebb6be1a 100644
--- a/app/SyncAdapters/Osctrl/OsctrlAdapter.php
+++ b/app/SyncAdapters/Osctrl/OsctrlAdapter.php
@@ -23,6 +23,11 @@ class OsctrlAdapter extends SyncAdapter
return 'osctrl';
}
+ public static function typeSlug(): string
+ {
+ return 'osctrl';
+ }
+
public function settingsSchema(): array
{
return [
diff --git a/app/SyncAdapters/SyncAdapter.php b/app/SyncAdapters/SyncAdapter.php
index 491859c1e9..a8a15ba8a2 100644
--- a/app/SyncAdapters/SyncAdapter.php
+++ b/app/SyncAdapters/SyncAdapter.php
@@ -5,8 +5,6 @@ namespace App\SyncAdapters;
use App\Models\SyncAdapterConfig;
use App\Models\SyncAdapterInstance;
use App\Rules\ExternalUrl;
-use App\SyncAdapters\Support\MappingTargets;
-use App\SyncAdapters\Support\NotesComposer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
@@ -47,9 +45,108 @@ abstract class SyncAdapter
/** Display label for the adapter TYPE (not the instance). Shown in the "Add adapter" dropdown. */
abstract public static function typeLabel(): string;
+ /**
+ * Stable identifier stored in sync_adapter_instances.adapter_type.
+ * Distinct from typeLabel() (localized display string) and name()
+ * (per-instance slug). Adapters declare their own so the discovery
+ * loop below can key the class map without a central registry.
+ */
+ abstract public static function typeSlug(): string;
+
/** Vendor-specific inventory fetch. Yield HostInventoryRecord objects. */
abstract public function pull(): iterable;
+ /**
+ * Discover every adapter subclass under app/SyncAdapters/*Adapter.php
+ * and return a slug-keyed map of the concrete classes. Cached in a
+ * static so repeat calls within a request don't re-scan the tree.
+ *
+ * @return array<string, class-string<self>>
+ */
+ public static function allTypes(): array
+ {
+ static $cache = null;
+ if ($cache !== null) {
+ return $cache;
+ }
+
+ $out = [];
+ $base = app_path('SyncAdapters').DIRECTORY_SEPARATOR;
+ foreach (glob($base.'*'.DIRECTORY_SEPARATOR.'*Adapter.php') as $file) {
+ $relative = substr($file, strlen($base), -4);
+ $class = 'App\\SyncAdapters\\'.str_replace(DIRECTORY_SEPARATOR, '\\', $relative);
+ if (! class_exists($class) || ! is_subclass_of($class, self::class)) {
+ continue;
+ }
+ $reflection = new \ReflectionClass($class);
+ if ($reflection->isAbstract()) {
+ continue;
+ }
+ $out[$class::typeSlug()] = $class;
+ }
+ ksort($out);
+
+ return $cache = $out;
+ }
+
+ /**
+ * Instantiate the adapter class for a given SyncAdapterInstance row.
+ * Returns null when the instance's adapter_type doesn't match any
+ * discovered class (e.g. an old row for a removed adapter).
+ */
+ public static function factory(SyncAdapterInstance $instance): ?static
+ {
+ $types = self::allTypes();
+ $class = $types[$instance->adapter_type] ?? null;
+ if ($class === null) {
+ return null;
+ }
+
+ return new $class($instance);
+ }
+
+ /**
+ * List of registered type slugs, in the same order as allTypes().
+ *
+ * @return array<int, string>
+ */
+ public static function typeNames(): array
+ {
+ return array_keys(self::allTypes());
+ }
+
+ /**
+ * Type slug -> display label. Powers the "Add adapter" dropdown.
+ *
+ * @return array<string, string>
+ */
+ public static function typeLabels(): array
+ {
+ $out = [];
+ foreach (self::allTypes() as $slug => $class) {
+ $out[$slug] = $class::typeLabel();
+ }
+
+ return $out;
+ }
+
+ /**
+ * Hydrate every configured instance from the database as a live
+ * adapter, skipping rows whose adapter_type is no longer registered.
+ *
+ * @return array<int, self>
+ */
+ public static function allInstances(): array
+ {
+ return SyncAdapterInstance::query()
+ ->orderBy('label')
+ ->get()
+ ->map(fn (SyncAdapterInstance $i) => self::factory($i))
+ ->filter()
+ ->values()
+ ->all();
+ }
+
/**
* Declare this adapter's authentication field schema. The base
* class drives every settings-page, storage, validation, and
diff --git a/app/SyncAdapters/SyncHostFromAdapter.php b/app/SyncAdapters/SyncHostFromAdapter.php
index 4ddaf5d761..7d63b7aeb0 100644
--- a/app/SyncAdapters/SyncHostFromAdapter.php
+++ b/app/SyncAdapters/SyncHostFromAdapter.php
@@ -12,7 +12,7 @@ use App\Models\SyncAdapterConfig;
use App\Models\SyncAdapterInstance;
use App\Models\User;
use App\SyncAdapters\SyncAdapter;
-use App\SyncAdapters\Support\MappingTargets;
+use App\SyncAdapters\MappingTargets;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use RuntimeException;
diff --git a/app/SyncAdapters/Unifi/UnifiAdapter.php b/app/SyncAdapters/Unifi/UnifiAdapter.php
index 33ab55ff9c..fa9fc5e4dc 100644
--- a/app/SyncAdapters/Unifi/UnifiAdapter.php
+++ b/app/SyncAdapters/Unifi/UnifiAdapter.php
@@ -30,6 +30,11 @@ class UnifiAdapter extends SyncAdapter
return 'UniFi';
}
+ public static function typeSlug(): string
+ {
+ return 'unifi';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://your-controller:8443';
diff --git a/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php b/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php
index e6492bb0e4..76f51849ba 100644
--- a/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php
+++ b/app/SyncAdapters/WorkspaceOne/WorkspaceOneAdapter.php
@@ -41,6 +41,11 @@ class WorkspaceOneAdapter extends SyncAdapter implements PushableAdapter
return 'Omnissa Workspace ONE';
}
+ public static function typeSlug(): string
+ {
+ return 'workspace_one';
+ }
+
public function baseUrlPlaceholder(): ?string
{
return 'https://as###.awmdm.com';
diff --git a/app/SyncAdapters/Zentral/ZentralAdapter.php b/app/SyncAdapters/Zentral/ZentralAdapter.php
index 34d7eee8b9..ef7cfdf7a1 100644
--- a/app/SyncAdapters/Zentral/ZentralAdapter.php
+++ b/app/SyncAdapters/Zentral/ZentralAdapter.php
@@ -19,6 +19,11 @@ class ZentralAdapter extends SyncAdapter
return 'Zentral';
}
+ public static function typeSlug(): string
+ {
+ return 'zentral';
+ }
+
public function settingsSchema(): array
{
return [