Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auraphp decoupled cli #1

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion composer.json
Expand Up @@ -8,7 +8,11 @@
"email": "cordoval@gmail.com"
}
],
"require": {},
"require": {
"aura/di": "2.*@dev",
"aura/cli": "2.*@dev",
"aura/dispatcher": "2.*@dev"
},
"require-dev": {
"phpspec/phpspec": "~2.1@dev",
"bossa/phpspec2-expect": "dev-master@dev",
Expand Down
15 changes: 15 additions & 0 deletions console.php
@@ -0,0 +1,15 @@
<?php

use Aura\Di\ContainerBuilder;
use Cordoval\Console\Config;
use Cordoval\Console\Runner;

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

$status = (new ContainerBuilder())
->newInstance([], [Config::class])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally my preference and love is to make the above code as

$builder = new ContainerBuilder();
$di = $builder->newInstance([], [Config::class]);
$app = $di->newInstance(Runner::class);
exit($app->run());

That may always help people who are new and don't want to check the api what it is doing.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe components should be about clean and clear apis so I am in favor of the version i proposed. I don't like to be too verbose and not use php5.5+ features. I bleed 💃

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

personal preferences :) . If you are the only developer it is good, else ;) .

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I say the same with your ways 😊 but it is ok, @pmjones is on my side ))

->newInstance(Runner::class)
->run()
;

exit($status);
20 changes: 0 additions & 20 deletions spec/Cordoval/Console/ApplicationSpec.php

This file was deleted.

10 changes: 0 additions & 10 deletions src/Console/Application.php

This file was deleted.

42 changes: 42 additions & 0 deletions src/Console/Config.php
@@ -0,0 +1,42 @@
<?php

namespace Cordoval\Console;

use Aura\Cli\Context;
use Aura\Cli\Stdio;
use Aura\Di\Container;
use Aura\Dispatcher\Dispatcher;
use Cordoval\Console\Task\Help;
use Aura\Cli\_Config\Common;

class Config extends Common
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to extend the Common class. You just extend with Aura\Cli\Config and pass the Aura\Cli\Common also to the builder. So it will be like

use Aura\Cli\_Config\Common;

$di = $builder->newInstance([], [Common::class, Config::class]);

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh good trick, i did not know, thanks, good stuff 😊 thanks @harikt

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was one of the things that cost me to understand, but after i did I did away with it and simplify it. But you do a great job explaining it.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks and good to know it helps.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am thinking now decoupling Gush using a custom container with auraphp but using an extension system

{
public function define(Container $di)
{
parent::define($di);

$di->set('cordoval:stdio', $di->lazyNew(Stdio::class));
$di->set('cordoval:context', $di->lazyNew(Context::class));
$di->set(
'cordoval:dispatcher',
$di->lazyNew(Dispatcher::class, ['object_param' => 'command'])
);

$di->params[Runner::class] = [
'dispatcher' => $di->lazyGet('cordoval:dispatcher'),
'context' => $di->lazyGet('cordoval:context'),
];

$di->params[Help::class] = [
'context' => $di->lazyGet('cordoval:context'),
'stdio' => $di->lazyGet('cordoval:stdio'),
];
}

public function modify(Container $di)
{
$di->get('cordoval:dispatcher')
->setObject('help', $di->lazyNew(Help::class))
;
}
}
37 changes: 37 additions & 0 deletions src/Console/Runner.php
@@ -0,0 +1,37 @@
<?php

namespace Cordoval\Console;

use Aura\Dispatcher\Dispatcher;
use Aura\Cli\Context;

/**
* It loads cli context from prompt and dispatches to registered callable commands
*/
final class Runner
{
private $dispatcher;
private $context;

public function __construct(Dispatcher $dispatcher, Context $context)
{
$this->dispatcher = $dispatcher;
$this->context = $context;
}

public function run()
{
list($params, $command) = $this->loadContext();

return (int) $this->dispatcher->__invoke($params, $command);
}

public function loadContext()
{
$params = $this->context->argv->get();
array_shift($params);
$command = array_shift($params);

return [$params, $command];
}
}
26 changes: 26 additions & 0 deletions src/Console/Task/Help.php
@@ -0,0 +1,26 @@
<?php

namespace Cordoval\Console\Task;

use Aura\Cli\Stdio;
use Aura\Cli\Context;
use Aura\Cli\Status;

class Help
{
private $context;
private $stdio;

public function __construct(Context $context, Stdio $stdio)
{
$this->context = $context;
$this->stdio = $stdio;
}

public function __invoke()
{
$this->stdio->outln('all good boss');

return Status::USAGE;
}
}