ScanResultController.php in Upgrade Status 8.2
File
src/Controller/ScanResultController.php
View source
<?php
namespace Drupal\upgrade_status\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\RendererInterface;
use Drupal\upgrade_status\ProjectCollector;
use Drupal\upgrade_status\ScanResultFormatter;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
class ScanResultController extends ControllerBase {
protected $resultFormatter;
protected $projectCollector;
protected $renderer;
public function __construct(ScanResultFormatter $result_formatter, ProjectCollector $project_collector, RendererInterface $renderer) {
$this->resultFormatter = $result_formatter;
$this->projectCollector = $project_collector;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('upgrade_status.result_formatter'), $container
->get('upgrade_status.project_collector'), $container
->get('renderer'));
}
public function resultPage(string $type, string $project_machine_name) {
$extension = $this->projectCollector
->loadProject($type, $project_machine_name);
return $this->resultFormatter
->formatResult($extension);
}
public function resultExport(string $type, string $project_machine_name, string $format) {
$extension = $this->projectCollector
->loadProject($type, $project_machine_name);
$result = $this->resultFormatter
->getRawResult($extension);
if (!in_array($format, [
'html',
'ascii',
])) {
$format = 'html';
}
$build = [
'#theme' => 'upgrade_status_' . $format . '_export',
];
$build['#projects'][empty($extension->info['project']) ? 'custom' : 'contrib'] = [
$project_machine_name => $format == 'html' ? $this->resultFormatter
->formatResult($extension) : $this->resultFormatter
->formatAsciiResult($extension),
];
$fileDate = $this->resultFormatter
->formatDateTime($result['date'], 'html_datetime');
$extension = $format == 'html' ? '.html' : '.txt';
$filename = 'single-export-' . $project_machine_name . '-' . $fileDate . $extension;
$response = new Response($this->renderer
->renderRoot($build));
$response->headers
->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
return $response;
}
public function analyze(string $type, string $project_machine_name) {
if ($type == 'upgrade_status_request_test' && $project_machine_name == 'upgrade_status_request_test') {
return new JsonResponse([
'message' => 'Request test success',
]);
}
else {
$extension = $this->projectCollector
->loadProject($type, $project_machine_name);
\Drupal::service('upgrade_status.deprecation_analyzer')
->analyze($extension);
return new JsonResponse([
'message' => $this
->t('Scanned @project', [
'@project' => $extension
->getName(),
]),
]);
}
}
}