View source
<?php
namespace Drupal\upgrade_rector\Form;
use Drupal\Core\Extension\Extension;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Extension\ProfileExtensionList;
use Drupal\Core\Extension\ThemeHandler;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\upgrade_rector\ProjectCollector;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\upgrade_rector\RectorProcessor;
class UpgradeRectorForm extends FormBase {
protected $projectCollector;
protected $rectorResults;
protected $rectorProcessor;
public static function create(ContainerInterface $container) {
return new static($container
->get('upgrade_rector.project_collector'), $container
->get('keyvalue'), $container
->get('upgrade_rector.rector_processor'));
}
public function __construct(ProjectCollector $project_collector, KeyValueFactoryInterface $key_value_factory, RectorProcessor $rector_processor) {
$this->projectCollector = $project_collector;
$this->rectorResults = $key_value_factory
->get('upgrade_status_rector_results');
$this->rectorProcessor = $rector_processor;
}
public function getFormId() {
return 'drupal_upgrade_rector_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attached']['library'][] = 'upgrade_rector/upgrade_rector.admin';
$projects = $this->projectCollector
->collectProjects();
$custom = [
'#type' => 'markup',
'#markup' => '<br /><strong>' . $this
->t('No custom projects found.') . '</strong>',
];
if (count($projects['custom'])) {
$custom = $this
->buildProjectList($projects['custom'], 'custom');
}
$form['custom'] = [
'#type' => 'details',
'#title' => $this
->t('Custom projects'),
'#description' => $this
->t('Custom code is specific to your site, and must be upgraded manually. <a href=":upgrade">Read more about how developers can upgrade their code to Drupal 9</a>.', [
':upgrade' => 'https://www.drupal.org/docs/9/how-drupal-9-is-made-and-what-is-included/how-and-why-we-deprecate-on-the-way-to-drupal-9',
]),
'#open' => TRUE,
'#attributes' => [
'class' => [
'upgrade-rector-summary',
],
],
'data' => $custom,
'#tree' => TRUE,
];
$contrib = [
'#type' => 'markup',
'#markup' => '<br /><strong>' . $this
->t('No contributed projects found.') . '</strong>',
];
if (count($projects['contrib'])) {
$contrib = $this
->buildProjectList($projects['contrib'], 'contrib');
}
$form['contrib'] = [
'#type' => 'details',
'#title' => $this
->t('Contributed projects'),
'#description' => $this
->t('Contributed code is available from drupal.org. Problems here may be partially resolved by updating to the latest version. <a href=":update">Read more about how to update contributed projects</a>.', [
':update' => 'https://www.drupal.org/docs/8/update/update-modules',
]),
'#open' => TRUE,
'#attributes' => [
'class' => [
'upgrade-rector-summary',
],
],
'data' => $contrib,
'#tree' => TRUE,
];
return $form;
}
private function buildProjectList(array $projects, string $category) {
$list = $form = [];
foreach ($projects as $name => $extension) {
$info = $extension->info;
$label = $info['name'] . (!empty($info['version']) ? ' ' . $info['version'] : '');
$list[$name] = $label;
if ($results = $this->rectorResults
->get($extension
->getName())) {
$form[$name] = $this->rectorProcessor
->formatResults($results, $extension, $category) + [
'#type' => 'details',
'#closed' => TRUE,
];
}
}
$form['project'] = [
'#type' => 'select',
'#title' => $this
->t('Select project'),
'#options' => $list,
];
$form['submit'] = [
'#type' => 'submit',
'#name' => 'rector_' . $category,
'#value' => $this
->t('Run rector'),
'#button_type' => 'primary',
'#submit' => [
[
$this,
'submit' . ucfirst($category) . 'Rector',
],
],
];
return $form;
}
public function submitContribRector(array &$form, FormStateInterface $form_state) {
$projects = $this->projectCollector
->collectProjects();
$this
->submitRector($projects['contrib'][$form_state
->getValue([
'contrib',
'data',
'project',
])]);
}
public function submitCustomRector(array &$form, FormStateInterface $form_state) {
$projects = $this->projectCollector
->collectProjects();
$this
->submitRector($projects['custom'][$form_state
->getValue([
'custom',
'data',
'project',
])]);
}
public function submitRector(Extension $extension) {
$info = $extension->info;
$label = $info['name'] . (!empty($info['version']) ? ' ' . $info['version'] : '');
if (\Drupal::service('upgrade_rector.rector_processor')
->runRector($extension)) {
$this
->messenger()
->addMessage($this
->t('Parsing @project was successful.', [
'@project' => $label,
]));
}
else {
$this
->messenger()
->addError($this
->t('Error while parsing @project.', [
'@project' => $label,
]));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}