Skip to content

Commit

Permalink
up: now, support run an plugin file
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 23, 2021
1 parent 6090b9c commit d419548
Show file tree
Hide file tree
Showing 9 changed files with 485 additions and 114 deletions.
109 changes: 12 additions & 97 deletions app/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
namespace Inhere\Kite\Console;

use Inhere\Console\ConsoleEvent;
use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Console\Listener\NotFoundListener;
use Inhere\Kite\Console\Plugin\PluginManager;
use Inhere\Kite\Kite;
use Inhere\Kite\Plugin\AbstractPlugin;
use Toolkit\Stdlib\Arr\ArrayHelper;
use function file_exists;
use const BASE_PATH;
Expand All @@ -25,26 +25,9 @@
class Application extends \Inhere\Console\Application
{
/**
* loaded plugin objects
*
* @var AbstractPlugin[]
* @var PluginManager
*/
private $plugins = [];

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

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

/**
* @var array
*/
private $pluginClasses = [];
private $plugManager;

protected function prepareRun(): void
{
Expand All @@ -61,9 +44,7 @@ protected function init(): void

$this->loadAppConfig();

$this->initAppEnv();

$this->on(ConsoleEvent::ON_NOT_FOUND, $this->onNotFound());
$this->initAppRun();
}

private function loadAppConfig(): void
Expand Down Expand Up @@ -100,86 +81,20 @@ private function loadAppConfig(): void
$this->setConfig($config);
}

protected function initAppEnv(): void
{
$this->pluginDirs = $this->getParam('pluginDirs', []);
}

protected function onNotFound(): callable
{
return static function (string $cmd, Application $app) {
$aliases = $app->getParam('aliases', []);

// - is an command alias.
if ($aliases && isset($aliases[$cmd])) {
$realCmd = $aliases[$cmd];

$app->notice("input command is alias name, will redirect to the real command '$realCmd'");
$app->dispatch($realCmd);
return true;
}

// check custom scripts
$scripts = $app->getParam('scripts', []);
if (!$scripts || !isset($scripts[$cmd])) {
// - run plugin
if ($app->isPlugin($cmd)) {

}

// - call system command.
if ($cmd[0] === '\\') {
$cmd = substr($cmd, 1);
}

$cmdLine = $app->getInput()->getFullScript();
$app->notice("input command is not found, will call system command: $cmdLine");

// call system command
CmdRunner::new($cmdLine)->do(true);
return true;
}

// - run custom scripts.
/** @see \Inhere\Kite\Console\Command\RunCommand::execute() */
$app->note("command not found, redirect to run script: $cmd");

$args = $app->getInput()->getArgs();
$args = array_merge([$cmd], $args);

$app->getInput()->setArgs($args, true);
$app->dispatch('run');

return true;
};
}

/**
* @param string $name
*
* @return bool
*/
public function isPlugin(string $name): bool
protected function initAppRun(): void
{
if (\strpos($name, ' ') !== false) {
return false;
}
$plugDirs = $this->getParam('pluginDirs', []);

foreach ($this->pluginDirs as $dir) {
$filename = $dir . '/' . $name . '.php';
if (\is_file($filename)) {

}
}
$this->plugManager = new PluginManager($plugDirs);

return false;
$this->on(ConsoleEvent::ON_NOT_FOUND, new NotFoundListener());
}

/**
* @param string $name
* @return PluginManager
*/
public function runPlugin(string $name): bool
public function getPlugManager(): PluginManager
{
return true;
return $this->plugManager;
}
}
1 change: 0 additions & 1 deletion app/Console/Command/EnvCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use function is_scalar;
use function strpos;

/**
* Class DemoCommand
Expand Down
76 changes: 76 additions & 0 deletions app/Console/Controller/PluginController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php declare(strict_types=1);
/**
* This file is part of Kite.
*
* @link https://github.com/inhere
* @author https://github.com/inhere
* @license MIT
*/

namespace Inhere\Kite\Console\Controller;

use Inhere\Console\Controller;
use Inhere\Console\IO\Input;
use Inhere\Console\IO\Output;
use Inhere\Kite\Kite;

