| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Zenstruck\Browser\Tests\Fixture; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Psr\Log\NullLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Bundle\FrameworkBundle\FrameworkBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Bundle\SecurityBundle\SecurityBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Config\Loader\LoaderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\DependencyInjection\ContainerBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\HttpFoundation\File\UploadedFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\HttpFoundation\JsonResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\RedirectResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpKernel\Kernel as BaseKernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Routing\RouteCollectionBuilder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Security\Core\User\InMemoryUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Symfony\Component\Security\Core\User\UserInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @author Kevin Bond <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | final class Kernel extends BaseKernel | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     use MicroKernelTrait; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function page1(): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         return new Response(\file_get_contents(__DIR__.'/files/page1.html')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function page2(): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         return new Response('success'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     public function text(): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         return new Response('text content', 200, ['Content-Type' => 'text/plain']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     public function javascript(): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return new Response(\file_get_contents(__DIR__.'/files/javascript.html')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     public function xml(): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return new Response(\file_get_contents(__DIR__.'/files/xml.xml'), 200, ['Content-Type' => 'text/xml']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function submitForm(Request $request): JsonResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $files = \array_map( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             static function($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |                 if (\is_array($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |                     return \array_map(fn(UploadedFile $file) => $file->getClientOriginalName(), $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 return $value instanceof UploadedFile ? $value->getClientOriginalName() : null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             $request->files->all() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return new JsonResponse(\array_merge( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $request->request->all(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             \array_filter($files) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     public function httpMethod(Request $request): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         return new JsonResponse([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             'method' => $request->getMethod(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             'headers' => $request->headers->all(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             'query' => $request->query->all(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             'attributes' => $request->attributes->all(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             'files' => $request->files->all(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'server' => $request->server->all(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'request' => $request->request->all(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'content' => $request->getContent(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'ajax' => $request->isXmlHttpRequest(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function json(Request $request): JsonResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         return new JsonResponse( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $request->getContent(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             200, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ['Content-Type' => $request->query->get('content-type', 'application/json')], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     public function exception(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         throw new \Exception('exception thrown'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     public function redirect1(): RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         return new RedirectResponse('/redirect2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function redirect2(): RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return new RedirectResponse('/redirect3'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     public function redirect3(): RedirectResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         return new RedirectResponse('/page1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function user(?UserInterface $user = null): Response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if ($user) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             $username = \method_exists($user, 'getUserIdentifier') ? $user->getUserIdentifier() : $user->getUsername(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         return new Response($user ? "user: {$username}/{$user->getPassword()}" : 'anon'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function registerBundles(): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         yield new FrameworkBundle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         if (self::securityEnabled()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |             yield new SecurityBundle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     public static function securityEnabled(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |         return self::VERSION_ID >= 50300; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |         $c->loadFromExtension('framework', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |             'secret' => 'S3CRET', | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |             'router' => ['utf8' => true], | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |             'test' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |             'profiler' => ['enabled' => true, 'collect' => true], | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         $c->register('logger', NullLogger::class); // disable logging | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  |         if (self::securityEnabled()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 152 |  |  |             $c->loadFromExtension('security', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 153 |  |  |                 'enable_authenticator_manager' => true, | 
            
                                                                        
                            
            
                                    
            
            
                | 154 |  |  |                 'password_hashers' => [InMemoryUser::class => 'plaintext'], | 
            
                                                                        
                            
            
                                    
            
            
                | 155 |  |  |                 'providers' => ['users' => ['memory' => ['users' => ['kevin' => ['password' => 'pass']]]]], | 
            
                                                                        
                            
            
                                    
            
            
                | 156 |  |  |                 'firewalls' => ['main' => []], | 
            
                                                                        
                            
            
                                    
            
            
                | 157 |  |  |             ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 159 |  |  |             $c->loadFromExtension('framework', [ | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |                 'session' => ['storage_factory_id' => 'session.storage.factory.mock_file'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @param RouteCollectionBuilder|RoutingConfigurator $routes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     protected function configureRoutes($routes): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         if ($routes instanceof RouteCollectionBuilder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |             $routes->add('/page1', 'kernel::page1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |             $routes->add('/page2', 'kernel::page2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |             $routes->add('/text', 'kernel::text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |             $routes->add('/submit-form', 'kernel::submitForm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |             $routes->add('/http-method', 'kernel::httpMethod'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |             $routes->add('/exception', 'kernel::exception'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |             $routes->add('/redirect1', 'kernel::redirect1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |             $routes->add('/redirect2', 'kernel::redirect2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             $routes->add('/redirect3', 'kernel::redirect3'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             $routes->add('/json', 'kernel::json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |             $routes->add('/xml', 'kernel::xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |             $routes->add('/javascript', 'kernel::javascript'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             $routes->add('/user', 'kernel::user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |         $routes->add('page1', '/page1')->controller('kernel::page1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |         $routes->add('page2', '/page2')->controller('kernel::page2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         $routes->add('text', '/text')->controller('kernel::text'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |         $routes->add('submit-form', '/submit-form')->controller('kernel::submitForm'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |         $routes->add('http-method', '/http-method')->controller('kernel::httpMethod'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |         $routes->add('exception', '/exception')->controller('kernel::exception'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |         $routes->add('redirect1', '/redirect1')->controller('kernel::redirect1'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |         $routes->add('redirect2', '/redirect2')->controller('kernel::redirect2'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         $routes->add('redirect3', '/redirect3')->controller('kernel::redirect3'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |         $routes->add('json', '/json')->controller('kernel::json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         $routes->add('xml', '/xml')->controller('kernel::xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |         $routes->add('javascript', '/javascript')->controller('kernel::javascript'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |         $routes->add('user', '/user')->controller('kernel::user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 202 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 203 |  |  |  |