Skip to content

Commit

Permalink
added the concept of Context
Browse files Browse the repository at this point in the history
  • Loading branch information
Emre Sokullu committed May 1, 2017
1 parent 0c78e05 commit 75e309d
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/Pho/Framework/AbstractEdge.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ abstract class AbstractEdge extends \Pho\Lib\Graph\Edge {
/**
* When invoked, returns the head node.
*
* @return NodeInterface
* @return ParticleInterface
*/
public function __invoke(): \Pho\Lib\Graph\NodeInterface
public function __invoke(): ParticleInterface
{
return $this->head()->node();
}
Expand Down
12 changes: 5 additions & 7 deletions src/Pho/Framework/AclCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Pho\Framework;

use Pho\Lib\Graph;

/**
* AclCore (Access Control Lists Core)
*
Expand All @@ -35,18 +33,18 @@ class AclCore {

/**
* In what context this node was created. Must point to a node
* that implements Pho\Lib\Graph\GraphInterface
* @var Pho\Lib\Graph\GraphInterface
* that implements ContextInterface
* @var ContextInterface
*/
protected $context;

/**
* Constructor.
*
* @param Actor $creator The creator of this node.
* @param \Pho\Lib\Graph\GraphInterface $context The context in which this node is created and will exist
* @param ContextInterface $context The context in which this node is created and will exist
*/
public function __construct(Actor $creator, Graph\GraphInterface $context) {
public function __construct(Actor $creator, ContextInterface $context) {
$this->creator = $creator;
$this->context = $context;
}
Expand All @@ -63,7 +61,7 @@ public function toArray(): array
//eval(\Psy\sh());
return [
"creator" => (string) $this->creator->id(),
"context" => ($this->context instanceof Graph\Graph) ? Graph\Graph::class : (string) $this->context->id()
"context" => ($this->context instanceof Graph) ? Graph::class : (string) $this->context->id()
];
}

Expand Down
76 changes: 72 additions & 4 deletions src/Pho/Framework/Actor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Pho\Framework;

use Pho\Lib\Graph;

/**
* The Actor Particle
*
Expand All @@ -25,10 +23,17 @@
*
* @author Emre Sokullu <[email protected]>
*/
class Actor extends Graph\Node implements ParticleInterface {
class Actor extends \Pho\Lib\Graph\Node implements ParticleInterface {

use ParticleTrait;

/**
* Current context that this actor is in.
*
* @var ContextInterface
*/
protected $current_context;

/**
* Incoming Edges
*
Expand All @@ -38,10 +43,73 @@ class Actor extends Graph\Node implements ParticleInterface {
*/
const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ObjectOut\Transmits::class];

public function __construct(Graph\GraphInterface $context) {
public function __construct(ContextInterface $context) {
parent::__construct($context);
$this->acl = new AclCore($this, $context);
$this->enter($context);
$this->setupEdges();
}

/**
* Puts the Actor into a context
*
* This is importnat because All particles formed by the Actor
* will be associated with their current context.
*
* @see Actor:cwd for UNIX-style alias.
*
* @param ContextInterface $context
*
* @return void
*/
public function enter(ContextInterface $context): void
{
$this->current_context = $context;
}

/**
* Alias to enter()
*
* This is a UNIX alias to the ```enter()``` method.
*
* @see Actor::enter
*
* @param ContextInterface $context
*
* @return void
*/
public function cwd(ContextInterface $context): void
{
$this->enter($context);
}

/**
* Returns which context the Actor is currently operating
*
* This is importnat because All particles formed by the Actor
* will be associated with their current context.
*
* @see Actor:pwd for UNIX-style alias.
*
* @return ContextInterface Current context where the Actor is operating.
*/
public function where(): ContextInterface
{
return $this->current_context;
}

/**
* Alias to where()
*
* This is a UNIX alias to the ```where()``` method.
*
* @see Actor::where
*
* @return ContextInterface Current context where the Actor is operating.
*/
public function pwd(): ContextInterface
{
return $this->where();
}

}
25 changes: 25 additions & 0 deletions src/Pho/Framework/ContextInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Pho package.
*
* (c) Emre Sokullu <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pho\Framework;

/**
* ContextInterface
*
* This class is a shell to Pho\Lib\Graph's GraphInterface implementation.
* It is used by all Framework particles and graphs that can be used
* as a context object in higher level packages.
*
* @author Emre Sokullu <[email protected]>
*/
interface ContextInterface extends \Pho\Lib\Graph\GraphInterface {

}
10 changes: 4 additions & 6 deletions src/Pho/Framework/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Pho\Framework;

use Pho\Lib\Graph;

/**
* The Frame Particle
*
Expand All @@ -24,14 +22,14 @@
* any confusions with Pho\Lib\Graph's nomenclature, this class is called
* Frame instead.
*
* In contrast to other (particles?), Frame doesn't contain edges but
* its **"contains"** edge acts similarly to an edge.
* In contrast to other particles, Frame doesn't contain edges but
* its **"contains"** method acts similarly to an edge.
*
*
*
* @author Emre Sokullu <[email protected]>
*/
class Frame extends Graph\SubGraph implements ParticleInterface {
class Frame extends \Pho\Lib\Graph\SubGraph implements ParticleInterface, ContextInterface {

use ParticleTrait;

Expand All @@ -44,7 +42,7 @@ class Frame extends Graph\SubGraph implements ParticleInterface {
*/
const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ActorOut\Writes::class, ObjectOut\Transmits::class];

public function __construct(Actor $creator, Graph\GraphInterface $context) {
public function __construct(Actor $creator, ContextInterface $context) {
parent::__construct($context);
$this->acl = new AclCore($creator, $context);
$this->setupEdges();
Expand Down
25 changes: 25 additions & 0 deletions src/Pho/Framework/Graph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Pho package.
*
* (c) Emre Sokullu <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pho\Framework;

/**
* The Graph
*
* This class is a shell to Pho\Lib\Graph's Graph implementation
* and it implements ContextInterface to give higher-level
* software access to use both Frame and Graph as context objects.
*
* @author Emre Sokullu <[email protected]>
*/
class Graph extends \Pho\Lib\Graph\Graph implements ContextInterface {

}
4 changes: 1 addition & 3 deletions src/Pho/Framework/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Pho\Framework;

use Pho\Lib\Graph;

/**
* The Object Particle
*
Expand All @@ -36,7 +34,7 @@ class Object extends \Pho\Lib\Graph\Node implements ParticleInterface {
*/
const EDGES_IN = [ActorOut\Reads::class, ActorOut\Subscribes::class, ActorOut\Writes::class, ObjectOut\Transmits::class];

public function __construct(Actor $creator, Graph\GraphInterface $context) {
public function __construct(Actor $creator, ContextInterface $context) {
parent::__construct($context);
$this->acl = new AclCore($creator, $context);
$this->setupEdges();
Expand Down
13 changes: 10 additions & 3 deletions tests/Pho/Framework/SimpleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

namespace Pho\Framework;

use Pho\Lib\Graph;
use Pho\Lib\Graph\Predicate;

class SimpleTest extends \PHPUnit\Framework\TestCase
{
private $graph;

public function setUp() {
$this->graph = new Graph\Graph();
$this->graph = new Graph();
}

public function tearDown() {
Expand All @@ -36,7 +36,7 @@ public function testActorEdge() {
$object = new Object($actor, $this->graph);
$edge = $actor->writes($object);
$this->assertInstanceOf(ActorOut\Writes::class, $edge);
$this->assertInstanceOf(Graph\Predicate::class, $edge->predicate());
$this->assertInstanceOf(Predicate::class, $edge->predicate());
}

public function testActorPredicate() {
Expand Down Expand Up @@ -128,4 +128,11 @@ public function testFrameToArray() {
$this->assertEquals($actor->id(), $array["acl"]["creator"]);
}

public function testContextInterface() {
$this->assertInstanceOf(ContextInterface::class, $this->graph);
$actor = new Actor($this->graph);
$frame = new Frame($actor, $this->graph);
$this->assertInstanceOf(ContextInterface::class, $frame);
}

}

0 comments on commit 75e309d

Please sign in to comment.