Version 1
[yaffs-website] / web / core / modules / aggregator / tests / modules / aggregator_test / src / Controller / AggregatorTestRssController.php
1 <?php
2
3 namespace Drupal\aggregator_test\Controller;
4
5 use Drupal\Component\Datetime\DateTimePlus;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Component\Utility\Crypt;
8 use Symfony\Component\HttpFoundation\Response;
9 use Symfony\Component\HttpFoundation\Request;
10
11 /**
12  * Controller for the aggregator_test module.
13  */
14 class AggregatorTestRssController extends ControllerBase {
15
16   /**
17    * Generates a test feed and simulates last-modified and etags.
18    *
19    * @param bool $use_last_modified
20    *   Set TRUE to send a last modified header.
21    * @param bool $use_etag
22    *   Set TRUE to send an etag.
23    * @param \Symfony\Component\HttpFoundation\Request $request
24    *   Information about the current HTTP request.
25    *
26    * @return \Symfony\Component\HttpFoundation\Response
27    *   A feed that forces cache validation.
28    */
29   public function testFeed($use_last_modified, $use_etag, Request $request) {
30     $response = new Response();
31
32     $last_modified = strtotime('Sun, 19 Nov 1978 05:00:00 GMT');
33     $etag = Crypt::hashBase64($last_modified);
34
35     $if_modified_since = strtotime($request->server->get('HTTP_IF_MODIFIED_SINCE'));
36     $if_none_match = stripslashes($request->server->get('HTTP_IF_NONE_MATCH'));
37
38     // Send appropriate response. We respond with a 304 not modified on either
39     // etag or on last modified.
40     if ($use_last_modified) {
41       $response->headers->set('Last-Modified', gmdate(DateTimePlus::RFC7231, $last_modified));
42     }
43     if ($use_etag) {
44       $response->headers->set('ETag', $etag);
45     }
46     // Return 304 not modified if either last modified or etag match.
47     if ($last_modified == $if_modified_since || $etag == $if_none_match) {
48       $response->setStatusCode(304);
49       return $response;
50     }
51
52     // The following headers force validation of cache.
53     $response->headers->set('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
54     $response->headers->set('Cache-Control', 'must-revalidate');
55     $response->headers->set('Content-Type', 'application/rss+xml; charset=utf-8');
56
57     // Read actual feed from file.
58     $file_name = __DIR__ . '/../../aggregator_test_rss091.xml';
59     $handle = fopen($file_name, 'r');
60     $feed = fread($handle, filesize($file_name));
61     fclose($handle);
62
63     $response->setContent($feed);
64     return $response;
65   }
66
67   /**
68    * Generates a rest redirect to the test feed.
69    *
70    * @return \Symfony\Component\HttpFoundation\RedirectResponse
71    *   A response that redirects users to the test feed.
72    */
73   public function testRedirect() {
74     return $this->redirect('aggregator_test.feed', [], [], 301);
75   }
76
77 }