| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of Blitz PHP framework. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * the LICENSE file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace BlitzPHP\Exceptions; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use BlitzPHP\Container\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use BlitzPHP\Contracts\Http\ResponsableInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * RedirectException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | class RedirectException extends Exception implements ResponsableInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Status code pour les redirections | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @var int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     protected $code = 302; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     protected ?ResponseInterface $response = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param ResponseInterface|string $message Response object or a string containing a relative URI. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param int                      $code    HTTP status code to redirect if $message is a string. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct(ResponseInterface|string $message = '', int $code = 0, ?Throwable $previous = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         if ($message instanceof ResponseInterface) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->response = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $message        = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             if ($this->response->getHeaderLine('Location') === '' && $this->response->getHeaderLine('Refresh') === '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 throw new LogicException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     'The Response object passed to RedirectException does not contain a redirect address.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             if ($this->response->getStatusCode() < 301 || $this->response->getStatusCode() > 308) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |                 $this->response = $this->response->withStatus($this->code); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         parent::__construct($message, $code, $previous); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     public function getResponse(): ResponseInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         if (null === $this->response) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             $this->response = Services::response() | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |                 ->redirect(base_url($this->getMessage()), 'auto', $this->getCode()); | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         Services::logger()->info( | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |             'REDIRECTED ROUTE at ' | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |             . ($this->response->getHeaderLine('Location') ?: substr($this->response->getHeaderLine('Refresh'), 6)) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         return $this->response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function toResponse(ServerRequestInterface $request): ResponseInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $this->getResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |