From 938b2cf3b4469148def23a0441955745204cc381 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Mon, 2 Oct 2023 19:05:09 +0200 Subject: [PATCH] Rework most of the examples to be runnable via PHAR (#497) --- examples/bootstrap.php | 4 --- examples/output/{.gitkep => .gitkeep} | 0 examples/topics/aggregations/power_plant.php | 5 ++-- .../aggregations/power_plant_bar_chart.php | 5 ++-- examples/topics/async/bootstrap.php | 8 ++++++ examples/topics/async/csv_to_db_async_amp.php | 2 +- .../topics/async/csv_to_db_async_react.php | 2 +- examples/topics/async/csv_to_db_sync.php | 2 +- .../topics/async/csv_to_json_async_react.php | 2 +- .../topics/async/csv_to_text_async_react.php | 2 +- .../topics/fs/remote/json_remote_stream.php | 2 +- .../fs/remote/json_remote_stream_glob.php | 2 +- .../topics/join/left_anti/left_anti_join.php | 5 ++-- .../join/left_anti/left_anti_join_each.php | 5 ++-- examples/topics/transformations/aggregate.php | 5 ++-- .../topics/transformations/array_expand.php | 5 ++-- .../topics/transformations/array_unpack.php | 5 ++-- .../{filter.php => filter_divide.php} | 16 ++---------- .../topics/transformations/filter_mod.php | 24 ++++++++++++++++++ examples/topics/transformations/group_by.php | 5 ++-- examples/topics/transformations/literals.php | 5 ++-- examples/topics/transformations/math.php | 9 +++---- examples/topics/transformations/size.php | 5 ++-- examples/topics/transformations/sort.php | 5 ++-- .../{when.php => when_null.php} | 19 ++------------ examples/topics/transformations/when_odd.php | 25 +++++++++++++++++++ .../topics/types/csv/csv_read_partitioned.php | 2 +- examples/topics/types/csv/csv_to_json.php | 10 ++------ examples/topics/types/xml/reading.php | 5 ++-- examples/topics/types/xml/salaries.php | 5 ++-- 30 files changed, 101 insertions(+), 95 deletions(-) rename examples/output/{.gitkep => .gitkeep} (100%) create mode 100644 examples/topics/async/bootstrap.php rename examples/topics/transformations/{filter.php => filter_divide.php} (62%) create mode 100644 examples/topics/transformations/filter_mod.php rename examples/topics/transformations/{when.php => when_null.php} (68%) create mode 100644 examples/topics/transformations/when_odd.php diff --git a/examples/bootstrap.php b/examples/bootstrap.php index 042ce5a13..872e8150f 100644 --- a/examples/bootstrap.php +++ b/examples/bootstrap.php @@ -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__); diff --git a/examples/output/.gitkep b/examples/output/.gitkeep similarity index 100% rename from examples/output/.gitkep rename to examples/output/.gitkeep diff --git a/examples/topics/aggregations/power_plant.php b/examples/topics/aggregations/power_plant.php index 3e3fc0e89..ac6c47b9b 100644 --- a/examples/topics/aggregations/power_plant.php +++ b/examples/topics/aggregations/power_plant.php @@ -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.', '') @@ -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)); diff --git a/examples/topics/aggregations/power_plant_bar_chart.php b/examples/topics/aggregations/power_plant_bar_chart.php index 8fc159db4..cfe95a4ae 100644 --- a/examples/topics/aggregations/power_plant_bar_chart.php +++ b/examples/topics/aggregations/power_plant_bar_chart.php @@ -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.', '') @@ -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(); + ); diff --git a/examples/topics/async/bootstrap.php b/examples/topics/async/bootstrap.php new file mode 100644 index 000000000..661a0b122 --- /dev/null +++ b/examples/topics/async/bootstrap.php @@ -0,0 +1,8 @@ +load(__FLOW_EXAMPLES_AUTOLOAD__ . '/.env'); +$dotenv->load(__DIR__ . '/../../.env'); $s3_client_option = [ 'client' => [ diff --git a/examples/topics/fs/remote/json_remote_stream_glob.php b/examples/topics/fs/remote/json_remote_stream_glob.php index 629a9d4ec..6810b5619 100644 --- a/examples/topics/fs/remote/json_remote_stream_glob.php +++ b/examples/topics/fs/remote/json_remote_stream_glob.php @@ -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' => [ diff --git a/examples/topics/join/left_anti/left_anti_join.php b/examples/topics/join/left_anti/left_anti_join.php index 2b237601a..3ef332392 100644 --- a/examples/topics/join/left_anti/left_anti_join.php +++ b/examples/topics/join/left_anti/left_anti_join.php @@ -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 // diff --git a/examples/topics/join/left_anti/left_anti_join_each.php b/examples/topics/join/left_anti/left_anti_join_each.php index 66d3c1b64..6b9723b87 100644 --- a/examples/topics/join/left_anti/left_anti_join_each.php +++ b/examples/topics/join/left_anti/left_anti_join_each.php @@ -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: // diff --git a/examples/topics/transformations/aggregate.php b/examples/topics/transformations/aggregate.php index 7e0d3cade..c8fdd7979 100644 --- a/examples/topics/transformations/aggregate.php +++ b/examples/topics/transformations/aggregate.php @@ -13,7 +13,7 @@ require __DIR__ . '/../../bootstrap.php'; -(new Flow()) +return (new Flow()) ->read( From::rows(new Rows( Row::with(Entry::int('a', 100)), @@ -24,5 +24,4 @@ )) ) ->aggregate(Aggregation::sum(ref('a'))) - ->write(To::output(false)) - ->run(); + ->write(To::output(false)); diff --git a/examples/topics/transformations/array_expand.php b/examples/topics/transformations/array_expand.php index 8c748a044..999aaf0f0 100644 --- a/examples/topics/transformations/array_expand.php +++ b/examples/topics/transformations/array_expand.php @@ -13,7 +13,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])), @@ -21,5 +21,4 @@ ) ->write(To::output(false)) ->withEntry('expanded', array_expand(ref('array'))) - ->write(To::output(false)) - ->run(); + ->write(To::output(false)); diff --git a/examples/topics/transformations/array_unpack.php b/examples/topics/transformations/array_unpack.php index 4c1fddd50..98adffa66 100644 --- a/examples/topics/transformations/array_unpack.php +++ b/examples/topics/transformations/array_unpack.php @@ -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])), @@ -21,5 +21,4 @@ ) ->write(To::output(false)) ->withEntry('unpacked', ref('row')->unpack()) - ->write(To::output(false)) - ->run(); + ->write(To::output(false)); diff --git a/examples/topics/transformations/filter.php b/examples/topics/transformations/filter_divide.php similarity index 62% rename from examples/topics/transformations/filter.php rename to examples/topics/transformations/filter_divide.php index b4f84b716..3b6d8eaf2 100644 --- a/examples/topics/transformations/filter.php +++ b/examples/topics/transformations/filter_divide.php @@ -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)), @@ -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)); diff --git a/examples/topics/transformations/filter_mod.php b/examples/topics/transformations/filter_mod.php new file mode 100644 index 000000000..9dd70dfba --- /dev/null +++ b/examples/topics/transformations/filter_mod.php @@ -0,0 +1,24 @@ +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)); diff --git a/examples/topics/transformations/group_by.php b/examples/topics/transformations/group_by.php index 0023be214..1c5afa278 100644 --- a/examples/topics/transformations/group_by.php +++ b/examples/topics/transformations/group_by.php @@ -12,7 +12,7 @@ require __DIR__ . '/../../bootstrap.php'; -(new Flow()) +return (new Flow()) ->read( From::rows(new Rows( Row::with(Entry::int('a', 100)), @@ -23,5 +23,4 @@ )) ) ->groupBy(ref('a')) - ->write(To::output(false)) - ->run(); + ->write(To::output(false)); diff --git a/examples/topics/transformations/literals.php b/examples/topics/transformations/literals.php index 49878c490..b7d43b0c5 100644 --- a/examples/topics/transformations/literals.php +++ b/examples/topics/transformations/literals.php @@ -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)); diff --git a/examples/topics/transformations/math.php b/examples/topics/transformations/math.php index 8f801c6a0..823c4be30 100644 --- a/examples/topics/transformations/math.php +++ b/examples/topics/transformations/math.php @@ -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)); diff --git a/examples/topics/transformations/size.php b/examples/topics/transformations/size.php index b27a3e7fd..36b4ba508 100644 --- a/examples/topics/transformations/size.php +++ b/examples/topics/transformations/size.php @@ -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)); diff --git a/examples/topics/transformations/sort.php b/examples/topics/transformations/sort.php index b5a4205c7..05cf8d5fa 100644 --- a/examples/topics/transformations/sort.php +++ b/examples/topics/transformations/sort.php @@ -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)); diff --git a/examples/topics/transformations/when.php b/examples/topics/transformations/when_null.php similarity index 68% rename from examples/topics/transformations/when.php rename to examples/topics/transformations/when_null.php index 562469844..bc3c78932 100644 --- a/examples/topics/transformations/when.php +++ b/examples/topics/transformations/when_null.php @@ -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)), @@ -40,5 +26,4 @@ 'value', when(ref('value')->isNull(), then: lit(0)) ) - ->write(To::output(false)) - ->run(); + ->write(To::output(false)); diff --git a/examples/topics/transformations/when_odd.php b/examples/topics/transformations/when_odd.php new file mode 100644 index 000000000..2e018f392 --- /dev/null +++ b/examples/topics/transformations/when_odd.php @@ -0,0 +1,25 @@ +read(From::sequence_number('number', 1, 100)) + ->collect() + ->withEntry( + 'type', + when( + ref('number')->isOdd(), + then: lit('odd'), + else: lit('even') + ) + ) + ->write(To::output(false)); diff --git a/examples/topics/types/csv/csv_read_partitioned.php b/examples/topics/types/csv/csv_read_partitioned.php index 4b785f6dc..22fd8be93 100644 --- a/examples/topics/types/csv/csv_read_partitioned.php +++ b/examples/topics/types/csv/csv_read_partitioned.php @@ -41,7 +41,7 @@ ->drop('row') ->collect() ->filterPartitions(Partitions::only('t_shirt_color', 'green')) - ->sortBy(col('id')) + ->sortBy(ref('id')) ->write(To::output()) ->run(); diff --git a/examples/topics/types/csv/csv_to_json.php b/examples/topics/types/csv/csv_to_json.php index 81fc62eb8..d8414ea5e 100644 --- a/examples/topics/types/csv/csv_to_json.php +++ b/examples/topics/types/csv/csv_to_json.php @@ -12,15 +12,9 @@ \unlink(__FLOW_OUTPUT__ . '/dataset.json'); } -(new Flow()) +return (new Flow()) ->read(CSV::from(__FLOW_OUTPUT__ . '/dataset.csv')) ->withEntry('unpacked', ref('row')->unpack()) ->renameAll('unpacked.', '') ->drop(col('row')) - ->write(To::callback(static function (Rows $rows) use (&$total, $memory) : void { - $total += $rows->count(); - - $memory->current(); - })) - ->write(Json::to(__FLOW_OUTPUT__ . '/dataset.json')) - ->run(); + ->write(Json::to(__FLOW_OUTPUT__ . '/dataset.json')); diff --git a/examples/topics/types/xml/reading.php b/examples/topics/types/xml/reading.php index 3c27d7874..b93ce3021 100644 --- a/examples/topics/types/xml/reading.php +++ b/examples/topics/types/xml/reading.php @@ -10,7 +10,6 @@ print "Reading XML dataset...\n"; -(new Flow()) +return (new Flow()) ->read(XML::from(__FLOW_DATA__ . '/simple_items.xml', 'root/items/item')) - ->write(To::output(false)) - ->run(); + ->write(To::output(false)); diff --git a/examples/topics/types/xml/salaries.php b/examples/topics/types/xml/salaries.php index 2efe1f867..c8655e275 100644 --- a/examples/topics/types/xml/salaries.php +++ b/examples/topics/types/xml/salaries.php @@ -12,7 +12,7 @@ print "Reading XML dataset...\n"; -(new Flow()) +return (new Flow()) ->read(XML::from(__FLOW_DATA__ . '/salaries.xml')) ->withEntry('months', ref('row')->xpath('/Salaries/Month')) ->withEntry('month', ref('months')->expand()) @@ -25,5 +25,4 @@ ->groupBy(ref('month_name')) ->aggregate(Aggregation::sum(ref('department_salary'))) ->rename('department_salary_sum', 'total_monthly_salaries') - ->write(To::output(false)) - ->run(); + ->write(To::output(false));