Пример #1
0
 public function testInputContainer()
 {
     $array = ['first' => '1', 'second' => '2', 'third' => '3'];
     $Input = new InputContainer($array);
     $this->assertTrue($Input->has('first'));
     $this->assertTrue($Input->has(['first', 'second', 'third']));
     $this->assertFalse($Input->has(['first', 'fourth']));
     $this->assertFalse($Input->has('fourth'));
 }
Пример #2
0
 public function route($inputData)
 {
     try {
         $Input = new InputContainer($inputData);
         if (!$Input->has('data')) {
             throw new RouterException(static::INVALID_DATA_PROVIDED_ERROR);
         }
         $requestData = $Input->get('data');
         if (!$requestData->has(['namespace', 'method'])) {
             throw new RouterException(static::INVALID_DATA_PROVIDED_ERROR);
         }
         $namespace = $requestData->get('namespace');
         $method = $requestData->get('method');
         $methodParams = [];
         if ($requestData->has('params')) {
             $methodParams = $requestData->get('params');
         }
         if (is_array($methodParams)) {
             $methodParams = $this->completeParams($methodParams);
         }
         $ActionHandler = $this->getHandler($namespace);
         $ActionHandler->setRequest($requestData);
         if (!is_callable([$ActionHandler, $method])) {
             throw new RouterException('Handler method not found.');
         }
         Observer::fireEvent(Observer::getEventName('before', $ActionHandler, $method));
         $Reflection = new \ReflectionMethod($ActionHandler, $method);
         if ($Reflection->getNumberOfParameters() == 1) {
             call_user_func([$ActionHandler, $method], $methodParams);
         } else {
             call_user_func_array([$ActionHandler, $method], $methodParams);
         }
         Observer::fireEvent(Observer::getEventName('after', $ActionHandler, $method));
         return $this->SerializationEngine->serialize($ActionHandler->getResult());
     } catch (\Exception $E) {
         return $this->SerializationEngine->serialize(['error' => true, 'errorMessage' => $E->getMessage(), 'errorCode' => $E->getCode(), 'params' => isset($methodParams) ? $methodParams : 'none gotten', 'trace' => debug_backtrace()]);
     }
 }