百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
P

Porter

> 数据库
开源

: lipstick: 可持续且异步的数据导入,可大规模消费数据并发布可测试的 SDK。

612 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

: lipstick: 可持续且异步的数据导入,可大规模消费数据并发布可测试的 SDK。

Porter

[![Version image]][Releases] [![Downloads image]][Downloads] [![Build image]][Build] [![Quickstart image]][Quickstart build] [![Quickstart Symfony image]][Quickstart Symfony build] [![Coverage image]][Coverage] [![Mutation score image][MSI image]][MSI report]

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

[![][Porter icon]][Provider] [![][Porter transformers icon]][Porter transformers] [![][Porter connectors icon]][Porter connectors]

Contents

  1. Benefits
  2. Quick start
  3. About this manual
  4. Usage
  5. Porter's API
  6. Overview
  7. Import specifications
  8. Record collections
  9. Asynchronous
  10. Transformers
  11. Filtering
  12. Durability
  13. Caching
  14. Architecture
  15. Providers
  16. Resources
  17. Connectors
  18. Limitations
  19. Testing
  20. Contributing
  21. 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][MappingTransformer], 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][Sub-imports] automatically.

Quick start

To get started quickly, consuming an existing Porter provider, try one of our quick start guides:

  • [General quickstart][Quickstart] – Get started using Porter with vanilla PHP (no framework) and the [European Central Bank][ECB provider] provider.
  • [Symfony quickstart][] – Get started by integrating Porter into a new Symfony project with the [Steam provider][].

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][PSR-11 search] 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][Provider]. 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.

php
$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.

php
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 type, offering the flexibility to present data series in whatever format is most useful for the user, such as an array for JSON data or an object that bundles data with functionality that the user can directly invoke.

Details

Record collections may be Countable, depending on whether the imported data was countable and whether any destructive operations were performed after import. Filtering is a destructive operation since it may remove records and therefore the count reported by a ProviderResource would no longer be accurate. It is the responsibility of the resource to supply the total number of records in its collection by returning an iterator that implements Countable, such as ArrayIterator, or more commonly, CountableProviderRecords. When a countable iterator is used, Porter returns CountablePorterRecords, provided no destructive operations were performed.

Record collections are composed by Porter using the decorator pattern. If provider data is not modified, PorterRecords will decorate the ProviderRecords returned from a ProviderResource. That is, PorterRecords has a pointer back to the previous collection, which could be written as: PorterRecords → ProviderRecords. If a filter was applied, the collection stack would be PorterRecords → FilteredRecords → ProviderRecords. Normally, this is an unimportant detail but can sometimes be useful for debugging.

The stack of record collec

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHPabstractionasynchronousdata-importdata-transformation

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类数据库
定价开源

> 相关工具

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库