Skip to content

Commit

Permalink
[console] Remove usage of expression builder on services registration. (
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas authored Nov 23, 2017
1 parent 5995a90 commit 3ab4b98
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 175 deletions.
1 change: 0 additions & 1 deletion config/services/rest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ services:
- '@?plugin.manager.rest'
- '@authentication_collector'
- '@config.factory'
- "@=container.hasParameter('serializer.formats') ? parameter('serializer.formats') : []"
- '@entity.manager'
tags:
- { name: drupal.command }
12 changes: 6 additions & 6 deletions services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ services:
arguments: ['@app.root', '@entity_type.manager', '@http_client']
console.create_node_data:
class: Drupal\Console\Utils\Create\NodeData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@=service("console.drupal_api").getBundles()']
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@console.drupal_api']
console.create_comment_data:
class: Drupal\Console\Utils\Create\CommentData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter']
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@console.drupal_api']
console.create_term_data:
class: Drupal\Console\Utils\Create\TermData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@=service("console.drupal_api").getVocabularies()']
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@console.drupal_api']
console.create_user_data:
class: Drupal\Console\Utils\Create\UserData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@=service("console.drupal_api").getRoles()']
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@console.drupal_api']
console.create_role_data:
class: Drupal\Console\Utils\Create\RoleData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter']
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@console.drupal_api']
console.create_vocabulary_data:
class: Drupal\Console\Utils\Create\VocabularyData
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter']
arguments: ['@entity_type.manager', '@entity_field.manager', '@date.formatter', '@console.drupal_api']
console.annotation_command_reader:
class: Drupal\Console\Annotations\DrupalCommandAnnotationReader
console.annotation_validator:
Expand Down
29 changes: 10 additions & 19 deletions src/Command/Rest/EnableCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Console\Core\Command\Command;
use Drupal\Console\Core\Command\ContainerAwareCommand;
use Drupal\Console\Annotations\DrupalCommand;
use Drupal\rest\RestResourceConfigInterface;
use Drupal\Console\Core\Style\DrupalStyle;
Expand All @@ -26,7 +26,7 @@
* extensionType = "module"
* )
*/
class EnableCommand extends Command
class EnableCommand extends ContainerAwareCommand
{
use RestTrait;

Expand All @@ -45,46 +45,36 @@ class EnableCommand extends Command
*/
protected $configFactory;

/**
* The available serialization formats.
*
* @var array
*/
protected $formats;

/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
* @var EntityManager
*/
protected $entityManager;

/**
* EnableCommand constructor.
*
* @param ResourcePluginManager $pluginManagerRest
* @param AuthenticationCollector $authenticationCollector
* @param ConfigFactory $configFactory
* @param array $formats
* The available serialization formats.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* @param ResourcePluginManager $pluginManagerRest
* @param AuthenticationCollector $authenticationCollector
* @param ConfigFactory $configFactory
* @param EntityManager $entity_manager
* The entity manager.
*/
public function __construct(
ResourcePluginManager $pluginManagerRest,
AuthenticationCollector $authenticationCollector,
ConfigFactory $configFactory,
array $formats,
EntityManager $entity_manager
) {
$this->pluginManagerRest = $pluginManagerRest;
$this->authenticationCollector = $authenticationCollector;
$this->configFactory = $configFactory;
$this->formats = $formats;
$this->entityManager = $entity_manager;
parent::__construct();
}


protected function configure()
{
$this
Expand Down Expand Up @@ -136,8 +126,9 @@ protected function execute(InputInterface $input, OutputInterface $output)

$format = $io->choice(
$this->trans('commands.rest.enable.arguments.formats'),
$this->formats
$this->container->getParameter('serializer.formats')
);

$io->writeln(
$this->trans('commands.rest.enable.messages.selected-format') . ' ' . $format
);
Expand Down
1 change: 0 additions & 1 deletion src/Command/Shared/RestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function getRestResources($rest_status = false)

$resources = $this->pluginManagerRest->getDefinitions();


$enabled_resources = array_combine(array_keys($config), array_keys($config));
$available_resources = ['enabled' => [], 'disabled' => []];

Expand Down
30 changes: 23 additions & 7 deletions src/Utils/Create/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Console\Utils\DrupalApi;

/**
* Class ContentNode
Expand All @@ -21,36 +22,52 @@
*/
abstract class Base
{
/* @var EntityTypeManagerInterface */
/**
* @var EntityTypeManagerInterface
*/
protected $entityTypeManager = null;

/* @var EntityFieldManagerInterface */
/**
* @var EntityFieldManagerInterface
*/
protected $entityFieldManager = null;

/* @var DateFormatterInterface */
/**
* @var DateFormatterInterface
*/
protected $dateFormatter = null;

/* @var array */
protected $users = [];

/* @var Random $random */
/**
* @var Random $random
*/
protected $random = null;

/**
* @var DrupalApi
*/
protected $drupalApi;

/**
* ContentNode constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
* @param EntityFieldManagerInterface $entityFieldManager
* @param DateFormatterInterface $dateFormatter
* @param DrupalApi $drupalApi
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityFieldManagerInterface $entityFieldManager,
DateFormatterInterface $dateFormatter
DateFormatterInterface $dateFormatter,
DrupalApi $drupalApi = null
) {
$this->entityTypeManager = $entityTypeManager;
$this->entityFieldManager = $entityFieldManager;
$this->dateFormatter = $dateFormatter;
$this->drupalApi = $drupalApi;
}

/**
Expand Down Expand Up @@ -82,8 +99,7 @@ protected function generateFieldSampleData($entity)
/* @var \Drupal\field\FieldConfigInterface[] $fields */
foreach ($fields as $field) {
$fieldName = $field->getFieldStorageDefinition()->getName();
$cardinality = $field->getFieldStorageDefinition()->getCardinality(
);
$cardinality = $field->getFieldStorageDefinition()->getCardinality();
if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$cardinality = rand(1, 5);
}
Expand Down
19 changes: 0 additions & 19 deletions src/Utils/Create/CommentData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,6 @@
*/
class CommentData extends Base
{
/**
* Comments constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
* @param EntityFieldManagerInterface $entityFieldManager
* @param DateFormatterInterface $dateFormatter
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityFieldManagerInterface $entityFieldManager,
DateFormatterInterface $dateFormatter
) {
parent::__construct(
$entityTypeManager,
$entityFieldManager,
$dateFormatter
);
}

/**
* @param $nid
* @param $limit
Expand Down
31 changes: 3 additions & 28 deletions src/Utils/Create/NodeData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,6 @@
*/
class NodeData extends Base
{
/* @var array */
protected $bundles = [];

/**
* Nodes constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
* @param EntityFieldManagerInterface $entityFieldManager
* @param DateFormatterInterface $dateFormatter
* @param array $bundles
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityFieldManagerInterface $entityFieldManager,
DateFormatterInterface $dateFormatter,
$bundles
) {
$this->bundles = $bundles;
parent::__construct(
$entityTypeManager,
$entityFieldManager,
$dateFormatter
);
}

/**
* @param $contentTypes
* @param $limit
Expand All @@ -60,13 +35,13 @@ public function create(
$language = LanguageInterface::LANGCODE_NOT_SPECIFIED
) {
$nodes = [];
$bundles = $this->drupalApi->getBundles();
for ($i=0; $i<$limit; $i++) {
$contentType = $contentTypes[array_rand($contentTypes)];
$node = $this->entityTypeManager->getStorage('node')->create(
[
'nid' => null,
'type' => $contentType,

'created' => REQUEST_TIME - mt_rand(0, $timeRange),
'uid' => $this->getUserID(),
'title' => $this->getRandom()->sentences(mt_rand(1, $titleWords), true),
Expand All @@ -83,7 +58,7 @@ public function create(
$node->save();
$nodes['success'][] = [
'nid' => $node->id(),
'node_type' => $this->bundles[$contentType],
'node_type' => $bundles[$contentType],
'title' => $node->getTitle(),
'created' => $this->dateFormatter->format(
$node->getCreatedTime(),
Expand All @@ -93,7 +68,7 @@ public function create(
];
} catch (\Exception $error) {
$nodes['error'][] = [
'node_type' => $this->bundles[$contentType],
'node_type' => $bundles[$contentType],
'title' => $node->getTitle(),
'error' => $error->getMessage()
];
Expand Down
22 changes: 0 additions & 22 deletions src/Utils/Create/RoleData.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,10 @@
*/
class RoleData extends Base
{
/**
* Roles constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
* @param EntityFieldManagerInterface $entityFieldManager
* @param DateFormatterInterface $dateFormatter
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityFieldManagerInterface $entityFieldManager,
DateFormatterInterface $dateFormatter
) {
parent::__construct(
$entityTypeManager,
$entityFieldManager,
$dateFormatter
);
}

/**
* Create and returns an array of new Roles.
*
* @param $roles
* @param $limit
* @param $password
* @param $timeRange
*
* @return array
*/
Expand Down
30 changes: 3 additions & 27 deletions src/Utils/Create/TermData.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,6 @@
*/
class TermData extends Base
{
/* @var array */
protected $vocabularies = [];

/**
* Terms constructor.
*
* @param EntityTypeManagerInterface $entityTypeManager
* @param EntityFieldManagerInterface $entityFieldManager
* @param DateFormatterInterface $dateFormatter
* @param array $vocabularies
*/
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
EntityFieldManagerInterface $entityFieldManager,
DateFormatterInterface $dateFormatter,
$vocabularies
) {
$this->vocabularies = $vocabularies;
parent::__construct(
$entityTypeManager,
$entityFieldManager,
$dateFormatter
);
}

/**
* Create and returns an array of new Terms.
*
Expand All @@ -58,6 +33,7 @@ public function create(
$limit,
$nameWords
) {
$siteVocabularies = $this->drupalApi->getVocabularies();
$terms = [];
for ($i=0; $i<$limit; $i++) {
$vocabulary = $vocabularies[array_rand($vocabularies)];
Expand All @@ -77,12 +53,12 @@ public function create(
$term->save();
$terms['success'][] = [
'tid' => $term->id(),
'vocabulary' => $this->vocabularies[$vocabulary],
'vocabulary' => $siteVocabularies[$vocabulary],
'name' => $term->getName(),
];
} catch (\Exception $error) {
$terms['error'][] = [
'vocabulary' => $this->vocabularies[$vocabulary],
'vocabulary' => $siteVocabularies[$vocabulary],
'name' => $term->getName(),
'error' => $error->getMessage()
];
Expand Down
Loading

0 comments on commit 3ab4b98

Please sign in to comment.