/**
* Class PluginController
*/
class PluginController extends Controller
{
protected static $name = 'plugin';

protected static $description = 'kite plugins manage tools';

/**
* @return string[]
*/
public static function aliases(): array
{
return ['plugins', 'plug'];
}

/**
* run an plugin by input name
*
* @arguments
* name The plugin name for run
*
* @param Input $input
* @param Output $output
*/
public function runCommand(Input $input, Output $output): void
{
$input->bindArgument('name', 0);
$name = $input->getRequiredArg('name');

$kpm = Kite::plugManager();
$kpm->run($name, $this->app);

$output->success('completed');
}

/**
* list all plugins dir and file information
*
* @options
* --only-files Only list all plugin names
*
* @param Input $input
* @param Output $output
*/
public function listCommand(Input $input, Output $output): void
{
$kpm = Kite::plugManager();
$opts = ['ucFirst' => false];

if (!$input->getBoolOpt('only-files')) {
$dirs = $kpm->getPluginDirs();
$output->aList($dirs, 'plugin dirs', $opts);
}

$files = $kpm->loadPluginFiles()->getPluginFiles();
$output->aList($files, 'Plugin Files', $opts);
}
}
86 changes: 86 additions & 0 deletions app/Console/Listener/NotFoundListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\Listener;

use Inhere\Kite\Common\CmdRunner;
use Inhere\Kite\Console\Application;

/**
* Class NotFoundListener
*
* @package Inhere\Kite\Console\Listener
*/
final class NotFoundListener
{
/**
* @param string $cmd
* @param Application $app
*
* @return bool
*/
public function __invoke(string $cmd, Application $app): bool
{
$aliases = $app->getParam('aliases', []);

// - is an command alias.
if ($aliases && isset($aliases[$cmd])) {
$realCmd = $aliases[$cmd];

$app->notice("input command is alias name, will redirect to the real command '$realCmd'");
$app->dispatch($realCmd);
return true;
}

// check custom scripts
$scripts = $app->getParam('scripts', []);
if (!$scripts || !isset($scripts[$cmd])) {
// - run plugin
if ($app->getPlugManager()->isPlugin($cmd)) {
$app->notice("input is an plugin name, will run plugin: $cmd");
$app->getPlugManager()->run($cmd, $app);
return true;
}

// - call system command.
$this->callSystemCmd($cmd, $app);
return true;
}

// - run custom scripts.
$this->runCustomScript($cmd, $app);
return true;
}

/**
* @param string $cmd
* @param Application $app
*/
private function callSystemCmd(string $cmd, Application $app): void
{
if ($cmd[0] === '\\') {
$cmd = substr($cmd, 1);
}

$cmdLine = $app->getInput()->getFullScript();
$app->notice("input command is not found, will call system command: $cmdLine");

// call system command
CmdRunner::new($cmdLine)->do(true);
}

/**
* @param string $cmd
* @param Application $app
*/
private function runCustomScript(string $cmd, Application $app): void
{
/** @see \Inhere\Kite\Console\Command\RunCommand::execute() */
$app->note("command not found, redirect to run script: $cmd");

$args = $app->getInput()->getArgs();
$args = array_merge([$cmd], $args);

$app->getInput()->setArgs($args, true);
$app->dispatch('run');
}
}
38 changes: 38 additions & 0 deletions app/Console/Plugin/AbstractPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php declare(strict_types=1);

namespace Inhere\Kite\Console\Plugin;

use Inhere\Kite\Console\Application;

/**
* Class AbstractPlugin
*
* @package Inhere\Kite\Plugin
*/
abstract class AbstractPlugin
{
/**
* @return array
*/
public function metadata(): array
{
return [
// 'author' => 'inhere',
// 'version' => '',
// 'desc' => '',
];
}

/**
* @param Application $app
*/
public function run(Application $app): void
{
$this->exec($app);
}

/**
* @param Application $app
*/
abstract public function exec(Application $app): void;
}
Loading

0 comments on commit d419548

Please sign in to comment.