2015-08-07 17 views
5

Próbuję profilować żądania wysłane do serwera API z klienta PHP przy użyciu Guzzle (v 6).Jak profilować żądania Guzzle 6?

W Guzzle 5.3 istnieje obsługa zdarzeń complete i before.

class GuzzleProfiler implements SubscriberInterface 
{ 
    public function getEvents() 
    { 
     return [ 
      'before' => ['onBefore'], 
      'complete' => ['onComplete'] 
     ]; 
    } 

    public function onBefore(BeforeEvent $event, $name) 
    { 
     start_profiling(); 
    } 

    public function onComplete(CompleteEvent $event, $name) 
    { 
     end_profiling(); 
    } 
} 

Ale jak to zrobić w wersji 6?

Odpowiedz

1

Właśnie znalazłem to za pomocą oprogramowania pośredniego. Oto kod.

class Profiler { 

    /** 
    * @return callable 
    */ 
    public static function profile() { 
     return function(callable $handler) { 
      return function(\Psr\Http\Message\RequestInterface $request, array $options) use ($handler) { 
       start_profiling(); 
       return $handler($request, $options)->then(function(\Psr\Http\Message\ResponseInterface $response) use ($token) { 
        end_profiling(); 
        return $response; 
       }); 
      }; 
     }; 
    } 
} 

A następnie załóż profiler w ten sposób.

$stack = \GuzzleHttp\HandlerStack::create(); 
$stack->push(Profiler::profile()); 
$client = new \GuzzleHttp\Client([ 
    'handler' => $stack 
]);