-
Notifications
You must be signed in to change notification settings - Fork 12
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
External User Commands #5
Comments
Experience with the Symfony Console has shown that it is important to provide users an interface for defining their own commands. I support this idea. I think it would be a good idea to store all user commands in a predefined directory and automatically include them in Slim Console. |
Two questions arise:
|
|
👍 for directory name, also, it may contain subdirectories as well.
Makes sense to me. |
Sweet, I was looking forward to this issue ~ wanted to comment earlier but know I've added a few scope creep comments :) Although this may be considered currently outside the current scope, I can't help but think that each core group of setup One concern that's jabbing at me is our initial implementation's close coupling Example External Commands Plugin Repo: PhpDi Container Commands Repo
Plugin.php
Setup Inside Of Slim Core
Then we can simply use the command |
@zhorton34 I partially agree with you that the |
@ABGEO07 Which commands/command types are you thinking should be apart of the slim-console core? |
@zhorton34 I think the The Slim Console Core should be an interface for managing user and third-party commands. We can also define a "command to create a command" ( |
@ABGEO07 I absolutely agree. One point, that I may be jumping the gun on, is whether we want to directly integrate 3rd party commands (Phinx for example) or create slim adapter commands that integrate those commands. The concern for this thought thread in lies our capabilities to use dependency injection with commands. If a container is implemented, and only if, then we'd want to allow the command's handler function to resolve dependencies from the container. |
@zhorton34 It would probably be better to use some kind of bridge or adapter, as you said, for integration with 3rd party commands. Let's get some feedback and decide after that. |
@l0gicgate What are the expectations for creating issues within this repo? We'll need a way for commands to run other commands, I have a pretty good idea on how to set that up but want to make sure I'm following how the slim team normally goes about building out core packages. |
@zhorton34 feel free to create issues as new problems arise! I’m also in favour of using a bridge for integrating with third party librairies. This way, it’s easy for other people to create libraries to plug into the console or develop commands for the console. We can also version the integration process this way. |
What if we gave devs the option to publish the commands. Once the commands are registered in slim and everything is setup, that command will take 20 minutes to write.
|
Say a repository /* file: src/Commands/HelloCommand.php */
declare(strict_types=1);
namespace Example\Commands;
use Slim\Console\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HelloCommand extends AbstractCommand
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Hello World');
return 0;
}
} Say a developer wants to use that, so he/she runs $ composer require slim/console
$ composer require example/slim-commands Now the file containing the command is stored in IdeaWe define a file (like /* file: slim-console.php */
declare(strict_types=1);
return [
]; So the dev just needs to add the command loader provided by the /* file: slim-console.php */
declare(strict_types=1);
return [
\Example\CommandLoader::class
]; The slim console app would require this file, iterate over all these classes (command loaders), check if they are really instances of Below is an example of the command loader provided by the <?php
/* file: vendor/example/slim-commands/src/Example/CommandLoader.php */
declare(strict_types=1);
namespace Example;
use Example\Commands\HelloCommand;
use Slim\Console\Interfaces\SlimConsoleCommandLoaderInterface;
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
use Symfony\Component\Console\CommandLoader\FactoryCommandLoader;
class CommandLoader implements SlimConsoleCommandLoaderInterface
{
public function getCommandLoader(): CommandLoaderInterface
{
return new FactoryCommandLoader([
'example:hello' => function () {
return new HelloCommand();
}
]);
}
} What are your thoughts about that? |
Actually IdeaThe The /* ... */
/**
* @return array<string, Closure>
*/
public static function getCommandFactories(): array
{
return [
'example:hello' => function () {
return new HelloCommand();
}
];
}
/* ... */ Once the I guess that way, the Slim Console can be extended simply by requiring new packages. So And if a dev would like to add his/her own commands (specific to the project), then it is only a matter of writing a Please tell me, if I am completely wrong. |
@adriansuter this makes sense to me. We should provide an interface for a custom command loader which merges them into the application. I think we may need to add that into the
<?php
use Package\Commands\Command;
return [
// Can be a class name
Command::class,
// Can be a closure and must return a Command
static function (): CommandInterface {
return new MyCommand($dependency);
},
]; Then our command loader parses that file and iterates over those and merges them into the |
Symfony has already two loaders, the They (one of them) could be used to bring these commands into our app - best of all, they allow some sort of deferred instantiation of the command classes (the class would be created only if needed). I don't think we should mix class files and pure configuration files in the same directory. So I would rather put the file containing the command factories providers to the root of the project by default. But yes, that should be configurable. The idea is, that the Slim Console provides the most fundamental commands only. Really just a slim set of commands. If a dev needs more commands, he/she just requires one or more packages via composer and adds the command factories provider class name(s) to the php array in the file mentioned above. Of course, he/she could always add custom developed commands. That's just a matter of writing the commands, writing the command factories provider and adding it to the php array. This is because the set of commands for an API style app would be different than the set of commands used for a Web style app. So those could be separated into two different repositories (packages). Commands for database queries are again another isolated use case, as not every app needs a database (or needs console access to the database). |
I would like for the console to be easily extensible by the end user. How would we go about this?
I was thinking that we could establish a project base level directory that would give a plugin interface to the user to add commands to the existing console.
Should we make a folder where we loop through files and add the commands in those files? Or could we have the CLI look for
../../commands/external.php
which would return an array of user defined commands so we can then call$this->addCommands($external)
within theApplication
classThe text was updated successfully, but these errors were encountered: