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

Lumen console to artisan #2202

Merged
merged 11 commits into from
Nov 14, 2017
14 changes: 13 additions & 1 deletion app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,19 @@ class Kernel extends ConsoleKernel
* @var array
*/
protected $commands = [
Commands\RoutesCommand::class
Commands\RoutesCommand::class,
\Ushahidi\Console\Command\ConfigSet::class,
\Ushahidi\Console\Command\ConfigGet::class,
\Ushahidi\Console\Command\DataproviderList::class,
\Ushahidi\Console\Command\DataproviderIncoming::class,
\Ushahidi\Console\Command\DataproviderOutgoing::class,
\Ushahidi\Console\Command\Import::class,
\Ushahidi\Console\Command\UserCreate::class,
\Ushahidi\Console\Command\UserDelete::class,
\Ushahidi\Console\Command\Notification::class,
\Ushahidi\Console\Command\PostExporter::class,
\Ushahidi\Console\Command\SavedSearch::class,
\Ushahidi\Console\Command\Webhook::class
];

/**
Expand Down
14 changes: 1 addition & 13 deletions bin/ushahidi
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
#!/usr/bin/env php
<?php

// Kohana requires that DOCROOT be defined. This script is expected to be run
// from the project root, which is the DOCROOT.
define('DOCROOT', getcwd() . DIRECTORY_SEPARATOR);

// Make Kohana available for the console commands.
require DOCROOT . 'application/kohana.php';

// Load the console application as a service.
$app = service('app.console');

// All commands are automatically injected when the service is loaded.
// Use `bin/ushahidi list` to show all available commands.
$app->run();
include(__DIR__.'/../artisan');
2 changes: 1 addition & 1 deletion src/App/Repository/ConfigRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function get($group)
// Merge defaults
$defaults = $this->getDefaults($group);

$config = array_merge_recursive($defaults, $config);
$config = array_replace_recursive($defaults, $config);

return $this->getEntity(['id' => $group] + $config);
}
Expand Down
32 changes: 0 additions & 32 deletions src/Console/Application.php

This file was deleted.

73 changes: 0 additions & 73 deletions src/Console/Command.php

This file was deleted.

62 changes: 39 additions & 23 deletions src/Console/Command/ConfigGet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,69 @@

namespace Ushahidi\Console\Command;

use Ushahidi\Core\Usecase;
use Ushahidi\Console\Command;
use Illuminate\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\TableHelper;
use Ushahidi\Core\Usecase;
use \Ushahidi\Factory\UsecaseFactory;

class ConfigGet extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'config:get';

/**
* The console command signature.
*
* @var string
*/
protected $signature = 'config:get {group}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Get config params';

/**
* @var Ushahidi\Core\Usecase\Usecase
* @todo support multiple entity types
*/
protected $usecase;

public function setUsecase(Usecase $usecase)
public function __construct()
{
$this->usecase = $usecase;
}
parent::__construct();

protected function configure()
{
$this
->setName('config:get')
->setDescription('Get config')
->addArgument('group', InputArgument::REQUIRED, 'group')
;
// @todo inject
$this->usecase = service('factory.usecase')
->get('config', 'read')
// Override authorizer for console
->setAuthorizer(service('authorizer.console'))
// Override formatter for console
->setFormatter(service('formatter.entity.console'));
}

// Execution router takes the action argument and uses it to reroute execution.
protected function execute(InputInterface $input, OutputInterface $output)
public function handle()
{
$group = $input->getArgument('group');
$group = $this->argument('group');

$this->usecase->setIdentifiers([ 'id' => $group ]);

$response = $this->usecase->interact();

// Format the response and output
$this->handleResponse($response, $output);
$this->handleResponse($response);
}

/**
* Override response handler to flatten array
*/
protected function handleResponse($response, OutputInterface $output)
protected function handleResponse($response)
{
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($response));
$result = [];
Expand All @@ -70,6 +84,8 @@ protected function handleResponse($response, OutputInterface $output)
}
$result[ join('.', $keys) ] = $leafValue;
}
return parent::handleResponse($result, $output);

// Format as table
$this->table(array_keys($result), [$result]);
}
}
75 changes: 47 additions & 28 deletions src/Console/Command/ConfigSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,63 @@

namespace Ushahidi\Console\Command;

use Ushahidi\Core\Usecase;
use Ushahidi\Console\Command;
use Illuminate\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Helper\TableHelper;
use Ushahidi\Core\Usecase;
use \Ushahidi\Factory\UsecaseFactory;

class ConfigSet extends Command
{
/**
* The console command name.
*
* @var string
*/
protected $name = 'config:set';

/**
* The console command signature.
*
* @var string
*/
protected $signature = 'config:set {group} {value} {--key=}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Set config params';

/**
* @var Ushahidi\Core\Usecase\Usecase
* @todo support multiple entity types
*/
protected $usecase;

public function setUsecase(Usecase $usecase)
public function __construct()
{
$this->usecase = $usecase;
}
parent::__construct();

protected function configure()
{
$this
->setName('config:set')
->setDescription('Set config')
->addArgument('group', InputArgument::REQUIRED, 'group')
->addArgument('value', InputArgument::REQUIRED, 'value or json of values')
->addOption('key', ['k'], InputOption::VALUE_OPTIONAL, 'key')
;
// @todo inject
$this->usecase = service('factory.usecase')
->get('config', 'update')
// Override authorizer for console
->setAuthorizer(service('authorizer.console'))
// Override formatter for console
->setFormatter(service('formatter.entity.console'));
}

// Execution router takes the action argument and uses it to reroute execution.
protected function execute(InputInterface $input, OutputInterface $output)
{
$group = $input->getArgument('group');
$key = $input->getOption('key');
$value = $input->getArgument('value');
/**
* Execute the console command.
*
* @return void
*/
public function handle()
{
$group = $this->argument('group');
$key = $this->option('key');
$value = $this->argument('value');

if ($key) {
$value = [
Expand All @@ -69,13 +86,13 @@ protected function execute(InputInterface $input, OutputInterface $output)
$response = $this->usecase->interact();

// Format the response and output
$this->handleResponse($response, $output);
$this->handleResponse($response);
}

/**
* Override response handler to flatten array
*/
protected function handleResponse($response, OutputInterface $output)
protected function handleResponse($response)
{
$iterator = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($response));
$result = [];
Expand All @@ -86,6 +103,8 @@ protected function handleResponse($response, OutputInterface $output)
}
$result[ join('.', $keys) ] = $leafValue;
}
return parent::handleResponse($result, $output);

// Format as table
$this->table(array_keys($result), [$result]);
}
}
Loading