-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[console] Implement statistics feature (#342)
* [console] Implement statistics feature * [statistics] Change statistics config location * [statistics] Remove getStatisticsDirectory * [statistics] Use fgetcsv function * [statistics] Remove statistics after success response * [statistics] Add validation to custom commands. * [statistics] Remove var_dump from saveStatistics class * [statistics] Add validation is finder count is 0 * [config] Add statistics to console config * [configManager] Change getConfigGlobalAsArray function * [settings:set] Add missing message to success execution * [statistics] Add more condition to send info * [statistics] Change count-attempted to times-attempted * [statistics] Delete the error message for failed attempted * [statistics] Add validation if there is not config file * Add url to config.yml to send data
- Loading branch information
1 parent
4914556
commit c4d578d
Showing
12 changed files
with
559 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
application: | ||
statistics: | ||
enabled: false | ||
last-attempted: ~ | ||
times-attempted: 0 | ||
language: 'en' | ||
autowire: | ||
commands: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']; | ||
} | ||
} |
Oops, something went wrong.