View source
<?php
namespace Drupal\module_builder\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use DrupalCodeBuilder\Exception\SanityException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ProcessForm extends FormBase {
protected $messenger;
public function __construct($drupal_code_builder, MessengerInterface $messenger) {
$this->drupalCodeBuilder = $drupal_code_builder;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('module_builder.drupal_code_builder'), $container
->get('messenger'));
}
public function getFormId() {
return 'module_builder_process';
}
protected static function getCollectTask() {
return \Drupal::service('module_builder.drupal_code_builder')
->getTask('Collect');
}
public function buildForm(array $form, FormStateInterface $form_state) {
try {
$task_handler_report = $this->drupalCodeBuilder
->getTask('ReportHookDataFolder');
$task_report_summary = $this->drupalCodeBuilder
->getTask('ReportSummary');
} catch (SanityException $e) {
if ($e
->getFailedSanityLevel() == 'data_directory_exists') {
$this->messenger
->addError($this
->t("The hooks data directory does not exist, or is not writeable. Check your settings and your filesystem."));
return $form;
}
}
$last_update = $task_handler_report
->lastUpdatedDate();
$directory = \DrupalCodeBuilder\Factory::getEnvironment()
->getHooksDirectory();
$form['intro'] = array(
'#markup' => '<p>' . t("Module Builder analyses your site's code to find data about Drupal components such as hooks, plugins, tagged services, and more." . ' ' . "This processed data is stored in your local filesystem." . ' ' . "You should update the code analysis when updating site code, or updating Module Builder or Drupal Code Builder.") . '</p>',
);
$form['analyse'] = [
'#type' => 'fieldset',
'#title' => "Perform analysis",
];
$form['analyse']['last_update'] = array(
'#markup' => '<p>' . ($last_update ? t('Your last data update was %date.', array(
'%date' => \Drupal::service('date.formatter')
->format($last_update, 'large'),
)) : t("The site's code has not yet been analysed.")) . '</p>',
);
$form['analyse']['submit'] = array(
'#type' => 'submit',
'#value' => $last_update ? t('Update code analysis') : t('Perform code analysis'),
);
if ($last_update) {
try {
$analysis_data = $task_report_summary
->listStoredData();
} catch (\DrupalCodeBuilder\Exception\StorageException $e) {
$this
->messenger()
->addError($e
->getMessage());
return $form;
}
$form['results'] = [
'#type' => 'fieldset',
'#title' => "Analysis results",
];
$form['results']['text'] = array(
'#markup' => '<p>' . t('You have the following data saved in %dir: ', array(
'%dir' => $directory,
)) . '</p>',
);
foreach ($analysis_data as $type => $type_data) {
$form['results'][$type] = [
'#type' => 'details',
'#title' => "{$type_data['label']} ({$type_data['count']})",
'#open' => FALSE,
];
if (is_array(reset($type_data['list']))) {
$items = [];
foreach ($type_data['list'] as $group_name => $group_items) {
$items = array_merge($items, array_keys($group_items));
}
}
else {
$items = array_keys($type_data['list']);
}
$form['results'][$type]['items'] = [
'#theme' => 'item_list',
'#items' => $items,
];
}
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$task_handler_collect = static::getCollectTask();
$job_list = $task_handler_collect
->getJobList();
$batch = array(
'title' => t('Analysing site code'),
'operations' => array(),
'file' => drupal_get_path('module', 'module_builder') . '/includes/module_builder.admin.inc',
'finished' => [
get_class($this),
'batchFinished',
],
);
$job_batches = array_chunk($job_list, 10);
foreach ($job_batches as $job_batch) {
$batch['operations'][] = [
[
get_class($this),
'batchOperation',
],
[
$job_batch,
],
];
}
batch_set($batch);
}
public static function batchOperation($job_batch, &$context) {
$task_handler_collect = static::getCollectTask();
try {
$task_handler_collect
->collectComponentDataIncremental($job_batch, $context['results']);
} catch (\Exception $e) {
$context['results']['errors'][] = $e
->getMessage();
return;
}
$labels = [];
$message_pieces = [];
foreach ($job_batch as $job) {
if (isset($job['item_label'])) {
$labels[$job['process_label']][] = $job['item_label'];
}
else {
$labels[$job['process_label']] = NULL;
}
}
foreach ($labels as $process_label => $item_labels) {
if (is_null($item_labels)) {
$message_pieces[] = $process_label;
}
else {
$message_pieces[] = t('@task for @items', array(
'@task' => $process_label,
'@items' => implode(', ', $item_labels),
));
}
}
$context['message'] = t("Processed: @list.", array(
'@list' => implode(', ', $message_pieces),
));
}
public static function batchFinished($success, $results, $operations) {
if (isset($results['errors'])) {
foreach ($results['errors'] as $error_message) {
\Drupal::messenger()
->addError(t("Error with a code analysis process: @message", [
'@message' => $error_message,
]));
}
}
\Drupal::messenger()
->addStatus(t("Finished analysing site code. See results below for details."));
}
}