1 | <?php |
||
18 | class DisplayControllerGet extends AbstractController |
||
19 | { |
||
20 | /** |
||
21 | * The cache item pool. |
||
22 | * |
||
23 | * @var CacheItemPoolInterface |
||
24 | * @since 1.0 |
||
25 | */ |
||
26 | private $cache; |
||
27 | |||
28 | /** |
||
29 | * JSON view for displaying the statistics. |
||
30 | * |
||
31 | * @var StatsJsonView |
||
32 | * @since 1.0 |
||
33 | */ |
||
34 | private $view; |
||
35 | |||
36 | /** |
||
37 | * Constructor. |
||
38 | * |
||
39 | * @param StatsJsonView $view JSON view for displaying the statistics. |
||
40 | * @param CacheItemPoolInterface $cache The cache item pool. |
||
41 | * |
||
42 | * @since 1.0 |
||
43 | */ |
||
44 | 1 | public function __construct(StatsJsonView $view, CacheItemPoolInterface $cache) |
|
49 | |||
50 | /** |
||
51 | * Execute the controller. |
||
52 | * |
||
53 | * @return boolean |
||
54 | * |
||
55 | * @since 1.0 |
||
56 | */ |
||
57 | 2 | public function execute() |
|
58 | { |
||
59 | // Check if we are allowed to receive the raw data |
||
60 | 2 | $authorizedRaw = $this->getInput()->server->getString('HTTP_JOOMLA_RAW', 'fail') === $this->getApplication()->get('stats.rawdata', false); |
|
61 | |||
62 | // Check if a single data source is requested |
||
63 | 2 | $source = $this->getInput()->getString('source', ''); |
|
64 | |||
65 | 2 | $this->view->isAuthorizedRaw($authorizedRaw); |
|
66 | 2 | $this->view->setSource($source); |
|
67 | |||
68 | // Serve cached data if the cache layer is enabled and the raw data source is not requested |
||
69 | 2 | if ($this->getApplication()->get('cache.enabled', false) && !$authorizedRaw) |
|
70 | { |
||
71 | 1 | $key = md5(get_class($this->view) . __METHOD__ . $source); |
|
72 | |||
73 | 1 | if ($this->cache->hasItem($key)) |
|
74 | { |
||
75 | 1 | $item = $this->cache->getItem($key); |
|
76 | |||
77 | // Make sure we got a hit on the item, otherwise we'll have to re-cache |
||
78 | 1 | if ($item->isHit()) |
|
79 | { |
||
80 | 1 | $body = $item->get(); |
|
81 | } |
||
82 | else |
||
83 | { |
||
84 | $body = $this->view->render(); |
||
85 | |||
86 | 1 | $this->cacheData($key, $body); |
|
87 | } |
||
88 | } |
||
89 | else |
||
90 | { |
||
91 | 1 | $body = $this->view->render(); |
|
92 | |||
93 | 1 | $this->cacheData($key, $body); |
|
94 | } |
||
95 | } |
||
96 | else |
||
97 | { |
||
98 | 1 | $body = $this->view->render(); |
|
99 | } |
||
100 | |||
101 | 2 | $this->getApplication()->setBody($body); |
|
102 | |||
103 | 2 | return true; |
|
104 | } |
||
105 | |||
106 | /** |
||
107 | * Store the given data to the cache pool. |
||
108 | * |
||
109 | * @param string $key The key for the cache item. |
||
110 | * @param mixed $data The data to be stored to cache. |
||
111 | * |
||
112 | * @return void |
||
113 | * |
||
114 | * @since 1.0 |
||
115 | */ |
||
116 | 1 | private function cacheData(string $key, $data) |
|
123 | } |
||
124 |