| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Aoe\Varnish\System; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Aoe\Varnish\Domain\Model\TagInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Aoe\Varnish\TYPO3\Configuration\ExtensionConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use TYPO3\CMS\Core\Log\LogManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use TYPO3\CMS\Core\SingletonInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Varnish implements SingletonInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var Http | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var ExtensionConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $extensionConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var LogManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $logManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param Http $http | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param ExtensionConfiguration $extensionConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param LogManager $logManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 4 |  |     public function __construct(Http $http, ExtensionConfiguration $extensionConfiguration, LogManager $logManager) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 4 |  |         $this->http = $http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |         $this->extensionConfiguration = $extensionConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |         $this->logManager = $logManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 4 |  |         register_shutdown_function([$this, 'shutdown']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |     public function shutdown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |         $phrases = $this->http->wait(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |         if (is_array($phrases)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             foreach ($phrases as $phrase) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                 if ($phrase['success']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |                     $this->logManager->getLogger(__CLASS__)->info($phrase['reason']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |                     $this->logManager->getLogger(__CLASS__)->alert($phrase['reason']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param TagInterface $tag | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 2 |  |     public function banByTag(TagInterface $tag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 2 |  |         if (false === $tag->isValid()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |             throw new \RuntimeException('Tag is not valid', 1435159558); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $this->request('BAN', ['X-Ban-Tags' => $tag->getIdentifier()]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 1 |  |     public function banAll() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |         $this->request('BAN', ['X-Ban-All' => '1']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param string $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param array $headers | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 78 | 2 |  |     private function request($method, $headers = []) | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 80 | 2 |  |         foreach ($this->extensionConfiguration->getHosts() as $host) { | 
            
                                                        
            
                                    
            
            
                | 81 | 2 |  |             $this->http->request($method, $host, $headers); | 
            
                                                        
            
                                    
            
            
                | 82 | 2 |  |         } | 
            
                                                        
            
                                    
            
            
                | 83 | 2 |  |     } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |  |