-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
require dirname(__DIR__). '/vendor/autoload.php'; | ||
require __DIR__. '/middlewares/DatabaseMiddleware.php'; | ||
|
||
$app = new Clim\App(['settings' => [ | ||
// default database connection parameter. | ||
// it can be overwritten by '--dsn' option from cli. | ||
// @see middlewares/DatabaseMiddleware.php | ||
|
||
'dsn' => 'pgsql:host=localhost;dbname=sampledb;user=foobar', | ||
]]); | ||
|
||
$app->add('DatabaseMiddleware'); | ||
$app->arg('sql'); | ||
|
||
$app->task(function ($opts, $args) { | ||
// database connection is supplied by service provier | ||
$db = $this->db; | ||
|
||
// what to execute is from command line argument | ||
$sql = $args['sql']; | ||
|
||
echo $sql . "\n"; | ||
|
||
foreach ($db->query($sql) as $row) { | ||
foreach ($row as $key => $value) { | ||
echo "{$key} = $value "; | ||
} | ||
echo "\n"; | ||
} | ||
}); | ||
|
||
$app->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
class DatabaseMiddleware | ||
{ | ||
private $container; | ||
|
||
public function __construct(ContainerInterface $c) | ||
{ | ||
|
||
$this->container = $c; | ||
} | ||
|
||
public function __invoke($context, $next) | ||
{ | ||
$app = $context->getApp(); | ||
|
||
$app->opt('--dsn {DSN}'); | ||
|
||
$this->container['db'] = function($c) use ($context) { | ||
$opt = $context->options(); | ||
$dsn = $opt['dsn'] ?: $c['settings']['dsn']; | ||
echo $dsn . "\n"; | ||
|
||
$db = new PDO($dsn); | ||
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); | ||
|
||
return $db; | ||
}; | ||
return $next($context); | ||
} | ||
} |