diff --git a/Client.php b/Client.php new file mode 100644 index 0000000..0e9939f --- /dev/null +++ b/Client.php @@ -0,0 +1,21 @@ + + */ +class Client extends Elastica_Client +{ + public function setLogger($logger) + { + $this->logger = $logger; + } + + public function request($path, $method, $data = array()) { + $this->logger->logQuery($path, $method, $data); + + return parent::request($path, $method, $data); + } +} diff --git a/DataCollector/ElasticaDataCollector.php b/DataCollector/ElasticaDataCollector.php new file mode 100644 index 0000000..dea2cbb --- /dev/null +++ b/DataCollector/ElasticaDataCollector.php @@ -0,0 +1,49 @@ +logger = $logger; + } + + /** + * {@inheritdoc} + */ + public function collect(Request $request, Response $response, \Exception $exception = null) + { + $this->data['nb_queries'] = $this->logger->getNbQueries(); + $this->data['queries'] = $this->logger->getQueries(); + } + + public function getQueryCount() + { + return $this->data['nb_queries']; + } + + public function getQueries() + { + return $this->data['queries']; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'elastica'; + } +} \ No newline at end of file diff --git a/DependencyInjection/FOQElasticaExtension.php b/DependencyInjection/FOQElasticaExtension.php index 4a223b1..aa165d0 100644 --- a/DependencyInjection/FOQElasticaExtension.php +++ b/DependencyInjection/FOQElasticaExtension.php @@ -65,9 +65,13 @@ class FOQElasticaExtension extends Extension { $clientIds = array(); foreach ($clients as $name => $clientConfig) { - $clientDef = new Definition('%foq_elastica.client.class%', array($clientConfig)); + $clientDef = $container->getDefinition('foq_elastica.client'); + $clientDef->replaceArgument(0, array($clientConfig)); + $clientId = sprintf('foq_elastica.client.%s', $name); + $container->setDefinition($clientId, $clientDef); + $clientIds[$name] = $clientId; } diff --git a/Logger/ElasticaLogger.php b/Logger/ElasticaLogger.php new file mode 100644 index 0000000..9a44852 --- /dev/null +++ b/Logger/ElasticaLogger.php @@ -0,0 +1,74 @@ + + */ +class ElasticaLogger +{ + protected $logger; + protected $prefix; + protected $queries; + + /** + * Constructor. + * + * @param LoggerInterface $logger The Symfony logger + * @param string $prefix A prefix for messages sent to the Symfony logger + */ + public function __construct(LoggerInterface $logger = null, $prefix = 'Elastica') + { + $this->logger = $logger; + $this->prefix = $prefix; + $this->queries = array(); + } + + /** + * Logs a query. + * + * This method is configured as the logger callable in the service + * container. + * + * @param string $path Path to call + * @param string $method Rest method to use (GET, POST, DELETE, PUT) + * @param array $data OPTIONAL Arguments as array + */ + public function logQuery($path, $method, array $data = array()) + { + $logInfo = sprintf("%s: %s (%s) \n%s", $this->prefix, $path, $method, json_encode($data)); + + $this->queries[] = $logInfo; + + if (null !== $this->logger) { + $this->logger->info($logInfo); + } + } + + /** + * Returns the number of queries that have been logged. + * + * @return integer The number of queries logged + */ + public function getNbQueries() + { + return count($this->queries); + } + + /** + * Returns a human-readable array of queries logged. + * + * @return array An array of queries + */ + public function getQueries() + { + return $this->queries; + } + +} diff --git a/Resources/config/config.xml b/Resources/config/config.xml index 3867e97..6ef919f 100644 --- a/Resources/config/config.xml +++ b/Resources/config/config.xml @@ -5,14 +5,33 @@ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - Elastica_Client + FOQ\ElasticaBundle\Client Elastica_Index Elastica_Type + FOQ\ElasticaBundle\Logger\ElasticaLogger + FOQ\ElasticaBundle\DataCollector\ElasticaDataCollector + + + + + + + + + + + + + + + + + diff --git a/Resources/public/images/elastica.png b/Resources/public/images/elastica.png new file mode 100644 index 0000000..dbde014 Binary files /dev/null and b/Resources/public/images/elastica.png differ diff --git a/Resources/views/Collector/elastica.html.twig b/Resources/views/Collector/elastica.html.twig new file mode 100644 index 0000000..899b901 --- /dev/null +++ b/Resources/views/Collector/elastica.html.twig @@ -0,0 +1,45 @@ +{% extends 'WebProfilerBundle:Profiler:layout.html.twig' %} + +{% block toolbar %} + {% set icon %} + elastica + {% endset %} + {% set text %} + {{ collector.querycount }} + {% endset %} + {% include 'WebProfilerBundle:Profiler:toolbar_item.html.twig' with { 'link': profiler_url } %} +{% endblock %} + +{% block menu %} + + + Elastica + + {{ collector.querycount }} + + +{% endblock %} + +{% block panel %} +

Queries

+ + {% if not collector.queries %} +

+ Query logging is disabled. +

+ {% elseif not collector.querycount %} +

+ No queries. +

+ {% else %} +
    + {% for query in collector.queries %} +
  • +
    + {{ query }} +
    +
  • + {% endfor %} +
+ {% endif %} +{% endblock %} \ No newline at end of file