Bug #88939 » FlushFoobarCache.php

Guido Schmechel, 2019-09-25 17:18

 
1
<?php
2

    
3
namespace Foobar\Sitepackage\Hooks\Backend;
4

    
5

    
6
use GuzzleHttp\Exception\GuzzleException;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
10
use TYPO3\CMS\Backend\Routing\UriBuilder;
11
use TYPO3\CMS\Backend\Toolbar\ClearCacheActionsHookInterface;
12
use TYPO3\CMS\Backend\Utility\BackendUtility;
13
use TYPO3\CMS\Core\Http\JsonResponse;
14
use TYPO3\CMS\Core\Http\RequestFactory;
15
use TYPO3\CMS\Core\Messaging\FlashMessage;
16
use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Core\Http\Response;
19

    
20
/**
21
 * Class ClearCacheActionsHook
22
 *
23
 * @package BraProjectfiles\Hooks\Backend\Toolbar
24
 */
25
class FlushNodeCacheHook implements ClearCacheActionsHookInterface
26
{
27

    
28
    /**
29
     * @var UriBuilder $uriBuilder
30
     */
31
    protected $uriBuilder;
32

    
33
    /**
34
     * @var BackendUserAuthentication
35
     */
36
    protected $backendUser;
37

    
38
    /**
39
     * ClearCacheActionsHook constructor.
40
     *
41
     * @param UriBuilder|null                $uriBuilder
42
     * @param BackendUserAuthentication|null $backendUser
43
     */
44
    public function __construct(UriBuilder $uriBuilder = null, BackendUserAuthentication $backendUser = null)
45
    {
46
        $this->uriBuilder = $uriBuilder ?? GeneralUtility::makeInstance(UriBuilder::class);
47
        $this->backendUser = $backendUser ?? $GLOBALS['BE_USER'];
48
    }
49

    
50
    /**
51
     * @param array $cacheActions
52
     * @param array $optionValues
53
     *
54
     * @throws \TYPO3\CMS\Backend\Routing\Exception\RouteNotFoundException
55
     */
56
    public function manipulateCacheActions(&$cacheActions, &$optionValues)
57
    {
58
        if (!$this->backendUser->isAdmin()) {
59
            return;
60
        }
61

    
62
        $href = $this->uriBuilder->buildUriFromRoute('ajax_foobar');
63

    
64
        $optionValues[] = 'clearFoobarCache';
65
        $cacheActions[] = [
66
            'id' => 'clearFoobarCache',
67
            'title' => 'Title',
68
            'description' => 'Description',
69
            'href' => $href,
70
            'iconIdentifier' => 'foobar-flush'
71
        ];
72
    }
73

    
74
    /**
75
     * @param ServerRequestInterface $request
76
     * @param ResponseInterface      $response
77
     *
78
     * @return ResponseInterface
79
     */
80
    public function myFunctionName(ServerRequestInterface $request): ResponseInterface
81
    {
82

    
83
		$content = [
84
			'success' => true
85
		];
86

    
87
		$response = new Response();
88
		$response->getBody()->write(json_encode($content));
89
		return $response;
90
}
91
}
92

    
(2-2/3)