Skip to content

Commit

Permalink
Commands in container (#2604)
Browse files Browse the repository at this point in the history
* [generate:post:update] New Command

* Fixed services call as option in some commands

* Language separation

* Revert "Language separation"

This reverts commit e4d45c9.

* Register cache commands in Container #1

* Revert "Register cache commands in Container #1"

This reverts commit dee0362.

* Register chain commands in Container #1

* Register chain commands in Container #2

* Register chain commands in Container #3

* Register chain commands in Container #4

* Register chain commands in Container #5

* Register chain commands in Container #5

* Register chain commands in Container #6

* Register chain commands in Container #7

* Register config commands in Container #1

* Register config commands in Container #2

* Register config commands in Container #3

* Register config commands in Container #4

* Register config commands in Container #5

* Register config commands in Container #6

* Register config commands in Container #7

* Register config commands in Container #8

* Register config commands in Container #9

* Register config commands in Container #10

* Register config commands in Container #11

* Register config commands in Container #12

* Register config commands in Container #13

* Register config commands in Container #15
enzolutions authored and jmolivas committed Aug 23, 2016
1 parent 25066a2 commit 2dc1954
Showing 11 changed files with 202 additions and 92 deletions.
19 changes: 10 additions & 9 deletions config/services/chain.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#services:
# chain:
# class: Drupal\Console\Command\Chain\ChainCommand
# tags:
# - { name: console.command }
# chain_debug:
# class: Drupal\Console\Command\Chain\ChainDebugCommand
# tags:
# - { name: console.command }
services:
chain:
class: Drupal\Console\Command\Chain\ChainCommand
arguments: ['@console.file_util', '@console.chain_queue', '@console.configuration_manager', '@app.root']
tags:
- { name: console.command }
chain_debug:
class: Drupal\Console\Command\Chain\ChainDebugCommand
tags:
- { name: console.command }
56 changes: 31 additions & 25 deletions config/services/config.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
#services:
# config_debug:
# class: Drupal\Console\Command\Config\DebugCommand
# tags:
# - { name: console.command }
# config_delete:
# class: Drupal\Console\Command\Config\DeleteCommand
# tags:
# - { name: console.command }
# config_diff:
# class: Drupal\Console\Command\Config\DiffCommand
# tags:
# - { name: console.command }
# config_edit:
# class: Drupal\Console\Command\Config\EditCommand
# tags:
# - { name: console.command }
# config_export:
# class: Drupal\Console\Command\Config\ExportCommand
# tags:
# - { name: console.command }
# config_export_content_type:
# class: Drupal\Console\Command\Config\ExportContentTypeCommand
# tags:
# - { name: console.command }
services:
config_debug:
class: Drupal\Console\Command\Config\DebugCommand
arguments: ['@config.factory', '@config.storage']
tags:
- { name: console.command }
config_delete:
class: Drupal\Console\Command\Config\DeleteCommand
arguments: ['@config.factory', '@config.storage', '@config.storage.sync']
tags:
- { name: console.command }
config_diff:
class: Drupal\Console\Command\Config\DiffCommand
arguments: ['@config.storage', '@config.manager']
tags:
- { name: console.command }
config_edit:
class: Drupal\Console\Command\Config\EditCommand
arguments: ['@config.factory', '@config.storage', '@console.configuration_manager']
tags:
- { name: console.command }
config_export:
class: Drupal\Console\Command\Config\ExportCommand
arguments: ['@config.manager']
tags:
- { name: console.command }
config_export_content_type:
class: Drupal\Console\Command\Config\ExportContentTypeCommand
arguments: ['@entity_type.manager', '@config.storage']
tags:
- { name: console.command }
# config_export_view:
# class: Drupal\Console\Command\Config\ExportViewCommand
# tags:
15 changes: 15 additions & 0 deletions services.yml
Original file line number Diff line number Diff line change
@@ -10,6 +10,21 @@ services:
# console.command_dependencies:
# class: Drupal\Console\Command\CommandDependencies
# arguments: ["@console.annotation_file_cache"]
console.site:
class: Drupal\Console\Utils\Site
console.file_util:
class: Drupal\Console\Utils\File
# translator:
# class: Drupal\Console\Utils\Translator
# arguments: ["@parser", "@filesystem"]
# console.translator:
# class: Drupal\Console\Utils\Translator
# showfile:
# class: Drupal\Console\Utils\ShowFile
# arguments: ["@site", "@translator"]
# shell_process:
# class: Drupal\Console\Utils\ShellProcess
# arguments: ["@site", '@config']
console.validator:
class: Drupal\Console\Utils\Validator
console.drupal_api:
38 changes: 30 additions & 8 deletions src/Command/Chain/ChainCommand.php
Original file line number Diff line number Diff line change
@@ -13,11 +13,14 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Parser;
use Drupal\Console\Command\Shared\ChainFilesTrait;
use Drupal\Console\Command\Shared\InputTrait;
use Drupal\Console\Style\DrupalStyle;
use Drupal\Console\Command\Shared\CommandTrait;


/**
* Class ChainCommand
* @package Drupal\Console\Command\Chain
@@ -28,6 +31,27 @@ class ChainCommand extends Command
use ChainFilesTrait;
use InputTrait;


protected $fileUtil;
protected $chainQueue;
protected $configurationManager;
protected $appRoot;

/**
* ChainCommand constructor.
* @param $fileUtil
* @param $chainQueue
* @param $configurationManager
* @param $appRoot
*/
public function __construct($fileUtil, $chainQueue, $configurationManager, $appRoot) {
$this->fileUtil = $fileUtil;
$this->chainQueue = $chainQueue;
$this->configurationManager = $configurationManager;
$this->appRoot = $appRoot;
parent::__construct();
}

/**
* {@inheritdoc}
*/
@@ -57,7 +81,6 @@ protected function interact(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$file = $input->getOption('file');
$fileUtil = $this->getApplication()->getContainerHelper()->get('file_util');

if (!$file) {
$files = $this->getChainFiles(true);
@@ -68,7 +91,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
);
}

$file = $fileUtil->calculateRealPath($file);
$file = $this->fileUtil->calculateRealPath($file);
$input->setOption('file', $file);

$chainContent = file_get_contents($file);
@@ -113,16 +136,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
$learning = $input->hasOption('learning')?$input->getOption('learning'):false;

$file = $input->getOption('file');
$fileUtil = $this->getApplication()->getContainerHelper()->get('file_util');
$fileSystem = $this->getApplication()->getContainerHelper()->get('filesystem');

if (!$file) {
$io->error($this->trans('commands.chain.messages.missing_file'));

return 1;
}

$file = $fileUtil->calculateRealPath($file);
$fileSystem = new Filesystem();

$file = $this->fileUtil->calculateRealPath($file);

if (!$fileSystem->exists($file)) {
$io->error(
@@ -230,7 +253,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$placeholderResolver = new RegexPlaceholderResolver($inlinePlaceHolderData, '%{{', '}}');
$chainContent = $placeholderResolver->resolvePlaceholder($chainContent);

$parser = $this->getApplication()->getContainerHelper()->get('parser');
$parser = new Parser();
$configData = $parser->parse($chainContent);

$commands = [];
@@ -259,8 +282,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
}

$this->get('chain_queue')
->addCommand(
$this->chainQueue->addCommand(
$command['command'],
$moduleInputs,
$interactive,
27 changes: 19 additions & 8 deletions src/Command/Config/DebugCommand.php
Original file line number Diff line number Diff line change
@@ -12,12 +12,26 @@
use Symfony\Component\Console\Output\OutputInterface;
use Drupal\Component\Serialization\Yaml;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;

class DebugCommand extends Command
{
use ContainerAwareCommandTrait;
use CommandTrait;

protected $configFactory;
protected $configStorage;

/**
* ChainCommand constructor.
* @param $fileUtil
*/
public function __construct($configFactory, $configStorage) {
$this->configFactory = $configFactory;
$this->configStorage = $configStorage;
parent::__construct();
}

/**
* {@inheritdoc}
*/
@@ -53,8 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
*/
private function getAllConfigurations(DrupalStyle $io)
{
$configFactory = $this->getDrupalService('config.factory');
$names = $configFactory->listAll();
$names = $this->configFactory->listAll();
$tableHeader = [
$this->trans('commands.config.debug.arguments.name'),
];
@@ -74,14 +87,12 @@ private function getAllConfigurations(DrupalStyle $io)
*/
private function getConfigurationByName(DrupalStyle $io, $config_name)
{
$configStorage = $this->getDrupalService('config.storage');

if ($configStorage->exists($config_name)) {
if ($this->configStorage->exists($config_name)) {
$tableHeader = [
$config_name,
];

$configuration = $configStorage->read($config_name);
$configuration = $this->configStorage->read($config_name);
$configurationEncoded = Yaml::encode($configuration);
$tableRows = [];
$tableRows[] = [
28 changes: 21 additions & 7 deletions src/Command/Config/DeleteCommand.php
Original file line number Diff line number Diff line change
@@ -6,20 +6,36 @@

namespace Drupal\Console\Command\Config;

use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Core\Config\FileStorage;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Exception\RuntimeException;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;

class DeleteCommand extends Command
{
use ContainerAwareCommandTrait;
use CommandTrait;

protected $allConfig = [];
protected $configFactory;
protected $configStorage;
protected $configStorageSync;

/**
* ChainCommand constructor.
* @param $configFactory
* @param $configStorage
* @param $configStorageSync
*/
public function __construct($configFactory , $configStorage, $configStorageSync ) {
$this->configFactory = $configFactory;
$this->configStorage = $configStorage;
$this->configStorageSync = $configStorageSync;
parent::__construct();
}

/**
* {@inheritdoc}
@@ -88,9 +104,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}

$configStorage = ('active' === $type) ?
$this->getDrupalService('config.storage') :
\Drupal::service('config.storage.sync');
$configStorage = ('active' === $type) ? $this->configStorage : $this->configStorageSync;

if (!$configStorage) {
$io->error($this->trans('commands.config.delete.errors.config-storage'));
@@ -148,7 +162,7 @@ private function getAllConfigNames()
return $this->allConfig;
}

foreach ($this->getDrupalService('config.factory')->listAll() as $name) {
foreach ($this->configFactory->listAll() as $name) {
$this->allConfig[] = $name;
}

@@ -177,7 +191,7 @@ private function yieldAllConfig()
private function removeConfig($name)
{
try {
$this->getDrupalService('config.factory')->getEditable($name)->delete();
$this->configFactory->getEditable($name)->delete();
} catch (\Exception $e) {
throw new RuntimeException($e->getMessage());
}
24 changes: 18 additions & 6 deletions src/Command/Config/DiffCommand.php
Original file line number Diff line number Diff line change
@@ -13,12 +13,26 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;

class DiffCommand extends Command
{
use ContainerAwareCommandTrait;
use CommandTrait;

protected $configStorage;
protected $configManager;

/**
* ChainCommand constructor.
* @param $fileUtil
*/
public function __construct($configStorage, $configManager ) {
$this->configStorage = $configStorage;
$this->configManager = $configManager;
parent::__construct();
}

/**
* A static array map of operations -> color strings.
*
@@ -82,13 +96,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$io = new DrupalStyle($input, $output);
$directory = $input->getArgument('directory');
$source_storage = new FileStorage($directory);
$active_storage = $this->getDrupalService('config.storage');
$config_manager = $this->getDrupalService('config.manager');

if ($input->getOption('reverse')) {
$config_comparer = new StorageComparer($source_storage, $active_storage, $config_manager);
$config_comparer = new StorageComparer($source_storage, $this->configStorage, $this->configManager);
} else {
$config_comparer = new StorageComparer($active_storage, $source_storage, $config_manager);
$config_comparer = new StorageComparer($this->configStorage, $source_storage, $this->configManager);
}
if (!$config_comparer->createChangelist()->hasChanges()) {
$output->writeln($this->trans('commands.config.diff.messages.no-changes'));
34 changes: 23 additions & 11 deletions src/Command/Config/EditCommand.php
Original file line number Diff line number Diff line change
@@ -16,12 +16,27 @@
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Drupal\Component\Serialization\Yaml;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;

class EditCommand extends Command
{
use ContainerAwareCommandTrait;
use CommandTrait;

protected $configFactory;
protected $configStorage;
protected $configurationManager;

/**
* ChainCommand constructor.
* @param $fileUtil
*/
public function __construct($configFactory , $configStorage, $configurationManager ) {
$this->configFactory = $configFactory;
$this->configStorage = $configStorage;
$this->configurationManager = $configurationManager;
parent::__construct();
}
/**
* {@inheritdoc}
*/
@@ -51,8 +66,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$configName = $input->getArgument('config-name');
$editor = $input->getArgument('editor');
$config = $this->getDrupalService('config.factory')->getEditable($configName);
$configSystem = $this->getDrupalService('config.factory')->get('system.file');
$config = $this->configFactory->getEditable($configName);
$configSystem = $this->configFactory->get('system.file');
$temporaryDirectory = $configSystem->get('path.temporary') ?: '/tmp';
$configFile = $temporaryDirectory.'/config-edit/'.$configName.'.yml';
$ymlFile = new Parser();
@@ -97,8 +112,7 @@ protected function interact(InputInterface $input, OutputInterface $output)

$configName = $input->getArgument('config-name');
if (!$configName) {
$configFactory = $this->getDrupalService('config.factory');
$configNames = $configFactory->listAll();
$configNames = $this->configFactory->listAll();
$configName = $io->choice(
'Choose a configuration',
$configNames
@@ -115,9 +129,8 @@ protected function interact(InputInterface $input, OutputInterface $output)
*/
protected function getYamlConfig($config_name)
{
$configStorage = $this->getDrupalService('config.storage');
if ($configStorage->exists($config_name)) {
$configuration = $configStorage->read($config_name);
if ($this->configStorage->exists($config_name)) {
$configuration = $this->configStorage->read($config_name);
$configurationEncoded = Yaml::encode($configuration);
}

@@ -129,8 +142,7 @@ protected function getYamlConfig($config_name)
*/
protected function getEditor()
{
$app = $this->getApplication();
$config = $app->getConfig();
$config = $this->configurationManager->getConfiguration();
$editor = $config->get('application.editor', 'vi');

if ($editor != '') {
26 changes: 19 additions & 7 deletions src/Command/Config/ExportCommand.php
Original file line number Diff line number Diff line change
@@ -13,12 +13,25 @@
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Command\Command;
use Drupal\Console\Command\Shared\ContainerAwareCommandTrait;
use Symfony\Component\Filesystem\Filesystem;
use Drupal\Console\Command\Shared\CommandTrait;
use Drupal\Console\Style\DrupalStyle;

class ExportCommand extends Command
{
use ContainerAwareCommandTrait;
use CommandTrait;

protected $configManager;

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

/**
* {@inheritdoc}
*/
@@ -71,10 +84,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

try {
$configManager = $this->getDrupalService('config.manager');
// Get raw configuration data without overrides.
foreach ($configManager->getConfigFactory()->listAll() as $name) {
$configData = $configManager->getConfigFactory()->get($name)->getRawData();
foreach ($this->configManager->getConfigFactory()->listAll() as $name) {
$configData = $this->configManager->getConfigFactory()->get($name)->getRawData();
$configName = sprintf('%s.yml', $name);
$ymlData = Yaml::encode($configData);

@@ -88,9 +100,9 @@ protected function execute(InputInterface $input, OutputInterface $output)

$configFileName = sprintf('%s/%s', $directory, $configName);

$fs = $this->get('filesystem');
$fileSystem = new Filesystem();
try {
$fs->mkdir($directory);
$fileSystem->mkdir($directory);
} catch (IOExceptionInterface $e) {
$io->error(
sprintf(
15 changes: 11 additions & 4 deletions src/Command/Config/ExportContentTypeCommand.php
Original file line number Diff line number Diff line change
@@ -27,6 +27,17 @@ class ExportContentTypeCommand extends Command
protected $configStorage;
protected $configExport;

/**
* ChainCommand constructor.
* @param $entityTypeManager
* @param $configStorage
*/
public function __construct($entityTypeManager, $configStorage) {
$this->entityTypeManager = $entityTypeManager;
$this->configStorage = $configStorage;
parent::__construct();
}

/**
* {@inheritdoc}
*/
@@ -99,10 +110,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);


$this->entityTypeManager = $this->getDrupalService('entity_type.manager');
$this->configStorage = $this->getDrupalService('config.storage');

$module = $input->getOption('module');
$contentType = $input->getArgument('content-type');
$optionalConfig = $input->getOption('optional-config');
12 changes: 5 additions & 7 deletions src/Command/Shared/ChainFilesTrait.php
Original file line number Diff line number Diff line change
@@ -17,12 +17,10 @@ trait ChainFilesTrait
{
private function getChainFiles($onlyFiles = false)
{
$config = $this->getApplication()->getConfig();

$directories = [
$config->getUserHomeDir() . DIRECTORY_SEPARATOR . '.console'. DIRECTORY_SEPARATOR .'chain',
$this->getApplication()->getSite()->getSiteRoot() . DIRECTORY_SEPARATOR . 'console'. DIRECTORY_SEPARATOR .'chain',
$this->getApplication()->getSite()->getSiteRoot() . DIRECTORY_SEPARATOR . '.console'. DIRECTORY_SEPARATOR .'chain',
$this->configurationManager->getHomeDirectory() . DIRECTORY_SEPARATOR . '.console'. DIRECTORY_SEPARATOR .'chain',
$this->appRoot . DIRECTORY_SEPARATOR . 'console'. DIRECTORY_SEPARATOR .'chain',
$this->appRoot . DIRECTORY_SEPARATOR . '.console'. DIRECTORY_SEPARATOR .'chain',
];

if ($this->get('site')->isInstalled()) {
@@ -32,7 +30,7 @@ private function getChainFiles($onlyFiles = false)
foreach ($modules as $module) {
$modulePath = sprintf(
'%s/%s/console/chain/',
$this->getApplication()->getSite()->getSiteRoot(),
$this->appRoot,
$module->getPath()
);

@@ -43,7 +41,7 @@ private function getChainFiles($onlyFiles = false)
foreach ($themes as $theme) {
$themePath = sprintf(
'%s/%s/console/chain',
$this->getApplication()->getSite()->getSiteRoot(),
$this->appRoot,
$theme->getPath()
);
if (is_dir($themePath)) {

0 comments on commit 2dc1954

Please sign in to comment.