Skip to content

Commit ce69b91

Browse files
committed
Rewrote readme "quick start".
Moved "overview" section further down.
1 parent 58c5e41 commit ce69b91

File tree

1 file changed

+68
-19
lines changed

1 file changed

+68
-19
lines changed

README.md

+68-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
Porter <img src="https://github.com/ScriptFUSION/Porter/wiki/images/porter%20222x.png" align="right">
32
======
43

@@ -22,11 +21,11 @@ Contents
2221
--------
2322

2423
1. [Benefits](#benefits)
25-
1. [Overview](#overview)
26-
1. [Understanding this manual](#understanding-this-manual)
2724
1. [Quick start](#quick-start)
25+
1. [Understanding this manual](#understanding-this-manual)
2826
1. [Usage](#usage)
2927
1. [Porter's API](#porters-api)
28+
1. [Overview](#overview)
3029
1. [Import specifications](#import-specifications)
3130
1. [Record collections](#record-collections)
3231
1. [Transformers](#transformers)
@@ -46,27 +45,71 @@ Contents
4645
Benefits
4746
--------
4847

49-
Here are some of the benefits of using Porter to import data into your application.
50-
5148
* Formally defines a structured data import framework with the following concepts: [providers](#providers) represent one or more [resources](#resources) that fetch data from [connectors](#connectors).
5249
* Provides efficient in-memory data processing interfaces to handle large data sets one record at a time, via iterators, which can be implemented using generators.
5350
* Offers post-import [transformations](#transformers), such as [filtering](#filtering) and [mapping][MappingTransformer], to transform third-party into data useful for first-party applications.
5451
* Protects against intermittent network failures with [durability](#durability) features.
5552
* Supports PSR-6 [caching](#caching), at the connector level, for each fetch operation.
5653
* Joins two or more linked data sets together using [sub-imports][Sub-imports] automatically.
5754

58-
Overview
59-
--------
55+
Quick start
56+
-----------
6057

61-
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 actual interface is called `ProviderResource`, because *resource* is a reserved word in PHP. Also note, I don't know how to draw data flow diagrams, so just go with it.
58+
This quick start guide will walk through getting up and running with Porter from scratch and assumes you already have a PHP environment set up with Composer. Let's start by initializing our Composer file by running `composer init` in our project's root directory and accepting the defaults. We can skip defining dependencies interactively because we'll issue separate commands in a moment.
6259

63-
<div align="center">
60+
Let's start with the [European Central Bank][ECB provider] (ECB) provider by including it in our `composer.json` with the following command.
6461

65-
![Data flow diagram][Data flow diagram]
62+
```sh
63+
composer require provider/european-central-bank
64+
```
6665

67-
</div>
66+
We now have the provider installed along with all its dependencies, including Porter herself. We want to create a `new Porter` instance now, but we need to pass a `ContainerInterface` to her constructor. Any PSR-11 container is valid, but let's use Joomla DI for now.
6867

69-
Our application calls `Porter::import()` with an `ImportSpecification` and receives `PorterRecords` in return. Everything else happens internally and we don't need to worry about it unless writing custom providers and resources.
68+
```sh
69+
composer require joomla/di
70+
```
71+
72+
Create a new container and register an instance of `EuropeanCentralBankProvider` with it. Pass the container to a new Porter instance. Don't forget to include the autoloader!
73+
74+
```php
75+
use Joomla\DI\Container;
76+
use ScriptFUSION\Porter\Porter;
77+
use ScriptFUSION\Porter\Provider\EuropeanCentralBank\Provider\EuropeanCentralBankProvider;
78+
79+
require 'vendor/autoload.php';
80+
81+
$container = new Container;
82+
$container->set(EuropeanCentralBankProvider::class, new EuropeanCentralBankProvider);
83+
84+
$porter = new Porter($container);
85+
```
86+
87+
We're now ready to import any of the ECB's resources. Let's import the latest daily foreign exchange rates provided by `DailyForexRates`. Porter's `import()` method requires an `ImportSpecification` which accepts the resource we want to import.
88+
89+
```php
90+
$rates = $porter->import(new ImportSpecification(new DailyForexRates));
91+
```
92+
93+
Porter returns an iterator so we can now loop over the rates and print them out.
94+
95+
```php
96+
foreach ($rates as $rate) {
97+
echo "$rate[currency]: $rate[rate]\n";
98+
}
99+
```
100+
101+
This outputs something similar to the following, with today's current rates.
102+
103+
>USD: 1.2304
104+
JPY: 131.66
105+
BGN: 1.9558
106+
CZK: 25.357
107+
DKK: 7.4469
108+
...
109+
110+
Since these rates come from the European Central Bank, they're relative to the Euro (EUR), which is assumed to always be `1`. We can use this information to write a currency converter that's always up-to-date with the latest exchange rates.
111+
112+
This just scratches the surface of Porter without going into any details. Explore the rest of this manual at your leisure to gain a fuller understanding of the features at your disposal.
70113

71114
Understanding this manual
72115
-------------------------
@@ -75,13 +118,6 @@ The first half of this manual covers Porter's main features and how to use them.
75118

76119
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 entire project as a whole.
77120

78-
Quick start
79-
-----------
80-
81-
The quickest way to start is with a [ready-made provider][Provider]. [Usage](#usage) shows how to register a provider and import data from its resources. Even if there isn't an existing provider you want to use, it's still recommended to try out an existing one first, to get comfortable with Porter before writing your own.
82-
83-
If a provider does not yet exist for the service we want to use, we must write one from scratch. This isn't as quick, but don't worry, it's pretty easy! Start by writing the [provider](#providers) that supplies the appropriate connector to access the service. Next, define a [resource](#resources) that fetches data using the connector and `yield` it as an `array`. For more information, see [writing a provider](#writing-a-provider) and [writing a resource](#writing-a-resource).
84-
85121
Usage
86122
-----
87123

@@ -125,6 +161,19 @@ Porter's API
125161
* `import(ImportSpecification) : PorterRecords|CountablePorterRecords` &ndash; Imports data according to the design of the specified import specification.
126162
* `importOne(ImportSpecification) : ?array` &ndash; Imports one record according to the design of the specified import specification. If more than one record is imported, `ImportException` is thrown. Use this when you're sure a provider just returns a single record.
127163

164+
Overview
165+
--------
166+
167+
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 actual interface is called `ProviderResource`, because *resource* is a reserved word in PHP. Also note, I don't know how to draw data flow diagrams, so just go with it.
168+
169+
<div align="center">
170+
171+
![Data flow diagram][Data flow diagram]
172+
173+
</div>
174+
175+
Our application calls `Porter::import()` with an `ImportSpecification` and receives `PorterRecords` in return. Everything else happens internally and we don't need to worry about it unless writing custom providers and resources.
176+
128177
Import specifications
129178
---------------------
130179

0 commit comments

Comments
 (0)