BatchController.php 3.32 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\system\Controller\BatchController.
 */

namespace Drupal\system\Controller;

10 11 12 13 14 15
use Drupal\Core\Controller\TitleResolverInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Page\DefaultHtmlPageRenderer;
use Drupal\Core\Page\HtmlPage;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
16 17 18 19 20 21 22
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;

/**
 * Controller routines for batch routes.
 */
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
class BatchController implements ContainerInjectionInterface {

  /**
   * The HTML page renderer.
   *
   * @var \Drupal\Core\Page\DefaultHtmlPageRenderer
   */
  protected $htmlPageRenderer;

  /**
   * The title resolver.
   *
   * @var \Drupal\Core\Controller\TitleResolverInterface
   */
  protected $titleResolver;

  /**
   * Constructs a new BatchController.
   *
   * @param \Drupal\Core\Page\DefaultHtmlPageRenderer $html_page_renderer
   *   The HTML page renderer.
   * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
   *   The title resolver.
   */
  public function __construct(DefaultHtmlPageRenderer $html_page_renderer, TitleResolverInterface $title_resolver) {
    $this->htmlPageRenderer = $html_page_renderer;
    $this->titleResolver = $title_resolver;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('html_page_renderer'),
      $container->get('title_resolver')
    );
  }
61 62 63 64

  /**
   * Returns a system batch page.
   *
65
   * @param \Symfony\Component\HttpFoundation\Request $request
66 67 68
   *   The current request object.
   *
   * @return mixed
69 70 71 72
   *   A \Symfony\Component\HttpFoundation\Response object or page element or
   *   NULL.
   *
   * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
   */
  public function batchPage(Request $request) {
    require_once DRUPAL_ROOT . '/core/includes/batch.inc';
    $output = _batch_page($request);

    if ($output === FALSE) {
      throw new AccessDeniedHttpException();
    }
    elseif ($output instanceof Response) {
      return $output;
    }
    elseif (isset($output)) {
      // Force a page without blocks or messages to
      // display a list of collected messages later.
      drupal_set_page_content($output);
      $page = element_info('page');
      $page['#show_messages'] = FALSE;
90 91 92

      $page = $this->render($page);

93 94 95 96
      return $page;
    }
  }

97 98 99 100 101 102 103 104
  /**
   * {@inheritdoc}
   */
  public function render(array $output, $status_code = 200) {
    if (!isset($output['#title'])) {
      $request = \Drupal::request();
      $output['#title'] = $this->titleResolver->getTitle($request, $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT));
    }
105
    $page = new HtmlPage('', isset($output['#cache']) ? $output['#cache'] : array(), $output['#title']);
106 107 108 109 110 111 112 113 114 115 116 117 118 119

    $page_array = drupal_prepare_page($output);

    $page = $this->htmlPageRenderer->preparePage($page, $page_array);

    $page->setBodyTop(drupal_render($page_array['page_top']));
    $page->setBodyBottom(drupal_render($page_array['page_bottom']));
    $page->setContent(drupal_render($page_array));

    $page->setStatusCode($status_code);

    return $page;
  }

120
}