Exemplo n.º 1
0
 /**
  * @return string
  */
 public function getPanel()
 {
     if (!$this->queries) {
         return NULL;
     }
     ob_start();
     $esc = callback('Nette\\Templating\\Helpers::escapeHtml');
     $click = class_exists('\\Tracy\\Dumper') ? function ($o, $c = FALSE, $d = 4) {
         return \Tracy\Dumper::toHtml($o, array('collapse' => $c, 'depth' => $d));
     } : callback('\\Tracy\\Helpers::clickableDump');
     $totalTime = $this->totalTime ? sprintf('%0.3f', $this->totalTime * 1000) . ' ms' : 'none';
     $extractData = function ($object) {
         if ($object instanceof Elastica\Request) {
             $data = $object->getData();
         } elseif ($object instanceof Elastica\Response) {
             $data = $object->getData();
         } else {
             return array();
         }
         try {
             return !is_array($data) ? Json::decode($data, Json::FORCE_ARRAY) : $data;
         } catch (Nette\Utils\JsonException $e) {
             try {
                 return array_map(function ($row) {
                     return Json::decode($row, Json::FORCE_ARRAY);
                 }, explode("\n", trim($data)));
             } catch (Nette\Utils\JsonException $e) {
                 return $data;
             }
         }
     };
     $processedQueries = array();
     $allQueries = $this->queries;
     foreach ($allQueries as $authority => $requests) {
         /** @var Elastica\Request[] $item */
         foreach ($requests as $i => $item) {
             $processedQueries[$authority][$i] = $item;
             if (isset($item[3])) {
                 continue;
                 // exception, do not re-execute
             }
             if (stripos($item[0]->getPath(), '_search') === FALSE || $item[0]->getMethod() !== 'GET') {
                 continue;
                 // explain only search queries
             }
             if (!is_array($data = $extractData($item[0]))) {
                 continue;
             }
             try {
                 $response = $this->client->request($item[0]->getPath(), $item[0]->getMethod(), $item[0]->getData(), array('explain' => 1) + $item[0]->getQuery());
                 // replace the search response with the explained response
                 $processedQueries[$authority][$i][1] = $response;
             } catch (\Exception $e) {
                 // ignore
             }
         }
     }
     require __DIR__ . '/panel.phtml';
     return ob_get_clean();
 }