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

[console] Implement statistics feature #342

Merged
merged 17 commits into from May 28, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -52,7 +52,8 @@
"symfony/yaml": "~2.8|~3.0",
"twig/twig": "^1.23.1",
"webflo/drupal-finder": "^1.0",
"webmozart/path-util": "^2.3"
"webmozart/path-util": "^2.3",
"guzzlehttp/guzzle": "~6.1"
},
"minimum-stability": "dev",
"prefer-stable": true,
@@ -1,4 +1,8 @@
application:
statistics:
enabled: false
last-attempted: ~
times-attempted: 0
language: 'en'
autowire:
commands:
@@ -105,6 +105,7 @@ services:
# DrupalConsoleCore Generators
console.init_generator:
class: Drupal\Console\Core\Generator\InitGenerator
arguments: ['@console.configuration_manager']
tags:
- { name: drupal.generator }
console.site_alias_generator:
@@ -2,7 +2,9 @@
namespace Drupal\Console\Core;
use Drupal\Console\Core\EventSubscriber\SendStatisticsListener;
use Drupal\Console\Core\EventSubscriber\RemoveMessagesListener;
use Drupal\Console\Core\EventSubscriber\SaveStatisticsListener;
use Drupal\Console\Core\EventSubscriber\ShowGenerateCountCodeLinesListener;
use Drupal\Console\Core\Utils\TranslatorManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -305,6 +307,21 @@ private function registerEvents()
)
);
$dispatcher->addSubscriber(
new SaveStatisticsListener(
$this->container->get('console.count_code_lines'),
$this->container->get('console.configuration_manager'),
$this->container->get('console.translator_manager')
)
);
$dispatcher->addSubscriber(
new SendStatisticsListener(
$this->container->get('console.configuration_manager'),
$this->container->get('console.translator_manager')
)
);
$dispatcher->addSubscriber(
new RemoveMessagesListener(
$this->container->get('console.message_manager')
@@ -709,7 +726,9 @@ public function getData($filterNamespaces = null, $excludeNamespaces = [], $excl
$namespaces = array_diff($namespaces, $excludeNamespaces);
// filter namespaces if available
if($filterNamespaces) $namespaces = array_intersect($namespaces, $filterNamespaces);
if ($filterNamespaces) {
$namespaces = array_intersect($namespaces, $filterNamespaces);
}
foreach ($namespaces as $namespace) {
$commands = $this->all($namespace);
@@ -721,8 +740,8 @@ public function getData($filterNamespaces = null, $excludeNamespaces = [], $excl
foreach ($commands as $command) {
// Exclude command if is a chain command and was requested to exclude chain commands
if($excludeChainCommands && $command instanceof ChainCustomCommand) {
continue;
if ($excludeChainCommands && $command instanceof ChainCustomCommand) {
continue;
}
if (method_exists($command, 'getModule')) {
@@ -740,9 +759,11 @@ public function getData($filterNamespaces = null, $excludeNamespaces = [], $excl
}
// Remove namepsaces without commands
$namespaces = array_filter($namespaces, function($namespace) use( $data) {
return count($data['commands'][$namespace]) > 0;
});
$namespaces = array_filter(
$namespaces, function ($namespace) use ($data) {
return count($data['commands'][$namespace]) > 0;
}
);
$input = $this->getDefinition();
$options = [];
@@ -885,7 +906,8 @@ public function getContainer()
/**
* Add Drupal system messages.
*/
protected function addDrupalMessages($messageManager) {
protected function addDrupalMessages($messageManager)
{
if (function_exists('drupal_get_messages')) {
$drupalMessages = drupal_get_messages();
foreach ($drupalMessages as $type => $messages) {
@@ -906,13 +928,14 @@ protected function addDrupalMessages($messageManager) {
* @return string
* Name of the method
*/
protected function getMessageMethod($type) {
protected function getMessageMethod($type)
{
$methodName = 'info';
switch ($type) {
case 'error':
case 'warning':
$methodName = $type;
break;
case 'error':
case 'warning':
$methodName = $type;
break;
}
return $methodName;
@@ -56,7 +56,8 @@ class InitCommand extends Command
'sites' => false,
'learning' => false,
'generate_inline' => false,
'generate_chain' => false
'generate_chain' => false,
'statistics' => true
];
private $directories = [
@@ -192,6 +193,27 @@ protected function interact(InputInterface $input, OutputInterface $output)
);
$input->setOption('autocomplete', $autocomplete);
}
$this->getIo()->commentBlock(
sprintf(
$this->trans('commands.init.messages.statistics'),
sprintf(
'%sconfig.yml',
$this->configurationManager->getConsoleDirectory()
)
)
);
$this->configParameters['statistics'] = $this->getIo()->confirm(
$this->trans('commands.init.questions.statistics'),
true
);
if ($this->configParameters['statistics']) {
$this->getIo()->commentBlock(
$this->trans('commands.init.messages.statistics-disable')
);
}
}
/**
@@ -269,13 +291,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
$process->stop();
}
$this->generator->generate([
'user_home' => $this->configurationManager->getConsoleDirectory(),
'executable_name' => $executableName,
'override' => $override,
'destination' => $destination,
'config_parameters' => $this->configParameters,
]);
$this->generator->generate(
[
'user_home' => $this->configurationManager->getConsoleDirectory(),
'executable_name' => $executableName,
'override' => $override,
'destination' => $destination,
'config_parameters' => $this->configParameters,
]
);
$this->getIo()->writeln($this->trans('application.messages.autocomplete'));
@@ -82,6 +82,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
$settingName = $input->getArgument('name');
$settingValue = $input->getArgument('value');
// Reset the default values ​​of the statistics.
if ($settingName == 'statistics.enabled') {
$this->configurationManager->updateConfigGlobalParameter(
'statistics.last-attempted',
null
);
$this->configurationManager->updateConfigGlobalParameter(
'statistics.times-attempted',
0
);
}
$userConfigFile = sprintf(
'%s/.console/config.yml',
$this->configurationManager->getHomeDirectory()
@@ -111,6 +123,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
}
$parents = array_merge(['application'], explode(".", $settingName));
// Change the value type if it is boolean.
$settingValue = json_decode($settingValue) === null ? $settingValue : json_decode($settingValue);
$this->nestedArray->setValue(
$userConfigFileParsed,
@@ -163,6 +177,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
return 1;
}
$settingValue = is_bool($settingValue) ? $settingValue ? 'true' : 'false' : $settingValue;
$this->getIo()->success(
sprintf(
$this->trans('commands.settings.set.messages.success'),
@@ -0,0 +1,119 @@
<?php
/**
* @file
* Contains \Drupal\Console\Core\EventSubscriber\SaveStatisticsListener.
*/
namespace Drupal\Console\Core\EventSubscriber;
use Drupal\Console\Core\Command\Chain\ChainCustomCommand;
use Drupal\Console\Core\Utils\ConfigurationManager;
use Drupal\Console\Core\Utils\CountCodeLines;
use Drupal\Console\Core\Utils\TranslatorManagerInterface;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Filesystem\Filesystem;
/**
* Class SaveStatisticsListener
*
* @package Drupal\Console\Core\EventSubscriber
*/
class SaveStatisticsListener implements EventSubscriberInterface
{
/**
* @var ShowGenerateChainListener
*/
protected $countCodeLines;
/**
* @var ConfigurationManager
*/
protected $configurationManager;
/**
* @var TranslatorManagerInterface
*/
protected $translator;
/**
* FileSystem $fs
*/
protected $fs;
/**
* SaveStatisticsListener constructor.
*
* @param CountCodeLines $countCodeLines
* @param ConfigurationManager $configurationManager
* @param TranslatorManagerInterface $translator
*/
public function __construct(
CountCodeLines $countCodeLines,
ConfigurationManager $configurationManager,
TranslatorManagerInterface $translator
) {
$this->countCodeLines = $countCodeLines;
$this->configurationManager = $configurationManager;
$this->translator = $translator;
$this->fs = new Filesystem();
}
/**
* @param ConsoleTerminateEvent $event
*/
public function saveStatistics(ConsoleTerminateEvent $event)
{
if ($event->getExitCode() != 0) {
return;
}
$configGlobalAsArray = $this->configurationManager->getConfigGlobalAsArray();
//Validate if the config is enable.
if (is_null($configGlobalAsArray) || !$configGlobalAsArray['application']['statistics']['enabled']) {
return;
}
//Check that the namespace starts with 'Drupal\Console'.
$class = new \ReflectionClass($event->getCommand());
if (strpos($class->getNamespaceName(), "Drupal\Console") !== 0) {
return;
}
//Validate if the command is not a custom chain command.
if ($event->getCommand() instanceof ChainCustomCommand) {
return;
}
$path = $path = sprintf(
'%s/.console/stats/',
$this->configurationManager->getHomeDirectory()
);
$information = $event->getCommand()->getName() . ',' . $this->translator->getLanguage();
$countCodeLines = $this->countCodeLines->getCountCodeLines();
if ($countCodeLines > 0) {
$information = $information . ',' . $countCodeLines;
}
$this->fs->appendToFile(
$path . date('Y-m-d') . '.csv',
$information . PHP_EOL
);
}
/**
* @{@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [ConsoleEvents::TERMINATE => 'saveStatistics'];
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.