Skip to content

Commit

Permalink
Rework most of the examples to be runnable via PHAR (#497)
Browse files Browse the repository at this point in the history
  • Loading branch information
stloyd authored Oct 2, 2023
1 parent c50899a commit 938b2cf
Show file tree
Hide file tree
Showing 30 changed files with 101 additions and 95 deletions.
4 changes: 0 additions & 4 deletions examples/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
const __FLOW_VAR__ = __DIR__ . '/var';
const __FLOW_VAR_RUN__ = __DIR__ . '/var/run';
const __FLOW_SRC__ = __DIR__ . '/../src';
const __FLOW_AUTOLOAD__ = __DIR__ . '/../vendor/autoload.php';

// library autoload for all dependencies
require __FLOW_AUTOLOAD__;

if (!\is_dir(__FLOW_VAR__)) {
\mkdir(__FLOW_VAR__);
Expand Down
File renamed without changes.
5 changes: 2 additions & 3 deletions examples/topics/aggregations/power_plant.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(CSV::from(__FLOW_DATA__ . '/power-plant-daily.csv', 10, delimiter: ';'))
->withEntry('unpacked', ref('row')->unpack())
->renameAll('unpacked.', '')
Expand Down Expand Up @@ -44,5 +44,4 @@
->withEntry('consumption', ref('consumption_kwh_sum')->divide(ref('production_kwh_sum')))
->withEntry('consumption', ref('consumption')->multiply(lit(100))->round(lit(2)))
->withEntry('consumption', concat(ref('consumption'), lit('%')))
->write(To::output(truncate: false))
->run();
->write(To::output(truncate: false));
5 changes: 2 additions & 3 deletions examples/topics/aggregations/power_plant_bar_chart.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow)
return (new Flow)
->read(CSV::from(__FLOW_DATA__ . '/power-plant-daily.csv', 10, delimiter: ';'))
->withEntry('unpacked', ref('row')->unpack())
->renameAll('unpacked.', '')
Expand Down Expand Up @@ -49,5 +49,4 @@
ChartJS::bar(label: ref('date'), datasets: [ref('production_kwh_avg'), ref('consumption_kwh_avg')]),
output: __FLOW_OUTPUT__ . '/power_plant_bar_chart.html'
)
)
->run();
);
8 changes: 8 additions & 0 deletions examples/topics/async/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php declare(strict_types=1);

require __DIR__ . '/../../bootstrap.php';

const __FLOW_AUTOLOAD__ = __DIR__ . '/../vendor/autoload.php';

// library autoload for all dependencies
require __FLOW_AUTOLOAD__;
2 changes: 1 addition & 1 deletion examples/topics/async/csv_to_db_async_amp.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Monolog\Logger;
use Psr\Log\LogLevel;

require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/bootstrap.php';

$dbConnection = require __DIR__ . '/../db/db_clean.php';

Expand Down
2 changes: 1 addition & 1 deletion examples/topics/async/csv_to_db_async_react.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Monolog\Logger;
use Psr\Log\LogLevel;

require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/bootstrap.php';

$dbConnection = require __DIR__ . '/../db/db_clean.php';

Expand Down
2 changes: 1 addition & 1 deletion examples/topics/async/csv_to_db_sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Monolog\Handler\StreamHandler;
use Monolog\Logger;

require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/bootstrap.php';

$dbConnection = require __DIR__ . '/../db/db_clean.php';

Expand Down
2 changes: 1 addition & 1 deletion examples/topics/async/csv_to_json_async_react.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Monolog\Logger;
use Psr\Log\LogLevel;

require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/bootstrap.php';

$dbConnection = require __DIR__ . '/../db/db_clean.php';

Expand Down
2 changes: 1 addition & 1 deletion examples/topics/async/csv_to_text_async_react.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Monolog\Logger;
use Psr\Log\LogLevel;

require __DIR__ . '/../../bootstrap.php';
require __DIR__ . '/bootstrap.php';

$dbConnection = require __DIR__ . '/../db/db_clean.php';

Expand Down
2 changes: 1 addition & 1 deletion examples/topics/fs/remote/json_remote_stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
require __DIR__ . '/../../../bootstrap.php';

$dotenv = new Dotenv();
$dotenv->load(__FLOW_EXAMPLES_AUTOLOAD__ . '/.env');
$dotenv->load(__DIR__ . '/../../.env');

