Skip to content

Commit

Permalink
Cleanup example bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Jul 31, 2017
1 parent 3c91a56 commit 5cbbecb
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/001_connect.php → examples/1-connect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Generic example for establishing a connection
*/

require './example_bootstrap.php';
require 'support/bootstrap.php';

\Amp\Loop::run(function() {
/* If you want ssl, pass as second argument an array with ssl options (an empty options array is valid too); if null is passed, ssl is not enabled either */
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require './example_bootstrap.php';
require 'support/bootstrap.php';

\Amp\Loop::run(function() {
$db = new \Amp\Mysql\Pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

require './example_bootstrap.php';
require 'support/bootstrap.php';

\Amp\Loop::run(function() {
$db = new \Amp\Mysql\Pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);
Expand All @@ -15,7 +15,7 @@
}

/* wait until everything is inserted (in case where we wouldn't have to wait, we also could just */
yield \Amp\Promise\all($promises);
yield $promises;

print "Insertion successful (if it wasn't, an exception would have been thrown by now)\n";

Expand Down
8 changes: 4 additions & 4 deletions examples/004_multi_rows.php → examples/4-multi-rows.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

require './example_bootstrap.php';
require 'support/generic_table.php';
require 'support/bootstrap.php';
require 'support/generic-table.php';

\Amp\Loop::run(function() {
$db = new \Amp\Mysql\Pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);

/* create same table than in 003_generic_with_yield.php */
yield new \Amp\Coroutine(genTable($db));
/* create same table than in 3-generic-with-yield.php */
yield from createGenericTable($db);

/* yeah, we need a lot of yields and assigns here... With PHP 7 we finally can drop a lot of stupid parenthesis! */
$query = (yield $db->query("SELECT a * b FROM tmp"));
Expand Down
8 changes: 4 additions & 4 deletions examples/005_multi_stmts.php → examples/5-multi-stmts.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php

require './example_bootstrap.php';
require 'support/generic_table.php';
require 'support/bootstrap.php';
require 'support/generic-table.php';

\Amp\Loop::run(function() {
$db = new \Amp\Mysql\Pool("host=".DB_HOST.";user=".DB_USER.";pass=".DB_PASS.";db=".DB_NAME);

/* create same table than in 003_generic_with_yield.php */
yield new \Amp\Coroutine(genTable($db));
/* create same table than in 3-generic-with-yield.php */
yield from createGenericTable($db);

/* multi statements are enabled by default, but generally stored procedures also might return multiple resultsets anyway */
$promise = $db->query("SELECT a + b FROM tmp; SELECT a - b FROM tmp;");
Expand Down
22 changes: 0 additions & 22 deletions examples/example_bootstrap.php

This file was deleted.

13 changes: 13 additions & 0 deletions examples/support/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

require __DIR__ . '/../../vendor/autoload.php';

/*
* This file is not in VCS - create it if you want to set
* the DB credentials without having them commited to VCS.
*/
@include_once __DIR__ . "/../../mysql-config.php";

if (!defined('DB_HOST') || !defined('DB_USER') || !defined('DB_PASS') || !defined('DB_NAME')) {
throw new \Exception("Must create mysql-config.php in project root directory defining connection constants");
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

/* Create table and fill in a few rows for examples; for comments see 003_generic_with_yield.php */
function genTable(\Amp\Mysql\Pool $db) {
function createGenericTable(\Amp\Mysql\Pool $db): Generator {
yield $db->query("CREATE TABLE tmp SELECT 1 AS a, 2 AS b");
$promises = [];
foreach (range(1, 5) as $num) {
$promises[] = $db->query("INSERT INTO tmp (a, b) VALUES ($num, $num * 2)");
}
yield \Amp\Promise\all($promises);
return yield $promises;
}

0 comments on commit 5cbbecb

Please sign in to comment.