| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * 2017 Romain CANON <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * This file is part of the TYPO3 Formz project. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * It is free software; you can redistribute it and/or modify it | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * under the terms of the GNU General Public License, either | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * version 3 of the License, or any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, see: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * http://www.gnu.org/licenses/gpl-3.0.html | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | namespace Romm\Formz\Service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Romm\Formz\Core\Core; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Romm\Formz\Service\Traits\ExtendedFacadeInstanceTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use TYPO3\CMS\Core\Cache\Backend\AbstractBackend; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use TYPO3\CMS\Core\Cache\CacheManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use TYPO3\CMS\Core\Cache\Frontend\FrontendInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use TYPO3\CMS\Core\SingletonInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use TYPO3\CMS\Core\Utility\GeneralUtility; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  | class CacheService implements SingletonInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |     use ExtendedFacadeInstanceTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     const CACHE_IDENTIFIER = 'cache_formz'; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     const GENERATED_FILES_PATH = 'typo3temp/Formz/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      * @var TypoScriptService | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     protected $typoScriptService; | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @var FrontendInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     protected $cacheInstance; | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      * Returns the type of backend cache defined in TypoScript at the path: | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |      * `settings.defaultBackendCache`. | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * @throws \Exception | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     public function getBackendCache() | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         $backendCache = $this->typoScriptService->getExtensionConfigurationFromPath('settings.defaultBackendCache'); | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         if (false === class_exists($backendCache) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |             && false === in_array(AbstractBackend::class, class_parents($backendCache)) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             throw new \Exception( | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |                 'The cache class name given in configuration "config.tx_formz.settings.defaultBackendCache" must inherit "' . AbstractBackend::class . '" (current value: "' . (string)$backendCache . '")', | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |                 1459251263 | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             ); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return $backendCache; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * Returns the cache instance for this extension. | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @return FrontendInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     public function getCacheInstance() | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         if (null === $this->cacheInstance) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |             /** @var $cacheManager CacheManager */ | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |             $cacheManager = Core::instantiate(CacheManager::class); | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             if ($cacheManager->hasCache(self::CACHE_IDENTIFIER)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |                 $this->cacheInstance = $cacheManager->getCache(self::CACHE_IDENTIFIER); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         return $this->cacheInstance; | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @param FrontendInterface $cacheInstance | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     public function setCacheInstance(FrontendInterface $cacheInstance) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         $this->cacheInstance = $cacheInstance; | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |      * Generic cache identifier creation for usages in the extension. | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |      * @param string $string | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * @param string $formClassName | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * @param int    $maxLength | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * @return string | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |     public function getCacheIdentifier($string, $formClassName, $maxLength = 55) | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         $explodedClassName = explode('\\', $formClassName); | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |         $identifier = strtolower( | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |             $string . | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |             end($explodedClassName) . | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |             '-' . | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |             sha1($formClassName) | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         return substr($identifier, 0, $maxLength); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * @param TypoScriptService $typoScriptService | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |     public function injectTypoScriptService(TypoScriptService $typoScriptService) | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |         $this->typoScriptService = $typoScriptService; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |      * Function called when clearing TYPO3 caches. It will remove the temporary | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |      * asset files created by Formz. | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |      * @param array $parameters | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function clearCacheCommand($parameters) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         if (false === in_array($parameters['cacheCmd'], ['all', 'system'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         $files = glob(GeneralUtility::getFileAbsFileName(self::GENERATED_FILES_PATH . '*')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         if (false === $files) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         foreach ($files as $assetCacheFile) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             unlink($assetCacheFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 142 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 143 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 144 |  |  |  |