← Larasources 10 / 10

Reference

Source

Method Does
fetch() Read live from the origin and refresh the stored row
save() Build, push to the origin, and persist. Throws when the origin does not take it
trySave() The same, returning an OriginResult instead of throwing
reconcile() Re-read from the origin while the row is still processing, a no-op when it is not
delete() Delete at the origin and drop the row, keeping it when the origin throws or reports it deleted nothing
clear() Drop the stored row only
persist(?OriginStatus $status, ?string $externalId) Write the row for this source
build() Where your record becomes their payload. Override it
record(), getRecord() The stored SourceRecord or null, looked up once per instance, never from the origin
externalId() What the service calls this resource, when an origin has reported one
setExternalId(?string $id) Name it before anything is stored, which is the identity of a source with no model
attachTo(Model $model) Give the stored row its model, re-keyed to the model's name for it
make(array $attributes = []) A new instance, for a source that belongs to no model
useArgument(string $name, $value) Point an argument at a model or at another source
forgetArgument(string $name) Stop pointing it anywhere
resolveArguments() The arguments exactly as the origin will receive them
origin() The resolved Origin instance
setVariant(?string $variant) Set the variant
setVariantArguments(?array $args) Arguments for this call, highest precedence
setSourceable($model), getSourceable() The model this source hangs from
config(?string $key, mixed $default) This source's settings, under larasources.sources.{name}
name() How its owner calls it: the key the model maps this class under

Plus the model-like surface: fill(), forceFill(), toArray(), toJson(), $fillable, $guarded, $casts, $hidden, $visible, $appends, getXAttribute() accessors and array access.

On the model, through HasSources: source(string|Source $source, ?array $arguments = null, ?string $variant = null), getSources() and mockSource().

Origin

Method Does
fetch(array $arguments): array Read from the service. The only abstract one
save(array $data): array|OriginResult Write. Return the response, or an OriginResult to report a status or an id
delete(): bool Delete. Returning means it is gone
getAlias(): string Identifies the integration, in config and on every row
config(?string $key, mixed $default) This integration's settings, under larasources.origins.{alias}. Override for credentials in the database
isConfigured(): bool Whether it has what it is about to use. True unless you say otherwise
http(): PendingRequest HTTP client with this integration's timeout and retries
getSource(): Source The source being read or written

OriginResult

status (an OriginStatus), data, message, exception and externalId, plus ok(), processing() and failed().

OriginStatus is Saved, Processing or Failed. Only the first two are ever stored, because a row exists because an origin took the data.

SourceRecord

The row, as a plain Eloquent model on the sources table.

processing() Scope: rows the origin had not finished with
unattached() Scope: rows with no model yet
attachTo(Model $model) Set the row's model, throwing when it already has one for this source and variant
toSource(string $sourceClass) Turn the row back into its source, carrying the variant and the row
sourceable The model it belongs to
sourceArguments Its pointer arguments
arguments The JSON column merged with those pointers, resolved

The tables

sources

Column What it holds
sourceable_type, sourceable_id The model, nullable: a row can exist before it
name How its owner calls it
variant The mode, nullable
origin The alias of the client that wrote it
attributes The payload, as JSON
signature An md5 of the payload, for telling whether anything changed
status What the origin last said: saved or processing
external_id What the service calls the resource, when it says
arguments Arguments pinned to this row, as JSON

Unique on (sourceable_type, sourceable_id, name, variant). Indexed on (name, external_id) and (name, variant, external_id).

source_arguments

One row per argument that points at something: source_id, name, and the argumentable morph. Unique on (source_id, name), because a name points at one thing.

Exceptions

OriginException for anything a service did or refused to do, and what an origin should throw. MassAssignmentException when a fill is rejected by a totally guarded source.

Attributes

#[UsesOrigin(SomeOrigin::class)] on a Source class, which is how it finds its client. It is not inherited by reflection, so a subclass declares its own.

built and maintained by Edu Lazaro · MIT license