source
Source
The Source object is a central concept in the package, representing a connection to an external data system. It serves as the gateway for all interactions with that system, such as inspecting available datasets (tables, files) and initiating synchronization tasks.
A Source represents where the data comes from (e.g., a MySQL database server, a CSV file, an API). It holds configuration details like hostnames, credentials, or file paths. It is defined once and can be reused for multiple syncs.
This is distinct from a Sync or Dataset. This represents what specific data is being copied (e.g., the users table, the products.csv file). Multiple syncs can belong to a single source.
Creating a Source
You create a Source using the ExtractAndTransform::createSource method. This method persists the configuration to the database and returns a fluent Source object instance that you can use immediately.
use Andach\ExtractAndTransform\Facades\ExtractAndTransform;
// Create a CSV Source
$csvSource = ExtractAndTransform::createSource('Product CSVs', 'csv', [
'path' => storage_path('app/imports/products.csv')
]);
// Create a MySQL Database Source
$dbSource = ExtractAndTransform::createSource('Legacy ERP', 'sql', [
'driver' => 'mysql',
'host' => '10.0.0.5',
'database' => 'erp_db',
'username' => 'read_only',
'password' => 'secret',
]);
The returned $source object allows you to immediately chain methods like sync():
$dbSource->sync('users')->run();
Retrieving an Existing Source
If you have already created a source and need to use it later (e.g., in a scheduled job or a different controller), you can retrieve it by its name using ExtractAndTransform::source().
use Andach\ExtractAndTransform\Facades\ExtractAndTransform;
// Retrieve the source by the name you gave it during creation
$source = ExtractAndTransform::source('Legacy ERP');
// Now you can use it to run syncs
$source->sync('orders')->run();
Accessing the Underlying Model
The Source object is a wrapper around an Eloquent model (Andach\ExtractAndTransform\Models\ExtractSource). If you need to access the underlying model directly—for example, to get its database ID for a foreign key relationship—you can use the getModel() method.
$source = ExtractAndTransform::createSource('My Source', 'csv', [...]);
// Get the Eloquent model instance
$model = $source->getModel();
echo $model->id; // e.g., 1
echo $model->name; // "My Source"
Available Methods
The Source object provides several methods to interact with the external system:
- sync(string $datasetIdentifier): Sync Prepares a synchronization task for a specific dataset (e.g., table name or file path). Returns a Sync builder object.
- getDataset(string $identifier): ?Dataset Retrieves metadata about a specific dataset from the source, such as its schema. Returns a Dataset object or null if not found.
- run(): array Triggers all configured sync profiles associated with this source. Returns an array of SyncRun results.
- getModel(): ExtractSource Returns the underlying Eloquent model instance.