| 1 |  |  | <?php namespace Limoncello\Application\Packages\Csrf; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Copyright 2015-2018 [email protected] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Licensed under the Apache License, Version 2.0 (the "License"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * you may not use this file except in compliance with the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * You may obtain a copy of the License at | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * http://www.apache.org/licenses/LICENSE-2.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Unless required by applicable law or agreed to in writing, software | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * distributed under the License is distributed on an "AS IS" BASIS, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * See the License for the specific language governing permissions and | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * limitations under the License. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Limoncello\Application\Contracts\Csrf\CsrfTokenGeneratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Limoncello\Application\Contracts\Csrf\CsrfTokenStorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Limoncello\Application\Packages\Csrf\CsrfSettings as C; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use Limoncello\Contracts\Application\ContainerConfiguratorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use Limoncello\Contracts\Container\ContainerInterface as LimoncelloContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use Limoncello\Contracts\Session\SessionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use Limoncello\Contracts\Settings\SettingsProviderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use Psr\Container\ContainerInterface as PsrContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * @package Limoncello\Application | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class CsrfContainerConfigurator implements ContainerConfiguratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** @var callable */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     const CONFIGURATOR = [self::class, self::CONTAINER_METHOD_NAME]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @SuppressWarnings(PHPMD.StaticAccess) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |     public static function configureContainer(LimoncelloContainerInterface $container): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  |         $storage = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $factory = function (PsrContainerInterface $container) use (&$storage) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |             if ($storage === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 3 |  |                 $storage = static::createStorage($container); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 3 |  |             return $storage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |         $container[CsrfTokenGeneratorInterface::class] = $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         $container[CsrfTokenStorageInterface::class]   = $factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param PsrContainerInterface $container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @return CsrfTokenStorageInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 61 | 3 |  |     private static function createStorage(PsrContainerInterface $container): CsrfTokenStorageInterface | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         /** @var SettingsProviderInterface $provider */ | 
            
                                                        
            
                                    
            
            
                | 64 | 3 |  |         assert($container->has(SettingsProviderInterface::class)); | 
            
                                                        
            
                                    
            
            
                | 65 | 3 |  |         $provider = $container->get(SettingsProviderInterface::class); | 
            
                                                        
            
                                    
            
            
                | 66 | 3 |  |         assert($provider->has(C::class)); | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |         [ | 
            
                                                        
            
                                    
            
            
                | 68 | 3 |  |             C::TOKEN_STORAGE_KEY_IN_SESSION => $sessionKey, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 69 | 3 |  |             C::MAX_TOKENS                   => $maxTokens, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 70 | 3 |  |             C::MAX_TOKENS_THRESHOLD         => $maxTokensThreshold, | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |         ] | 
            
                                                        
            
                                    
            
            
                | 72 | 3 |  |             = $provider->get(C::class); | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |         /** @var SessionInterface $session */ | 
            
                                                        
            
                                    
            
            
                | 75 | 3 |  |         assert($container->has(SessionInterface::class)); | 
            
                                                        
            
                                    
            
            
                | 76 | 3 |  |         $session = $container->get(SessionInterface::class); | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 78 | 3 |  |         $storage = new CsrfTokenStorage($session, $sessionKey, $maxTokens, $maxTokensThreshold); | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 80 | 3 |  |         return $storage; | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |  | 
            
                        
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: