| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * apparat-server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @category    Apparat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @package     Apparat\Server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @subpackage  Apparat\Server\Infrastructure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author      Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license     http://opensource.org/licenses/MIT The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /*********************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *  Permission is hereby granted, free of charge, to any person obtaining a copy of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *  this software and associated documentation files (the "Software"), to deal in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *  the Software without restriction, including without limitation the rights to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *  the Software, and to permit persons to whom the Software is furnished to do so, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *  subject to the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *  The above copyright notice and this permission notice shall be included in all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *  copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  ***********************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | namespace Apparat\Server\Infrastructure\Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use Apparat\Kernel\Ports\Kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use Apparat\Server\Domain\Contract\ActionRouteInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | use Apparat\Server\Domain\Contract\RouteInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | use Apparat\Server\Domain\Contract\RouterContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | use Apparat\Server\Ports\Action\ActionInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | use Aura\Router\RouterContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | use Aura\Router\Rule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | use Psr\Http\Message\ResponseInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | use Psr\Http\Message\ServerRequestInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * Aura.Router adapter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  * @package Apparat\Server | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  * @subpackage Apparat\Server\Infrastructure | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  | class AuraRouterAdapter implements RouterContainerInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |      * Aura.Router container | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * @var RouterContainer | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     protected $routerContainer; | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * Constructor | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      * @param RouterContainer $routerContainer Router container | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 5 |  |     public function __construct(RouterContainer $routerContainer) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 5 |  |         $this->routerContainer = $routerContainer; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         // Override the default rules | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |         /** @var Rule\RuleIterator $ruleIterator */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 5 |  |         $ruleIterator = $routerContainer->getRuleIterator(); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 5 |  |         $ruleIterator->set([ | 
            
                                                                        
                            
            
                                    
            
            
                | 77 | 5 |  |             new Rule\Secure(), | 
            
                                                                        
                            
            
                                    
            
            
                | 78 | 5 |  |             new Rule\Host(), | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 5 |  |             new Rule\Allows(), | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 5 |  |             new Rule\Accepts(), | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 5 |  |             new ObjectPath(rtrim(parse_url(getenv('APPARAT_BASE_URL'), PHP_URL_PATH), '/') ?: null) | 
            
                                                                        
                            
            
                                    
            
            
                | 82 | 5 |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 83 | 5 |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      * Register a route | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |      * @param RouteInterface $route Route | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |      * @return RouterContainerInterface Self reference | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 5 |  |     public function registerRoute(RouteInterface $route) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         /** @var AuraRoute $auraRoute */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 5 |  |         $auraRoute = Kernel::create($route->isObject() ? AuraObjectRoute::class : AuraRoute::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 5 |  |         $auraRoute->name($route->getName()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 5 |  |             ->path($route->getPath()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 5 |  |             ->allows($route->getVerbs()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 5 |  |             ->handler($route->getAction()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 5 |  |             ->tokens($route->getTokens()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 5 |  |             ->defaults($route->getDefaults()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 5 |  |             ->wildcard($route->getWildcard()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 5 |  |             ->host($route->getHost()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 5 |  |             ->accepts($route->getAccepts()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 5 |  |             ->auth($route->getAuth()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 5 |  |             ->secure($route->getSecure()) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 5 |  |             ->extras($route->getExtras()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 5 |  |         $this->routerContainer->getMap()->addRoute($auraRoute); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 5 |  |         return $this; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |      * Dispatch a request to a route | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |      * @param ServerRequestInterface $request | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |      * @return AbstractActionRoute $route | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 118 | 77 |  |     public function dispatchRequestToRoute(ServerRequestInterface $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 120 | 77 |  |         $matcher = $this->routerContainer->getMatcher(); | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 77 |  |         $route = $matcher->match($request); | 
            
                                                                        
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |         // If a matching route was found | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 77 |  |         if ($route instanceof ActionRouteInterface) { | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 73 |  |             return $route; | 
            
                                                                        
                            
            
                                    
            
            
                | 126 | 5 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |         // Else create an error route | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 4 |  |         return AuraErrorRoute::cast($matcher->getFailedRoute()); | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |      * Prepare and return a route action | 
            
                                                                        
                            
            
                                    
            
            
                | 134 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |      * @param ServerRequestInterface $request Request | 
            
                                                                        
                            
            
                                    
            
            
                | 136 |  |  |      * @param AbstractActionRoute $route Route | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |      * @return ActionInterface|Callable $action Action | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 77 |  |     public function getRouteAction(ServerRequestInterface $request, AbstractActionRoute $route) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         // Pre-process the matched attributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 77 |  |         $route->preprocessAttributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         // Copy all route attributes to the server request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 77 |  |         foreach ($route->attributes as $key => $val) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 76 |  |             $request = $request->withAttribute($key, $val); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 | 77 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |         /** @var AbstractActionRoute $route */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 77 |  |         $handler = $route->getHandler(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         // If the handler is a callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 76 |  |         if (is_callable($handler)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 1 |  |             return function () use ($handler, $request) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 /** @var ResponseInterface $response */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 1 |  |                 $response = $handler($request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |                 return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 1 |  |             }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 75 |  |         return Kernel::create($handler, [$request]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 163 |  |  | } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 164 |  |  |  | 
            
                        
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.