Skip to content

Commit

Permalink
Merge pull request #93 from doctrine/inject-environment-in-nodes
Browse files Browse the repository at this point in the history
inject environment in nodes
  • Loading branch information
jwage authored Jan 31, 2019
2 parents f657b6d + a083924 commit 908578a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 31 deletions.
51 changes: 26 additions & 25 deletions lib/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,37 +207,37 @@ public function setFileExtension(string $fileExtension) : void
$this->fileExtension = $fileExtension;
}

public function getNodeFactory() : NodeFactory
public function getNodeFactory(Environment $environment) : NodeFactory
{
if ($this->nodeFactory !== null) {
return $this->nodeFactory;
}

return new DefaultNodeFactory(
$this->eventManager,
$this->createNodeInstantiator(NodeTypes::DOCUMENT, Nodes\DocumentNode::class),
$this->createNodeInstantiator(NodeTypes::SPAN, Nodes\SpanNode::class),
$this->createNodeInstantiator(NodeTypes::TOC, Nodes\TocNode::class),
$this->createNodeInstantiator(NodeTypes::TITLE, Nodes\TitleNode::class),
$this->createNodeInstantiator(NodeTypes::SEPARATOR, Nodes\SeparatorNode::class),
$this->createNodeInstantiator(NodeTypes::CODE, Nodes\CodeNode::class),
$this->createNodeInstantiator(NodeTypes::QUOTE, Nodes\QuoteNode::class),
$this->createNodeInstantiator(NodeTypes::PARAGRAPH, Nodes\ParagraphNode::class),
$this->createNodeInstantiator(NodeTypes::ANCHOR, Nodes\AnchorNode::class),
$this->createNodeInstantiator(NodeTypes::LIST, Nodes\ListNode::class),
$this->createNodeInstantiator(NodeTypes::TABLE, Nodes\TableNode::class),
$this->createNodeInstantiator(NodeTypes::DEFINITION_LIST, Nodes\DefinitionListNode::class),
$this->createNodeInstantiator(NodeTypes::WRAPPER, Nodes\WrapperNode::class),
$this->createNodeInstantiator(NodeTypes::FIGURE, Nodes\FigureNode::class),
$this->createNodeInstantiator(NodeTypes::IMAGE, Nodes\ImageNode::class),
$this->createNodeInstantiator(NodeTypes::META, Nodes\MetaNode::class),
$this->createNodeInstantiator(NodeTypes::RAW, Nodes\RawNode::class),
$this->createNodeInstantiator(NodeTypes::DUMMY, Nodes\DummyNode::class),
$this->createNodeInstantiator(NodeTypes::MAIN, Nodes\MainNode::class),
$this->createNodeInstantiator(NodeTypes::BLOCK, Nodes\BlockNode::class),
$this->createNodeInstantiator(NodeTypes::CALLABLE, Nodes\CallableNode::class),
$this->createNodeInstantiator(NodeTypes::SECTION_BEGIN, Nodes\SectionBeginNode::class),
$this->createNodeInstantiator(NodeTypes::SECTION_END, Nodes\SectionEndNode::class)
$this->createNodeInstantiator($environment, NodeTypes::DOCUMENT, Nodes\DocumentNode::class),
$this->createNodeInstantiator($environment, NodeTypes::SPAN, Nodes\SpanNode::class),
$this->createNodeInstantiator($environment, NodeTypes::TOC, Nodes\TocNode::class),
$this->createNodeInstantiator($environment, NodeTypes::TITLE, Nodes\TitleNode::class),
$this->createNodeInstantiator($environment, NodeTypes::SEPARATOR, Nodes\SeparatorNode::class),
$this->createNodeInstantiator($environment, NodeTypes::CODE, Nodes\CodeNode::class),
$this->createNodeInstantiator($environment, NodeTypes::QUOTE, Nodes\QuoteNode::class),
$this->createNodeInstantiator($environment, NodeTypes::PARAGRAPH, Nodes\ParagraphNode::class),
$this->createNodeInstantiator($environment, NodeTypes::ANCHOR, Nodes\AnchorNode::class),
$this->createNodeInstantiator($environment, NodeTypes::LIST, Nodes\ListNode::class),
$this->createNodeInstantiator($environment, NodeTypes::TABLE, Nodes\TableNode::class),
$this->createNodeInstantiator($environment, NodeTypes::DEFINITION_LIST, Nodes\DefinitionListNode::class),
$this->createNodeInstantiator($environment, NodeTypes::WRAPPER, Nodes\WrapperNode::class),
$this->createNodeInstantiator($environment, NodeTypes::FIGURE, Nodes\FigureNode::class),
$this->createNodeInstantiator($environment, NodeTypes::IMAGE, Nodes\ImageNode::class),
$this->createNodeInstantiator($environment, NodeTypes::META, Nodes\MetaNode::class),
$this->createNodeInstantiator($environment, NodeTypes::RAW, Nodes\RawNode::class),
$this->createNodeInstantiator($environment, NodeTypes::DUMMY, Nodes\DummyNode::class),
$this->createNodeInstantiator($environment, NodeTypes::MAIN, Nodes\MainNode::class),
$this->createNodeInstantiator($environment, NodeTypes::BLOCK, Nodes\BlockNode::class),
$this->createNodeInstantiator($environment, NodeTypes::CALLABLE, Nodes\CallableNode::class),
$this->createNodeInstantiator($environment, NodeTypes::SECTION_BEGIN, Nodes\SectionBeginNode::class),
$this->createNodeInstantiator($environment, NodeTypes::SECTION_END, Nodes\SectionEndNode::class)
);
}

