Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bazo committed Jul 23, 2012
1 parent d0a0ea7 commit 680c4bd
Show file tree
Hide file tree
Showing 4 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
nbproject/*
80 changes: 80 additions & 0 deletions Bazo/Extensions/Neo4j/DI/Neo4jExtension.php
@@ -0,0 +1,80 @@
<?php
namespace Bazo\Extensions\Neo4j\DI;

use Doctrine\Common\ClassLoader,
Doctrine\Common\Annotations\AnnotationReader;

/**
* Description of Neo4jExtension
*
* @author Martin Bažík
*/
class Neo4jExtension extends \Nette\Config\CompilerExtension
{
/**
* @var array
*/
public $defaults = array(
'host' => 'localhost',
'port' => 7474
);

/**
* Processes configuration data
*
* @return void
*/
public function loadConfiguration()
{

$container = $this->getContainerBuilder();

$config = $this->getConfig($this->defaults);

$container->addDefinition($this->prefix('neo4jClient'))
->setClass('\Everyman\Neo4j\Client')
->setFactory('Extensions\EntityManagerExtension::createNeo4jClient', array('@container', $config))
->setAutowired(FALSE);

$container->addDefinition($this->prefix('entityManager'))
->setClass('\HireVoice\Neo4j\EntityManager')
->setFactory('Extensions\EntityManagerExtension::createEntityManager', array('@container', $config))
->setAutowired(FALSE);

$container->addDefinition('entityManager')
->setClass('\HireVoice\Neo4j\EntityManager')
->setFactory('@container::getService', array($this->prefix('entityManager')));
}

public static function createNeo4jClient(\Nette\DI\Container $container, $config)
{
return new \Everyman\Neo4j\Client($config['host'], $config['port']);
}

public static function createEntityManager(\Nette\DI\Container $container, $config)
{
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(VENDOR_DIR . '/hirevoice/neo4jphp-ogm/lib/HireVoice/Neo4j/Annotation/Auto.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(VENDOR_DIR . '/hirevoice/neo4jphp-ogm/lib/HireVoice/Neo4j/Annotation/Entity.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(VENDOR_DIR . '/hirevoice/neo4jphp-ogm/lib/HireVoice/Neo4j/Annotation/Index.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(VENDOR_DIR . '/hirevoice/neo4jphp-ogm/lib/HireVoice/Neo4j/Annotation/ManyToMany.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(VENDOR_DIR . '/hirevoice/neo4jphp-ogm/lib/HireVoice/Neo4j/Annotation/ManyToOne.php');
\Doctrine\Common\Annotations\AnnotationRegistry::registerFile(VENDOR_DIR . '/hirevoice/neo4jphp-ogm/lib/HireVoice/Neo4j/Annotation/Property.php');

$metadataCache = new $config['metaDataCacheClass'];
$metadataCache->setNamespace($config['cachePrefix']);

$reader = new \Doctrine\Common\Annotations\CachedReader(
new AnnotationReader,
$metadataCache,
false
);

$client = $container->neo4jClient;
$metaRepository = new \HireVoice\Neo4j\Meta\Repository($reader);
$em = new \HireVoice\Neo4j\EntityManager($client, $metaRepository);
$em->setProxyFactory(new \HireVoice\Neo4j\Proxy\Factory($config['proxyDir'], true));
return $em;
}


}
139 changes: 139 additions & 0 deletions Bazo/Extensions/Neo4j/Diagnostics/Panel.php
@@ -0,0 +1,139 @@
<?php
/**
* Copyright (c) 2012 Martin Bazik <martin@bazo.sk>
*
* For the full copyright and license information, please view the file license.txt that was distributed with this source code.
*/

namespace Bazo\Extensions\Neo4j\Diagnostics;

use Everyman\Neo4j\Query;
use HireVoice\Neo4j;

use Nette;
use Nette\Diagnostics\Debugger;

/**
* @author Martin Bazik <martin@bazo.sk>
*/
class Panel extends Nette\Object implements Nette\Diagnostics\IBarPanel
{

/**
* @var int
*/
public static $maxLength = 1000;

/**
* @var int
*/
private $totalTime = 0;

/**
* @var array
*/
private $queries = array();

private function getQueryType(Query $query)
{
$types = array(
'Everyman\Neo4j\Cypher\Query' => 'cypher',
'Everyman\Neo4j\Gremlin\Query' => 'gremlin'
);
$class = get_class($query);
return $types[$class];
}

public function addQuery(Query $query, $parameters, $time)
{
$type = $this->getQueryType($query);
$queryEntry = (object)array(
'type' => $type,
'query'=> $query->getQuery(),
'parameters' => $parameters,
'time' => $time,
'results' => $query->getResultSet()->count()
);

$this->totalTime += $time;
$this->queries[] = $queryEntry;
}

/**
* Renders HTML code for custom tab.
* @return string
*/
public function getTab()
{
return '<span title="Neo4j">' .
'<img alt="" src="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAAQABADASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAACAYH/8QAHxAAAgIDAQEAAwAAAAAAAAAABAUDBgECBwgJEhMU/8QAFQEBAQAAAAAAAAAAAAAAAAAABwn/xAAdEQADAAMBAQEBAAAAAAAAAAADBAUBAgYTEgcR/9oADAMBAAIRAxEAPwCP5DdOE1vy5cq3buF0y99UtPQq7HVLy+KKFlRLBxUDCUBjMDquarkO2F7eAvau2IBg4w8IiP2AiXhmStP2r8wecefvOLrtlUu5k9gr0yTaw182EHarMYrI1DTYFpO0ukj8TCwpnCSLh05sJRqoUmWciMjX884B846d5m6cd0qk+l+gp67Wcoau+r1ae3IGkp3btXlyMU71fElgSQOawAftEIGGwFnNAdtckxGLxS4cHr0r2aawlH8iqvULrd+N8zd2hPQyLRfnVlEsCsBu2Erb6FYXKMmXwj1zYJalhAUwSQBfu3/o20NwMMltAuP/AKSOTydzoIAIVpTpu5FSnuUYnToV0pwl50Wg+coU/AM86xk0BrrhYdK4L0ZSIMc6B7Iig+zyyLO7SeykzIyCA4k0swXJTHGuPJD4JhgZsEb31+tQ+P8APncW5P/Z" />' .
count($this->queries) . ' queries' .
($this->queries ? ' / ' . sprintf('%0.1f', $this->totalTime * 1000) . ' ms' : '') .
'</span>';
}

/**
* Renders HTML code for custom panel.
* @return string
*/
public function getPanel()
{
$s = '';
$h = 'htmlSpecialChars';
foreach ($this->queries as $query) {
$s .= '<tr><td>' . sprintf('%0.3f', $query->time * 1000000).'</td>';
$s .= '<td class="neo4j-query">' .
$h(self::$maxLength ? Nette\Utils\Strings::truncate($query->query, self::$maxLength) : $query->query)
.'</td>';
$s .= '<td>'.$query->results.'</td>';
$s .= '</tr>';
}

return empty($this->queries) ? '' :
'<style> #nette-debug div.neo4j-panel table td { text-align: right }
#nette-debug div.neo4j-panel table td.neo4j-query { background: white !important; text-align: left } </style>
<h1>Queries: ' . count($this->queries) . ($this->totalTime ? ', time: ' . sprintf('%0.3f', $this->totalTime * 1000) . ' ms' : '') . '</h1>
<div class="nette-inner neo4j-panel">
<table>
<tr><th>Time&nbsp;µs</th><th>Query</th><th>Results</th></tr>' . $s . '
</table>
</div>';
}

/**
* @param Neo4j\Exception $e
* @return type
*/
public static function renderException(Neo4j\Exception $e)
{
if ($e instanceof Neo4j\Exception) {
$panel = NULL;
if ($e->getQuery() !== null) {
$panel .= '<h3>Query</h3>'
. '<pre class="nette-dump"><span class="php-string">'
. $e->getQuery()->getQuery()
. '</span></pre>';
}

if ($panel !== NULL) {
$panel = array(
'tab' => 'Neo4j',
'panel' => $panel
);
}

return $panel;
}
}

/**
* @return \Extensions\Neo4j\Diagnostics\Panel
*/
public static function register()
{
$panel = new static();
Debugger::$blueScreen->addPanel(array($panel, 'renderException'));
Debugger::$bar->addPanel($panel);
return $panel;
}
}
21 changes: 21 additions & 0 deletions composer.json
@@ -0,0 +1,21 @@
{
"name": "bazo/nette-neo4j-extension",
"type": "library",
"description": "Nette Neo4j compiler extension",
"keywords": ["nette", "neo4j", "graph", "database", "extension"],
"homepage": "https://github.com/bazo/nette-neo4j-extension",
"authors": [
{
"name": "Martin Bažík",
"email": "martin@bazo.sk",
"homepage": "http://bazo.sk"
}
],
"require": {
"php": ">=5.3.0",
"hirevoice/neo4jphp-ogm": "*"
},
"autoload": {
"psr-0": { "Bazo\\Extensions\\Neo4j": "" }
}
}

0 comments on commit 680c4bd

Please sign in to comment.