Skip to content

Commit

Permalink
autoRegisterOutgoingEdges
Browse files Browse the repository at this point in the history
  • Loading branch information
Emre Sokullu committed Jul 23, 2017
1 parent 427e5fa commit c70e0f3
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/Pho/Framework/ParticleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public function initializeParticle()
ObjectOut\Mention::class
);

$this->autoRegisterOutgoingEdges();

if(method_exists($this, "onIncomingEdgeRegistration")) {
$this->onIncomingEdgeRegistration();
}
Expand All @@ -98,6 +100,46 @@ protected function initializeHandler(): void
->deploy($this->handler->cargo_fields);
}


/**
* Auto-registers outgoing edge classes
*
* Auto-registration is done by directory structure. Directories that sit
* in this folder, and are named after this class with
* "Out" suffix (such as "MyNodeOut" for a node class named "MyNode")
* would be candidate for auto-registration.
*
* Please note, this does not check if it's actually an Edge class.
* The check is done by the OutgoingEdgeLoader class.
*
* @return void
*/
protected function autoRegisterOutgoingEdges(): void
{
$self_reflector = new \ReflectionObject($this);
if($self_reflector->isAnonymous()) {
return;
}

$edge_dir =
dirname($self_reflector->getFileName()) .
DIRECTORY_SEPARATOR .
$self_reflector->getShortName()
. "Out";
// !!! do not replace this with __DIR__

if(!file_exists($edge_dir)) {
Logger::info("Edge directory %s does not exist", $edge_dir);
return;
}

$locator = new \Zend\File\ClassFileLocator($edge_dir);
foreach ($locator as $file) {
$filename = str_replace($edge_dir . DIRECTORY_SEPARATOR, '', $file->getRealPath());
$this->addEdges("outgoing", ...$file->getClasses());
}
}

/**
* Registers the incoming edges.
*
Expand Down

0 comments on commit c70e0f3

Please sign in to comment.