diff --git a/Clim/Builder.php b/Clim/Builder.php index 0056a39..242c3ef 100644 --- a/Clim/Builder.php +++ b/Clim/Builder.php @@ -22,7 +22,9 @@ class Builder public function __construct(ContainerInterface $container) { $this->container = $container; + $this->runner = new Runner(); + $this->runner->setApp($this); } /** @@ -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; diff --git a/Clim/Context.php b/Clim/Context.php index 83131ea..241e52e 100644 --- a/Clim/Context.php +++ b/Clim/Context.php @@ -21,6 +21,9 @@ class Context implements MiddlewareContextInterface /** @var string hold tentative value */ protected $_tentative; + /** @var Builder */ + protected $app; + /** * hold result * @var string @@ -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; diff --git a/Clim/Runner.php b/Clim/Runner.php index 9d007e8..900fdb7 100644 --- a/Clim/Runner.php +++ b/Clim/Runner.php @@ -11,6 +11,9 @@ class Runner { + /** @var Builder */ + protected $app; + /** @var array */ private $options = []; @@ -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; @@ -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; diff --git a/examples/db.php b/examples/db.php new file mode 100644 index 0000000..fdf72f6 --- /dev/null +++ b/examples/db.php @@ -0,0 +1,34 @@ + [ + // 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(); diff --git a/examples/middlewares/DatabaseMiddleware.php b/examples/middlewares/DatabaseMiddleware.php new file mode 100644 index 0000000..c2e55e3 --- /dev/null +++ b/examples/middlewares/DatabaseMiddleware.php @@ -0,0 +1,33 @@ +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); + } +} \ No newline at end of file