Expand Down Expand Up @@ -277,11 +277,12 @@ public function getFormat() : Format
return $this->formats[$this->fileExtension];
}

private function createNodeInstantiator(string $type, string $nodeClassName) : NodeInstantiator
private function createNodeInstantiator(Environment $environment, string $type, string $nodeClassName) : NodeInstantiator
{
return new NodeInstantiator(
$type,
$nodeClassName,
$environment,
$this->getNodeRendererFactory($nodeClassName),
$this->eventManager
);
Expand Down
2 changes: 1 addition & 1 deletion lib/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public function setMetas(Metas $metas) : void

public function getNodeFactory() : NodeFactory
{
return $this->configuration->getNodeFactory();
return $this->configuration->getNodeFactory($this);
}

public function getTemplateRenderer() : TemplateRenderer
Expand Down
7 changes: 7 additions & 0 deletions lib/NodeFactory/NodeInstantiator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\RST\NodeFactory;

use Doctrine\Common\EventManager;
use Doctrine\RST\Environment;
use Doctrine\RST\Nodes\Node;
use Doctrine\RST\Nodes\NodeTypes;
use Doctrine\RST\Renderers\NodeRendererFactory;
Expand All @@ -26,10 +27,13 @@ class NodeInstantiator

/** @var EventManager|null */
private $eventManager;
/** @var Environment */
private $environment;

public function __construct(
string $type,
string $className,
Environment $environment,
?NodeRendererFactory $nodeRendererFactory = null,
?EventManager $eventManager = null
) {
Expand All @@ -49,6 +53,7 @@ public function __construct(
$this->className = $className;
$this->nodeRendererFactory = $nodeRendererFactory;
$this->eventManager = $eventManager;
$this->environment = $environment;
}

public function getType() : string
Expand All @@ -72,6 +77,8 @@ public function create(array $arguments) : Node
$node->setEventManager($this->eventManager);
}

$node->setEnvironment($this->environment);

return $node;
}
}
14 changes: 14 additions & 0 deletions lib/Nodes/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Common\EventArgs;
use Doctrine\Common\EventManager;
use Doctrine\RST\Environment;
use Doctrine\RST\Event\PostNodeRenderEvent;
use Doctrine\RST\Event\PreNodeRenderEvent;
use Doctrine\RST\Renderers\DefaultNodeRenderer;
Expand All @@ -25,6 +26,9 @@ abstract class Node
/** @var EventManager|null */
private $eventManager;

/** @var Environment|null */
protected $environment;

/** @var Node|string|null */
protected $value;

Expand All @@ -49,6 +53,16 @@ public function setEventManager(EventManager $eventManager) : void
$this->eventManager = $eventManager;
}

public function setEnvironment(Environment $environment) : void
{
$this->environment = $environment;
}

public function getEnvironment() : ?Environment
{
return $this->environment;
}

public function render() : string
{
$this->dispatchEvent(
Expand Down
2 changes: 1 addition & 1 deletion lib/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function getSubParser() : Parser

public function getNodeFactory() : NodeFactory
{
return $this->configuration->getNodeFactory();
return $this->configuration->getNodeFactory($this->environment);
}

/**
Expand Down
12 changes: 8 additions & 4 deletions tests/NodeInstantiatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ class NodeInstantiatorTest extends TestCase
{
public function testGetType() : void
{
$nodeInstantiator = new NodeInstantiator(NodeTypes::DOCUMENT, DocumentNode::class);
$environment = $this->createMock(Environment::class);

$nodeInstantiator = new NodeInstantiator(NodeTypes::DOCUMENT, DocumentNode::class, $environment);

self::assertSame(NodeTypes::DOCUMENT, $nodeInstantiator->getType());
}
Expand All @@ -25,15 +27,17 @@ public function testInvalidTypeThrowsInvalidArgumentException() : void
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Node type invalid is not a valid node type.');

$nodeInstantiator = new NodeInstantiator('invalid', DocumentNode::class);
$environment = $this->createMock(Environment::class);

$nodeInstantiator = new NodeInstantiator('invalid', DocumentNode::class, $environment);
}

public function testCreate() : void
{
$nodeInstantiator = new NodeInstantiator(NodeTypes::DOCUMENT, DocumentNode::class);

$environment = $this->createMock(Environment::class);

$nodeInstantiator = new NodeInstantiator(NodeTypes::DOCUMENT, DocumentNode::class, $environment);

$document = $nodeInstantiator->create([$environment]);

self::assertInstanceOf(DocumentNode::class, $document);
Expand Down

0 comments on commit 908578a

Please sign in to comment.