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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]) | ||
| ->newInstance(Runner::class) | ||
| ->run() | ||
| ; | ||
|
|
||
| exit($status); | ||
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 use Aura\Cli\_Config\Common;
$di = $builder->newInstance([], [Common::class, Config::class]);There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh good trick, i did not know, thanks, good stuff 😊 thanks @harikt There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is how aura internally loads all the classes via the project kernel https://github.com/auraphp/Aura.Project_Kernel/blob/develop-2/src/Project.php#L156-L163 and pass to the builder https://github.com/auraphp/Aura.Project_Kernel/blob/develop-2/src/Factory.php#L41-L51 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks and good to know it helps. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
| ; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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]; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
There was a problem hiding this comment.
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
That may always help people who are new and don't want to check the api what it is doing.
There was a problem hiding this comment.
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 💃
There was a problem hiding this comment.
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 ;) .
There was a problem hiding this comment.
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 ))