Last active
June 2, 2016 07:55
-
-
Save jadell/1341833 to your computer and use it in GitHub Desktop.
Neo4j and PHP sample application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Everyman\Neo4j\Node, | |
Everyman\Neo4j\Index; | |
class Actor | |
{ | |
public $id = null; | |
public $name = ''; | |
protected $node = null; | |
public static function save(Actor $actor) | |
{ | |
if (!$actor->node) { | |
$actor->node = new Node(Neo4Play::client()); | |
} | |
$actor->node->setProperty('name', $actor->name); | |
$actor->node->save(); | |
$actor->id = $actor->node->getId(); | |
$actorIndex = new Index(Neo4Play::client(), Index::TypeNode, 'actors'); | |
$actorIndex->add($actor->node, 'name', $actor->name); | |
} | |
public static function getActorByName($name) | |
{ | |
$actorIndex = new Index(Neo4Play::client(), Index::TypeNode, 'actors'); | |
$node = $actorIndex->findOne('name', $name); | |
if (!$node) { | |
return null; | |
} | |
$actor = new Actor(); | |
$actor->id = $node->getId(); | |
$actor->name = $node->getProperty('name'); | |
$actor->node = $node; | |
return $actor; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ActorTest extends PHPUnit_Framework_TestCase | |
{ | |
public function testCreateActorAndRetrieveByName() | |
{ | |
$actor = new Actor(); | |
$actor->name = 'Test Guy '.rand(); | |
Actor::save($actor); | |
$actorId = $actor->id; | |
self::assertNotNull($actorId); | |
$retrievedActor = Actor::getActorByName($actor->name); | |
self::assertInstanceOf('Actor', $retrievedActor); | |
self::assertEquals($actor->id, $retrievedActor->id); | |
self::assertEquals($actor->name, $retrievedActor->name); | |
} | |
public function testActorDoesNotExist() | |
{ | |
$retrievedActor = Actor::getActorByName('Test Guy '.rand()); | |
self::assertNull($retrievedActor); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('APPLICATION_ENV', 'testing'); | |
require_once('../bootstrap.php'); | |
$transport->delete('/cleandb/secret-key'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(__DIR__.'/vendor/autoload.php'); | |
require_once(__DIR__.'/lib/Neo4Play.php'); | |
require_once(__DIR__.'/lib/Actor.php'); | |
error_reporting(-1); | |
ini_set('display_errors', 1); | |
if (!defined('APPLICATION_ENV')) { | |
define('APPLICATION_ENV', 'development'); | |
} | |
$host = 'localhost'; | |
$port = (APPLICATION_ENV == 'development') ? 7474 : 7475; | |
$transport = new Everyman\Neo4j\Transport($host, $port); | |
$client = new Everyman\Neo4j\Client($transport); | |
Neo4Play::setClient($client); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('bootstrap.php'); | |
if (!empty($_POST['actorName'])) { | |
$actor = new Actor(); | |
$actor->name = $_POST['actorName']; | |
Actor::save($actor); | |
} else if (!empty($_GET['actorName'])) { | |
$actor = Actor::getActorByName($_GET['actorName']); | |
} | |
?> | |
<form action="" method="POST"> | |
Add Actor Name: <input type="text" name="actorName" /> | |
<input type="submit" value="Add" /> | |
</form> | |
<form action="" method="GET"> | |
Find Actor Name: <input type="text" name="actorName" /> | |
<input type="submit" value="Search" /> | |
</form> | |
<?php if (!empty($actor)) : ?> | |
Name: <?php echo $actor->name; ?><br /> | |
Id: <?php echo $actor->id; ?><br /> | |
<?php elseif (!empty($_GET['actorName'])) : ?> | |
No actor found by the name of "<?php echo $_GET['actorName']; ?>"<br /> | |
<?php endif; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Neo4Play | |
{ | |
protected static $client = null; | |
public static function client() | |
{ | |
return self::$client; | |
} | |
public static function setClient(Everyman\Neo4j\Client $client) | |
{ | |
self::$client = $client; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<phpunit colors="true" bootstrap="./bootstrap-test.php"> | |
<testsuite name="Neo4j Play Test Results"> | |
<directory>./unit</directory> | |
</testsuite> | |
</phpunit> |
what about the /vendor/autoload.php in bootsrap.php ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm following the steps of your blog. But i'm stuck at bootstrap.php file. Which diretory do I need to create it?