Skip to content

Commit

Permalink
Implemented twig content loader to watch dynamic content
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Berezovsky committed Nov 7, 2018
1 parent aad028a commit e2b8bd6
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 10 deletions.
20 changes: 14 additions & 6 deletions Config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\EventListener\EmailSubscriber::class,
'arguments' => [
'mautic.plugin.advanced_templates.helper.template_processor'
],
],
]
]
],
'other' => [
// Template processor
'mautic.plugin.advanced_templates.helper.template_processor' => [
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\Helper\TemplateProcessor::class,
'tag' => 'advanced_templates',
'arguments' => [
'monolog.logger.mautic',
],
'mautic.plugin.advanced_templates.helper.twig_loader_dynamiccontent'
]
],
'mautic.plugin.advanced_templates.helper.twig_loader_dynamiccontent' => [
'class' => \MauticPlugin\MauticAdvancedTemplatesBundle\Helper\Twig_Loader_DynamicContent::class,
'arguments' => [
'monolog.logger.mautic',
'mautic.model.factory',
'mautic.helper.dynamicContent'
]
]
],
],
]
]
];
4 changes: 4 additions & 0 deletions EventListener/EmailSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct(TemplateProcessor $templateProcessor)
*/
public static function getSubscribedEvents()
{
$a = 1 / 0;
return [
EmailEvents::EMAIL_ON_SEND => ['onEmailGenerate', 0],
EmailEvents::EMAIL_ON_DISPLAY => ['onEmailGenerate', 0]
Expand All @@ -44,6 +45,9 @@ public static function getSubscribedEvents()
* Search and replace tokens with content
*
* @param Events\EmailSendEvent $event
* @throws \Throwable
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
*/
public function onEmailGenerate(Events\EmailSendEvent $event)
{
Expand Down
12 changes: 8 additions & 4 deletions Helper/TemplateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ class TemplateProcessor
* @var \Twig_Environment
*/
private $twigEnv;
private $twigDynamicContentLoader;

private static $matchTwigBlockRegex = '/{%\s?TWIG_BLOCK\s?%}(.*?){%\s?END_TWIG_BLOCK\s?%}/ism';

/**
* TemplateProcessor constructor.
* @param LoggerInterface $logger
* @param Twig_Loader_DynamicContent $twigDynamicContentLoader
*/
public function __construct(LoggerInterface $logger)
public function __construct(LoggerInterface $logger, Twig_Loader_DynamicContent $twigDynamicContentLoader)
{
$this->logger = $logger;
$this->twigEnv = new \Twig_Environment(new \Twig_Loader_Array([]));
$this->twigDynamicContentLoader = $twigDynamicContentLoader;
$logger->debug('TemplateProcessor: created $twigDynamicContentLoader');
$this->twigEnv = new \Twig_Environment(new \Twig_Loader_Chain([
$twigDynamicContentLoader, new \Twig_Loader_Array([])
]));
$this->configureTwig($this->twigEnv);
}

Expand All @@ -36,8 +42,6 @@ public function __construct(LoggerInterface $logger)
* @param array $lead
* @return string
* @throws \Throwable
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
*/
public function processTemplate($content, $lead)
{
Expand Down
170 changes: 170 additions & 0 deletions Helper/Twig_Loader_DynamicContent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace MauticPlugin\MauticAdvancedTemplatesBundle\Helper;

use Mautic\CoreBundle\Factory\ModelFactory;
use Mautic\DynamicContentBundle\Entity\DynamicContent;
use Mautic\DynamicContentBundle\Helper\DynamicContentHelper;
use Psr\Log\LoggerInterface;
use Twig_Error_Loader;
use Twig_Source;

class Twig_Loader_DynamicContent implements \Twig_LoaderInterface, \Twig_ExistsLoaderInterface, \Twig_SourceContextLoaderInterface
{
private static $NAME_PREFIX = 'dc:';

/**
* @var ModelFactory
*/
private $modelFactory;
/**
* @var LoggerInterface
*/
protected $logger;
/**
* @var DynamicContentHelper
*/
private $dynamicContentHelper;

/**
* Twig_Loader_DynamicContent constructor.
* @param LoggerInterface $logger
* @param ModelFactory $modelFactory
* @param DynamicContentHelper $dynamicContentHelper
*/
public function __construct(LoggerInterface $logger, ModelFactory $modelFactory, DynamicContentHelper $dynamicContentHelper)
{
$this->modelFactory = $modelFactory;
$this->logger = $logger;
$this->dynamicContentHelper = $dynamicContentHelper;
$logger->debug('Twig_Loader_DynamicContent: created $twigDynamicContentLoader');
}


/**
* Gets the source code of a template, given its name.
*
* @param string $name The name of the template to load
*
* @return string The template source code
*
* @throws Twig_Error_Loader When $name is not found
*
* @deprecated since 1.27 (to be removed in 2.0), implement Twig_SourceContextLoaderInterface
*/
public function getSource($name)
{
@trigger_error(sprintf('Calling "getSource" on "%s" is deprecated since 1.27. Use getSourceContext() instead.', get_class($this)), E_USER_DEPRECATED);
return $this->getSourceContext($name)->getCode();
}

/**
* Gets the cache key to use for the cache for a given template name.
*
* @param string $name The name of the template to load
*
* @return string The cache key
*
* @throws Twig_Error_Loader When $name is not found
*/
public function getCacheKey($name)
{
return $name;
}

/**
* Returns true if the template is still fresh.
*
* @param string $name The template name
* @param int $time Timestamp of the last modification time of the
* cached template
*
* @return bool true if the template is fresh, false otherwise
*
* @throws Twig_Error_Loader When $name is not found
*/
public function isFresh($name, $time)
{
// TODO: Implement isFresh() method.
return false;
}

/**
* Returns the source context for a given template logical name.
*
* @param string $name The template logical name
*
* @return Twig_Source
*
* @throws Twig_Error_Loader When $name is not found
*/
public function getSourceContext($name)
{
$dynamicContent = $this->findTemplate($this->aliasForTemplateName($name));
if ($dynamicContent == null) {
throw new Twig_Error_Loader('Template ' . $name . ' does not exist');
}
return new Twig_Source($dynamicContent->getContent(), $name);

}

private function aliasForTemplateName($name)
{
return str_replace(Twig_Loader_DynamicContent::$NAME_PREFIX, '', $name);
}

/**
* @param $resourceAlias
* @return null|DynamicContent
*/
private function findTemplate($resourceAlias)
{
$model = $this->modelFactory->getModel('dynamicContent');
$this->logger->debug('Twig_Loader_DynamicContent: Loading dynamic content by alias: ' . $resourceAlias);
$result = $model->getEntities(
[
'filter' => [
'where' => [
[
'col' => 'e.name',
'expr' => 'eq',
'val' => $resourceAlias,
],
[
'col' => 'e.isPublished',
'expr' => 'eq',
'val' => 1,
]
]
],
'ignore_paginator' => true,
]);

if (count($result) === 0) {
return null;
}
return $result[1]; // Strange, but result is in the element 1 not 0...
}

/**
* Check if we have the source code of a template, given its name.
*
* @param string $name The name of the template to check if we can load
*
* @return bool If the template source code is handled by this loader or not
*/
public function exists($name)
{
$this->logger->debug('Twig_Loader_DynamicContent: EXISTS called for ' . $name);
return $this->supports($name) && $this->findTemplate($this->aliasForTemplateName($name)) !== null;
}

/**
* @param $name
* @return bool
*/
public function supports($name)
{
return strpos($name, Twig_Loader_DynamicContent::$NAME_PREFIX) === 0;
}
}

0 comments on commit e2b8bd6

Please sign in to comment.