Skip to content

Commit

Permalink
add ncr_preload_embedded_nodes and ncr_to_node_ref to twig
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbrown committed Jan 21, 2019
1 parent e10358d commit 89b0a9f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG-0.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
This changelog references the relevant changes done in 0.x versions.


## v0.3.8
* Add twig functions in NcrExtension:
* ncr_preload_embedded_nodes
* ncr_to_node_ref


## v0.3.7
* Actually return the preloaded nodes in the NcrExtension.

Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ Autowiring supported for these interfaces:


# Twig Extension
The `NcrExtension` provides one function called `ncr_get_node`. It is important to note
The `NcrExtension` provides a function called `ncr_get_node`. It is important to note
that this does __NOT__ make a query to get a node, instead it pulls from `NcrCache`.

> This might change in the future, but this strategy eliminates horribly performing
Expand All @@ -163,6 +163,15 @@ Here is the creator:
{{ created_by }}
```

__Other twig functions (documentation wip):__

+ ncr_get_preloaded_nodes
+ ncr_get_preloaded_published_nodes
+ ncr_preload_node
+ ncr_preload_nodes
+ ncr_preload_embedded_nodes
+ ncr_to_node_ref


# Console Commands
This library provides the basics for creating and extracting data from the Ncr services.
Expand Down
57 changes: 28 additions & 29 deletions src/Twig/NcrExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Gdbots\Ncr\Exception\NodeNotFound;
use Gdbots\Ncr\NcrCache;
use Gdbots\Ncr\NcrPreloader;
use Gdbots\Pbj\Message;
use Gdbots\Pbj\MessageRef;
use Gdbots\Pbj\WellKnown\Identifier;
use Gdbots\Schemas\Ncr\Mixin\Node\Node;
use Gdbots\Schemas\Ncr\NodeRef;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -56,6 +58,8 @@ public function getFunctions()
new \Twig_SimpleFunction('ncr_get_preloaded_published_nodes', [$this, 'getPreloadedPublishedNodes']),
new \Twig_SimpleFunction('ncr_preload_node', [$this, 'preloadNode']),
new \Twig_SimpleFunction('ncr_preload_nodes', [$this, 'preloadNodes']),
new \Twig_SimpleFunction('ncr_preload_embedded_nodes', [$this, 'preloadEmbeddedNodes']),
new \Twig_SimpleFunction('ncr_to_node_ref', [$this, 'toNodeRef']),
];
}

Expand All @@ -80,7 +84,7 @@ public function getName()
*/
public function getNode($ref): ?Node
{
$nodeRef = $this->refToNodeRef($ref);
$nodeRef = $this->toNodeRef($ref);
if (!$nodeRef instanceof NodeRef) {
return null;
}
Expand Down Expand Up @@ -129,26 +133,12 @@ public function getPreloadedPublishedNodes(): array
*/
public function preloadNode($ref): void
{
$nodeRef = $this->refToNodeRef($ref);
$nodeRef = $this->toNodeRef($ref);
if (!$nodeRef instanceof NodeRef) {
return;
}

try {
$this->ncrPreloader->addNodeRef($nodeRef);
} catch (\Throwable $e) {
if ($this->debug) {
throw $e;
}

$this->logger->error(
sprintf(
'%s::Unable to process twig "ncr_preload_node" function for [{node_ref}].',
ClassUtils::getShortName($e)
),
['exception' => $e, 'node_ref' => (string)$ref]
);
}
$this->ncrPreloader->addNodeRef($nodeRef);
}

/**
Expand All @@ -162,26 +152,35 @@ public function preloadNodes(array $refs = []): void
}

/**
* @param mixed $ref
* @param Message[] $messages Array of messages to extract NodeRefs from.
* @param array $paths An associative array of ['field_name' => 'qname'], i.e. ['user_id', 'acme:user']
*/
public function preloadEmbeddedNodes(array $messages, array $paths = []): void
{
$this->ncrPreloader->addEmbeddedNodeRefs($messages, $paths);
}

/**
* @param mixed $val
*
* @return NodeRef
*/
private function refToNodeRef($ref): ?NodeRef
public function toNodeRef($val): ?NodeRef
{
if ($ref instanceof NodeRef) {
return $ref;
}

if (empty($ref)) {
if ($val instanceof NodeRef) {
return $val;
} else if (empty($val)) {
return null;
}

if ($ref instanceof MessageRef) {
return NodeRef::fromMessageRef($ref);
} else if ($val instanceof MessageRef) {
return NodeRef::fromMessageRef($val);
} else if ($val instanceof Node) {
return NodeRef::fromNode($val);
} else if ($val instanceof Identifier && method_exists($val, 'toNodeRef')) {
return $val->toNodeRef();
}

try {
return NodeRef::fromString((string)$ref);
return NodeRef::fromString((string)$val);
} catch (\Throwable $e) {
return null;
}
Expand Down

0 comments on commit 89b0a9f

Please sign in to comment.