View source
<?php
namespace Drupal\system_test\Controller;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Lock\LockBackendInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SystemTestController extends ControllerBase implements TrustedCallbackInterface {
protected $lock;
protected $persistentLock;
protected $currentUser;
protected $renderer;
protected $messenger;
public function __construct(LockBackendInterface $lock, LockBackendInterface $persistent_lock, AccountInterface $current_user, RendererInterface $renderer, MessengerInterface $messenger) {
$this->lock = $lock;
$this->persistentLock = $persistent_lock;
$this->currentUser = $current_user;
$this->renderer = $renderer;
$this->messenger = $messenger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('lock'), $container
->get('lock.persistent'), $container
->get('current_user'), $container
->get('renderer'), $container
->get('messenger'));
}
public function mainContentFallback() {
return [
'#markup' => $this
->t('Content to test main content fallback'),
];
}
public function messengerServiceTest() {
$this->messenger
->addStatus('First message (removed).');
$this->messenger
->addStatus($this
->t('Second message with <em>markup!</em> (not removed).'));
$messages = $this->messenger
->deleteByType('status');
unset($messages[0]);
foreach ($messages as $message) {
$this->messenger
->addStatus($message);
}
$this->messenger
->addStatus('Non Duplicated message');
$this->messenger
->addStatus('Non Duplicated message');
$this->messenger
->addStatus('Duplicated message', TRUE);
$this->messenger
->addStatus('Duplicated message', TRUE);
$this->messenger
->addStatus(Markup::create('Markup with <em>markup!</em>'));
$this->messenger
->addStatus(Markup::create('Markup with <em>markup!</em>'));
$this->messenger
->addStatus(Markup::create('Markup2 with <em>markup!</em>'));
$this->messenger
->addStatus(Markup::create('Non duplicate Markup / string.'));
$this->messenger
->addStatus('Non duplicate Markup / string.');
$this->messenger
->addStatus(Markup::create('Duplicate Markup / string.'), TRUE);
$this->messenger
->addStatus('Duplicate Markup / string.', TRUE);
$this->messenger
->addStatus('<em>This<span>markup will be</span> escaped</em>.');
return [];
}
public function getDestination(Request $request) {
$response = new Response($request->query
->get('destination'));
return $response;
}
public function requestDestination(Request $request) {
$response = new Response($request->request
->get('destination'));
return $response;
}
public function lockAcquire() {
if ($this->lock
->acquire('system_test_lock_acquire')) {
$this->lock
->release('system_test_lock_acquire');
return [
'#markup' => 'TRUE: Lock successfully acquired in \\Drupal\\system_test\\Controller\\SystemTestController::lockAcquire()',
];
}
else {
return [
'#markup' => 'FALSE: Lock not acquired in \\Drupal\\system_test\\Controller\\SystemTestController::lockAcquire()',
];
}
}
public function lockExit() {
if ($this->lock
->acquire('system_test_lock_exit', 900)) {
echo 'TRUE: Lock successfully acquired in \\Drupal\\system_test\\Controller\\SystemTestController::lockExit()';
exit;
}
else {
return [
'#markup' => 'FALSE: Lock not acquired in system_test_lock_exit()',
];
}
}
public function lockPersist($lock_name) {
if ($this->persistentLock
->acquire($lock_name)) {
return [
'#markup' => 'TRUE: Lock successfully acquired in SystemTestController::lockPersist()',
];
}
else {
return [
'#markup' => 'FALSE: Lock not acquired in SystemTestController::lockPersist()',
];
}
}
public function system_test_cache_tags_page() {
$build['main'] = [
'#cache' => [
'tags' => [
'system_test_cache_tags_page',
],
],
'#pre_render' => [
'\\Drupal\\system_test\\Controller\\SystemTestController::preRenderCacheTags',
],
'message' => [
'#markup' => 'Cache tags page example',
],
];
return $build;
}
public function system_test_cache_maxage_page() {
$build['main'] = [
'#cache' => [
'max-age' => 90,
],
'message' => [
'#markup' => 'Cache max-age page example',
],
];
return $build;
}
public static function preRenderCacheTags($elements) {
$elements['#cache']['tags'][] = 'pre_render';
return $elements;
}
public function authorizeInit($page_title) {
$authorize_url = Url::fromUri('base:core/authorize.php', [
'absolute' => TRUE,
])
->toString();
system_authorized_init('system_test_authorize_run', __DIR__ . '/../../system_test.module', [], $page_title);
return new RedirectResponse($authorize_url);
}
public function setHeader(Request $request) {
$query = $request->query
->all();
$response = new CacheableResponse();
$response->headers
->set($query['name'], $query['value']);
$response
->getCacheableMetadata()
->addCacheContexts([
'url.query_args:name',
'url.query_args:value',
]);
$response
->setContent($this
->t('The following header was set: %name: %value', [
'%name' => $query['name'],
'%value' => $query['value'],
]));
return $response;
}
public function respondWithResponse(Request $request) {
return new Response('test');
}
public function respondWithPublicResponse() {
return (new Response('test'))
->setPublic()
->setMaxAge(60);
}
public function respondWithCacheableResponse(Request $request) {
return new CacheableResponse('test');
}
public function shutdownFunctions($arg1, $arg2) {
drupal_register_shutdown_function('_system_test_first_shutdown_function', $arg1, $arg2);
if (function_exists('fastcgi_finish_request')) {
return [
'#markup' => 'The function fastcgi_finish_request exists when serving the request.',
];
}
return [];
}
public function configureTitle($foo) {
return 'Bar.' . $foo;
}
public function simpleEcho($text) {
return [
'#plain_text' => $text,
];
}
public function permissionDependentContent() {
$build = [];
$access = AccessResult::allowedIfHasPermission($this->currentUser, 'pet llamas');
$this->renderer
->addCacheableDependency($build, $access);
if ($access
->isAllowed()) {
$build['allowed'] = [
'#markup' => 'Permission to pet llamas: yes!',
];
}
else {
$build['forbidden'] = [
'#markup' => 'Permission to pet llamas: no!',
];
}
return $build;
}
public function getCurrentDate() {
$response = new Response(\Drupal::service('date.formatter')
->format(1452702549));
return $response;
}
public function getTestHeader(Request $request) {
$response = new Response();
$response->headers
->set('Test-Header', $request->headers
->get('Test-Header'));
return $response;
}
public function getCacheableResponseWithCustomCacheControl() {
return new CacheableResponse('Foo', 200, [
'Cache-Control' => 'bar',
]);
}
public static function trustedCallbacks() {
return [
'preRenderCacheTags',
];
}
public function getInstallProfile() {
$install_profile = \Drupal::installProfile() ?: 'NONE';
return new Response('install_profile: ' . $install_profile);
}
}