Porter 
Durable and asynchronous data imports for consuming data at scale and publishing testable SDKs
Porter is the all-purpose PHP data importer. She fetches data from APIs, web scraping or anywhere and serves it as an iterable record collection, encouraging processing one record at a time instead of loading full data sets into memory. Durability features provide automatic, transparent recovery from intermittent network errors by default.
Porter's interface triad of providers, resources and connectors allows us to publish testable SDKs and maps well to APIs and HTTP endpoints. For example, a typical API such as GitHub would define the provider as GitHubProvider, a resource as GetUser or ListRepositories and the connector could be HttpConnector.
Porter supports asynchronous imports via fibers allowing multiple imports to be started, paused and resumed concurrently. Async allows us to import data as fast as possible, transforming applications from network-bound (slow) to CPU-bound (optimal). Throttle support ensures we do not exceed peer connection or throughput limits.
Porter network quick links

Contents
- Benefits
- Quick start
- About this manual
- Usage
- Porter's API
- Overview
- Import specifications
- Record collections
- Asynchronous
- Transformers
- Filtering
- Durability
- Caching
- Architecture
- Providers
- Resources
- Connectors
- Limitations
- Testing
- Contributing
- License
Benefits
- Defines an easily testable interface triad for data imports: providers represent one or more resources that fetch data from connectors. These interfaces make it very easy to test and mock specific parts of the import lifecycle using industry standard tools, whether we want to mock at the connector level and feed in raw responses or mock at the resource level and supply ready-hydrated objects.
- Provides memory-efficient data processing interfaces that handle large data sets one record at a time, via iterators, which can be implemented using deferred execution with generators.
- Asynchronous imports offer highly efficient CPU-bound data processing for large scale imports across multiple connections concurrently, eliminating network latency performance bottlenecks. Concurrency can be rate-limited using throttling.
- Protects against intermittent network failures with durability features that transparently and automatically retry failed data fetches.
- Offers post-import transformations, such as filtering and mapping, to transform third-party data into useful data for our applications.
- Supports PSR-6 caching, at the connector level, for each fetch operation.
- Joins two or more linked data sets together using sub-imports automatically.
Quick start
To get started quickly, consuming an existing Porter provider, try one of our quick start guides:
For a more thorough introduction continue reading.
About this manual
Those consuming a Porter provider create one instance of Porter for their application and an instance of Import for each data import they wish to perform. Those publishing providers must implement Provider and ProviderResource.
The first half of this manual covers Porter's main API for consuming data services. The second half covers architecture, interface and implementation details for publishing data services. There's an intermission in-between, so you'll know where the separation is!
Text marked as inline code denotes literal code, as it would appear in a PHP file. For example, Porter refers specifically to the class of the same name within this library, whereas Porter refers to this project as a whole.
Usage
Creating the container
Create a new Porter instance—we'll usually only need one per application. Porter's constructor requires a PSR-11 compatible ContainerInterface that acts as a repository of providers.
When integrating Porter into a typical MVC framework application, we'll usually have a service locator or DI container implementing this interface already. We can simply inject the entire container into Porter, although it's best practice to create a separate container just for Porter's providers. For an example of doing this correctly in Symfony, see the Symfony quickstart.
Without a framework, pick any PSR-11 compatible library and inject an instance of its container class. We could even write our own container since the interface is easy to implement, but using an existing library is beneficial, particularly since most support lazy-loading of services. If you're not sure which to use, Joomla DI seems fairly simple and light.
Registering providers
Configure the container by registering one or more Porter providers. In this example we'll add the ECB provider for foreign exchange rates. Most provider libraries will export just one provider class; in this case it's EuropeanCentralBankProvider. We could add the provider to the container by writing something similar to $container->set(EuropeanCentralBankProvider::class, new EuropeanCentralBankProvider), but consult the manual for your particular container implementation for the exact syntax.
It is recommended to use the provider's class name as the container service name, as in the example in the previous paragraph. Porter will retrieve the service matching the provider's class name by default, so this reduces friction when getting started. If we use a different service name, it will need to be configured on the Import by calling setProviderName().
Importing data
Porter's import method accepts an Import that describes which data should be imported and how the data should be transformed. To import DailyForexRates without applying any transformations we can write the following.
$records = $porter->import(new Import(new DailyForexRates));
Calling import() returns an instance of PorterRecords or CountablePorterRecords, which both implement Iterator, allowing each record in the collection to be enumerated using foreach as in the following example.
foreach ($records as $record) {
var_dump($record);
}
Porter's API
Porter's simple API comprises data import methods that must always be used to begin imports, instead of calling methods directly on providers or resources, in order to take advantage of Porter's features correctly.
Porter provides just two public methods for importing data. These are the methods to be most familiar with, where the life of a data import operation begins.
import(Import): PorterRecords|CountablePorterRecords – Imports one or more records from the resource contained in the specified import specification. If the total size of the collection is known, the record collection may implement Countable, otherwise PorterRecords is returned.
importOne(Import): mixed – Imports one record from the resource contained in the specified import specification. If more than one record is imported, ImportException is thrown. Use this when a provider implements SingleRecordResource, returning just a single record.
Overview
The following data flow diagram gives a high level overview of Porter's main interfaces and the data flows between them when importing data. Note that we use the term resource for brevity, but the interface is actually called ProviderResource, because resource is a reserved word in PHP.
Our application calls Porter::import() with an Import and receives PorterRecords back. Everything else happens internally, so we don't need to worry about it unless writing custom providers, resources or connectors.
Import specifications
Import specifications specify what to import, how it should be transformed and whether to use caching. Create a new instance of Import and pass a ProviderResource that specifies the resource we want to import.
Options may be configured using the methods below.
setProviderName(string) – Sets the provider service name.
addTransformer(Transformer) – Adds a transformer to the end of the transformation queue.
addTransformers(Transformer[]) – Adds one or more transformers to the end of the transformation queue.
setContext(mixed) – Specifies user-defined data to be passed to transformers.
enableCache() – Enables caching. Requires a CachingConnector.
setMaxFetchAttempts(int) – Sets the maximum number of fetch attempts per connection before failure is considered permanent.
setFetchExceptionHandler(FetchExceptionHandler) – Sets the exception handler invoked each time a fetch attempt fails.
setThrottle(Throttle) – Sets the connection throttle, invoked each time a connector fetches data.
Record collections
Record collections are Iterators, guaranteeing imported data is enumerable using foreach. Each record of the collection is the mixed