| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types = 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Jarvis\Skill\DependencyInjection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Jarvis\Jarvis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Jarvis\Skill\Core\CallbackResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Jarvis\Skill\EventBroadcaster\BroadcasterInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Jarvis\Skill\EventBroadcaster\ControllerEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Jarvis\Skill\EventBroadcaster\ExceptionEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Jarvis\Skill\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\HttpFoundation\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\HttpFoundation\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpFoundation\Session\Session; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * This is Jarvis internal container provider. It will inject every core | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * parameters and services into Jarvis. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @author Eric Chau <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | final class ContainerProvider implements ContainerProviderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     public function hydrate(Jarvis $app) | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $this->mountSettings($app); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         $this->mountServices($app); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $this->mountEventReceivers($app); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected function mountSettings(Jarvis $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $app['debug'] = $app['settings']['debug'] ?? Jarvis::DEFAULT_DEBUG; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $app->lock('debug'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $extra = $app['settings']['extra'] ?? []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         foreach ((array) $extra as $key => $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $app["{$key}.settings"] = $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             $app->lock("{$key}.settings"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         unset($app['settings']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     protected function mountServices(Jarvis $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $app['app'] = function () use ($app): Jarvis { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             return $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $app['request'] = function (Jarvis $app): Request { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             $request = Request::createFromGlobals(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $session = $request->getSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             if (null === $session) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |                 $settings = $app['session.settings'] ?? []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |                 $storageClassname = $settings['session.storage.classname'] ?? NativeSessionStorage::class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 unset($settings['session.storage.classname']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 $session = new Session(new $storageClassname($settings)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $request->setSession($session); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $app['session'] = function (Jarvis $app): Session { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             return $app['request']->getSession(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $app['router'] = function (): Router { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             return new Router(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $app['callbackResolver'] = function (Jarvis $app): CallbackResolver { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             return new CallbackResolver($app); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $app->lock(['app', 'request', 'session', 'router', 'callbackResolver']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     protected function mountEventReceivers(Jarvis $app) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $app->on(BroadcasterInterface::EXCEPTION_EVENT, function (ExceptionEvent $event) use ($app): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             $throwable = $event->exception(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             $msg = sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 '[%s] error in %s at line %s with message: %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 get_class($throwable), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 $throwable->getFile(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $throwable->getLine(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $throwable->getMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             if (!$app['debug']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |                 error_log($msg); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 $msg = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $event->setResponse(new Response($msg, Response::HTTP_INTERNAL_SERVER_ERROR)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         }, BroadcasterInterface::RECEIVER_LOW_PRIORITY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $app->on(BroadcasterInterface::CONTROLLER_EVENT, function (ControllerEvent $event) use ($app): void { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $finalArgs = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $rawArgs = $event->arguments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $refMethod = new \ReflectionMethod($event->callback(), '__invoke'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             foreach ($refMethod->getParameters() as $refParam) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |                 if (null !== $refClass = $refParam->getClass()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |                     if (isset($app[$refClass->getName()])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                         $finalArgs[$refParam->getPosition()] = $app[$refClass->getName()]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                         continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 if (in_array($refParam->name, array_keys($rawArgs))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                     $finalArgs[$refParam->getPosition()] = $rawArgs[$refParam->name]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $event->setArguments($finalArgs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         }, BroadcasterInterface::RECEIVER_LOW_PRIORITY); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  |