| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Selami; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Psr\Container\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Selami\Stdlib\Resolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use ReflectionClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class FrontController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $controllerClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private $uriParameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |         ContainerInterface $container, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         string $controller, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         ?array $uriParameters = [] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->container = $container; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->controller = $controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $this->uriParameters = $uriParameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     public function getUriParameters() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         return $this->uriParameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public function getControllerResponse() : ControllerResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->controllerClass = $this->controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $controllerConstructorArguments = Resolver::getParameterHints($this->controllerClass, '__construct'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $arguments = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         foreach ($controllerConstructorArguments as $argumentName => $argumentType) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             $arguments[] = $this->getArgument($argumentName, $argumentType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $controllerClass = new ReflectionClass($this->controllerClass); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |          * @var $controllerObject \Selami\Interfaces\ApplicationController | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |          */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $controllerObject = $controllerClass->newInstanceArgs($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         return $controllerObject(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 49 |  |  |     private function getArgument(string $argumentName, string $argumentType) | 
            
                                                        
            
                                    
            
            
                | 50 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 51 |  |  |         if ($argumentType === Resolver::ARRAY && $argumentName === 'uriParameters') { | 
            
                                                        
            
                                    
            
            
                | 52 |  |  |             return $this->getUriParameters(); | 
            
                                                        
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 54 |  |  |         return  $this->container->has($argumentType) ? $this->container->get($argumentType) : | 
            
                                                        
            
                                    
            
            
                | 55 |  |  |             $this->container->get($argumentName); | 
            
                                                        
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 57 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 58 |  |  |  |