Skip to content

Commit

Permalink
[console] Create roles commands (#3588)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjuarez20 authored and jmolivas committed Nov 15, 2017
1 parent 8c14c1d commit 2ad8c7b
Show file tree
Hide file tree
Showing 8 changed files with 490 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/services/create.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ services:
arguments: ['@console.create_vocabulary_data']
tags:
- { name: drupal.command }
console.create_roles:
class: Drupal\Console\Command\Create\RolesCommand
arguments: ['@console.create_role_data']
tags:
- { name: drupal.command }
5 changes: 5 additions & 0 deletions config/services/debug.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,8 @@ services:
class: Drupal\Console\Command\Debug\FeaturesCommand
tags:
- { name: drupal.command }
console.roles_debug:
class: Drupal\Console\Command\Debug\RolesCommand
arguments: ['@console.drupal_api']
tags:
- { name: drupal.command }
6 changes: 6 additions & 0 deletions config/services/role.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
console.role_new:
class: Drupal\Console\Command\Role\NewCommand
arguments: ['@database', '@entity_type.manager', '@date.formatter', '@console.drupal_api', '@console.validator', '@console.string_converter']
tags:
- { name: drupal.command }
3 changes: 3 additions & 0 deletions services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ services:
console.create_user_data:
class: Drupal\Console\Utils\Create\UserData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@=service("console.drupal_api").getRoles()']
console.create_role_data:
class: Drupal\Console\Utils\Create\RoleData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter']
console.create_vocabulary_data:
class: Drupal\Console\Utils\Create\VocabularyData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter']
Expand Down
107 changes: 107 additions & 0 deletions src/Command/Create/RolesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Create\RolesCommand.
*/

namespace Drupal\Console\Command\Create;

use Drupal\Console\Utils\Create\RoleData;
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 Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Style\DrupalStyle;

/**
* Class RolesCommand
*
* @package Drupal\Console\Command\Create
*/
class RolesCommand extends Command
{
/**
* @var RoleData
*/
protected $createRoleData;

/**
* RolesCommand constructor.
*
* @param RoleData $createRoleData
*/
public function __construct(
RoleData $createRoleData
) {
$this->createRoleData = $createRoleData;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('create:roles')
->setDescription($this->trans('commands.create.roles.description'))
->addOption(
'limit',
null,
InputOption::VALUE_OPTIONAL,
$this->trans('commands.create.roles.options.limit')
)
->setAliases(['crr']);
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$limit = $input->getOption('limit');
if (!$limit) {
$limit = $io->ask(
$this->trans('commands.create.roles.questions.limit'),
5
);
$input->setOption('limit', $limit);
}
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$limit = $input->getOption('limit')?:5;

$roles = $this->createRoleData->create(
$limit
);

$tableHeader = [
$this->trans('commands.create.roles.messages.role-id'),
$this->trans('commands.create.roles.messages.role-name'),
];

if ($roles['success']) {
$io->table($tableHeader, $roles['success']);

$io->success(
sprintf(
$this->trans('commands.create.roles.messages.created-roles'),
$limit
)
);
}

return 0;
}
}
75 changes: 75 additions & 0 deletions src/Command/Debug/RolesCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* @file
* Contains \Drupal\Console\Command\Roles\DebugCommand.
*/

namespace Drupal\Console\Command\Debug;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Utils\DrupalApi;

/**
* Class RolesCommand
*
* @package Drupal\Console\Command\Debug
*/
class RolesCommand extends Command
{
/**
* @var DrupalApi
*/
protected $drupalApi;

/**
* DebugCommand constructor.
*
* @param DrupalApi $drupalApi
*/
public function __construct(
DrupalApi $drupalApi
) {
$this->drupalApi = $drupalApi;
parent::__construct();
}

/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('debug:roles')
->setDescription($this->trans('commands.debug.roles.description'))
->setAliases(['dusr']);
}

/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);

$roles = $this->drupalApi->getRoles();

$tableHeader = [
$this->trans('commands.debug.roles.messages.role-id'),
$this->trans('commands.debug.roles.messages.role-name'),
];

$tableRows = [];
foreach ($roles as $roleId => $role) {
$tableRows[] = [
$roleId,
$role
];
}

$io->table($tableHeader, $tableRows);
}
}
Loading

0 comments on commit 2ad8c7b

Please sign in to comment.