Skip to content

Commit

Permalink
added example of DatabaseMiddleware
Browse files Browse the repository at this point in the history
  • Loading branch information
basuke committed Apr 19, 2017
1 parent 351dc1e commit c1450c3
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Clim/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class Builder
public function __construct(ContainerInterface $container)
{
$this->container = $container;

$this->runner = new Runner();
$this->runner->setApp($this);
}

/**
Expand Down Expand Up @@ -94,7 +96,7 @@ public function dispatch($meta_var, $children)
*
* @since 1.1.0
*/
public function add(callable $callable)
public function add($callable)
{
$this->runner->pushMiddleware($this->containerBoundCallable($callable));
return $this;
Expand Down
13 changes: 13 additions & 0 deletions Clim/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class Context implements MiddlewareContextInterface
/** @var string hold tentative value */
protected $_tentative;

/** @var Builder */
protected $app;

/**
* hold result
* @var string
Expand Down Expand Up @@ -110,6 +113,16 @@ public function tentative($value = null)
}
}

public function getApp()
{
return $this->app;
}

public function setApp(Builder $app)
{
$this->app = $app;
}

public function getResult()
{
return $this->result;
Expand Down
10 changes: 10 additions & 0 deletions Clim/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

class Runner
{
/** @var Builder */
protected $app;

/** @var array */
private $options = [];

Expand Down Expand Up @@ -38,6 +41,11 @@ public function __construct(array $options = [], array $arguments = [])
$this->arguments = $arguments;
}

public function setApp(Builder $app)
{
$this->app = $app;
}

public function addOption(Option $option)
{
$this->options[] = $option;
Expand All @@ -64,6 +72,8 @@ public function run($context)
$context = new Context($context);
}

if ($this->app) $context->setApp($this->app);

return $this->task_middleware->run($context, function ($context) {
if ($this->parse($context)) return $context;

Expand Down
34 changes: 34 additions & 0 deletions examples/db.php
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();
33 changes: 33 additions & 0 deletions examples/middlewares/DatabaseMiddleware.php
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);
}
}

0 comments on commit c1450c3

Please sign in to comment.