$s3_client_option = [
'client' => [
Expand Down
2 changes: 1 addition & 1 deletion examples/topics/fs/remote/json_remote_stream_glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require __DIR__ . '/../../../../vendor/autoload.php';

$dotenv = new Dotenv();
$dotenv->load(__FLOW_EXAMPLES_AUTOLOAD__ . '/.env');
$dotenv->load(__DIR__ . '/../../.env');

$s3_client_option = [
'client' => [
Expand Down
5 changes: 2 additions & 3 deletions examples/topics/join/left_anti/left_anti_join.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,14 @@
* then it might become performance bottleneck.
* In that case please look at DataFrame::joinEach.
*/
(new Flow())
return (new Flow())
->process($externalProducts)
->join(
(new Flow())->process($internalProducts),
Expression::on(new Equal('id', 'id')), // by using All or Any comparisons, more than one entry can be used to prepare the condition
Join::left_anti
)
->write(To::output())
->run();
->write(To::output());

// Output
//
Expand Down
5 changes: 2 additions & 3 deletions examples/topics/join/left_anti/left_anti_join_each.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,14 @@ private function findRowsInDatabase(Rows $rows) : Rows
* right size is much bigger then a left side. In that case it's better to reduce the ride side
* by fetching from the storage only what is relevant for the left side.
*/
(new Flow())
return (new Flow())
->extract($apiExtractor)
->joinEach(
$dbDataFrameFactory,
Expression::on(new Equal('id', 'id')), // by using All or Any comparisons, more than one entry can be used to prepare the condition
Join::left_anti
)
->write(To::output())
->run();
->write(To::output());

// Output:
//
Expand Down
5 changes: 2 additions & 3 deletions examples/topics/transformations/aggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('a', 100)),
Expand All @@ -24,5 +24,4 @@
))
)
->aggregate(Aggregation::sum(ref('a')))
->write(To::output(false))
->run();
->write(To::output(false));
5 changes: 2 additions & 3 deletions examples/topics/transformations/array_expand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('id', 1), Entry::array('array', ['a' => 1, 'b' => 2, 'c' => 3])),
))
)
->write(To::output(false))
->withEntry('expanded', array_expand(ref('array')))
->write(To::output(false))
->run();
->write(To::output(false));
5 changes: 2 additions & 3 deletions examples/topics/transformations/array_unpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('id', 1), Entry::array('array', ['a' => 1, 'b' => 2, 'c' => 3])),
Expand All @@ -21,5 +21,4 @@
)
->write(To::output(false))
->withEntry('unpacked', ref('row')->unpack())
->write(To::output(false))
->run();
->write(To::output(false));
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('a', 100), Entry::int('b', 100)),
Expand All @@ -22,16 +22,4 @@
)
->filter(ref('b')->divide(lit(2))->equals(lit('a')))
->withEntry('new_b', ref('b')->multiply(lit(2))->multiply(lit(5)))
->write(To::output(false))
->run();

(new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('a', 4), Entry::int('b', 5)),
Row::with(Entry::int('a', 3), Entry::int('b', 6))
))
)
->filter(ref('b')->mod(lit(2))->equals(lit(0)))
->write(To::output(false))
->run();
->write(To::output(false));
24 changes: 24 additions & 0 deletions examples/topics/transformations/filter_mod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\lit;
use function Flow\ETL\DSL\ref;
use Flow\ETL\DSL\Entry;
use Flow\ETL\DSL\From;
use Flow\ETL\DSL\To;
use Flow\ETL\Flow;
use Flow\ETL\Row;
use Flow\ETL\Rows;

require __DIR__ . '/../../bootstrap.php';

return (new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('a', 4), Entry::int('b', 5)),
Row::with(Entry::int('a', 3), Entry::int('b', 6))
))
)
->filter(ref('b')->mod(lit(2))->equals(lit(0)))
->write(To::output(false));
5 changes: 2 additions & 3 deletions examples/topics/transformations/group_by.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('a', 100)),
Expand All @@ -23,5 +23,4 @@
))
)
->groupBy(ref('a'))
->write(To::output(false))
->run();
->write(To::output(false));
5 changes: 2 additions & 3 deletions examples/topics/transformations/literals.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::create(Entry::string('name', 'Norbert'))
))
)
->withEntry('number', lit(1))
->write(To::output(false))
->run();
->write(To::output(false));
9 changes: 4 additions & 5 deletions examples/topics/transformations/math.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::create(Entry::integer('a', 100), Entry::integer('b', 200))
))
)
->write(To::output(false))
->withEntry('c', ref('a')->plus('b'))
->withEntry('d', ref('b')->minus('a'))
->write(To::output(false))
->run();
->withEntry('c', ref('a')->plus(ref('b')))
->withEntry('d', ref('b')->minus(ref('a')))
->write(To::output(false));
5 changes: 2 additions & 3 deletions examples/topics/transformations/size.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(
From::rows(new Rows(
Row::with(Entry::int('id', 1), Entry::array('array', ['a' => 1, 'b' => 2, 'c' => 3])),
))
)
->withEntry('array_size', ref('array')->size())
->write(To::output(false))
->run();
->write(To::output(false));
5 changes: 2 additions & 3 deletions examples/topics/transformations/sort.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
return (new Flow())
->read(From::sequence_number('id', 0, 10))
->sortBy(ref('id')->desc())
->write(To::output(false))
->run();
->write(To::output(false));
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,7 @@

require __DIR__ . '/../../bootstrap.php';

(new Flow())
->read(From::sequence_number('number', 1, 100))
->collect()
->withEntry(
'type',
when(
ref('number')->isOdd(),
then: lit('odd'),
else: lit('even')
)
)
->write(To::output(false))
->run();

(new Flow())
return (new Flow())
->read(From::rows(new Rows(
Row::with(Entry::int('id', 1), Entry::int('value', 1)),
Row::with(Entry::int('id', 2), Entry::int('value', 1)),
Expand All @@ -40,5 +26,4 @@
'value',
when(ref('value')->isNull(), then: lit(0))
)
->write(To::output(false))
->run();
->write(To::output(false));
25 changes: 25 additions & 0 deletions examples/topics/transformations/when_odd.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use function Flow\ETL\DSL\lit;
use function Flow\ETL\DSL\ref;
use function Flow\ETL\DSL\when;
use Flow\ETL\DSL\From;
use Flow\ETL\DSL\To;
use Flow\ETL\Flow;

require __DIR__ . '/../../bootstrap.php';

return (new Flow())
->read(From::sequence_number('number', 1, 100))
->collect()
->withEntry(
'type',
when(
ref('number')->isOdd(),
then: lit('odd'),
else: lit('even')
)
)
->write(To::output(false));
2 changes: 1 addition & 1 deletion examples/topics/types/csv/csv_read_partitioned.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
->drop('row')
->collect()
->filterPartitions(Partitions::only('t_shirt_color', 'green'))
->sortBy(col('id'))
->sortBy(ref('id'))
->write(To::output())
->run();

Expand Down
Loading

0 comments on commit 938b2cf

Please sign in to comment.