Refactor Preprocessor to more classes

[MAILPOET-5591]
This commit is contained in:
Jan Lysý
2023-10-12 17:30:45 +02:00
committed by Jan Lysý
parent 125b0dfe82
commit c1768fd0b2
7 changed files with 75 additions and 18 deletions

View File

@@ -336,10 +336,11 @@ class ContainerConfigurator implements IContainerConfigurator {
$container->autowire(\MailPoet\EmailEditor\Engine\EmailEditor::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\EmailEditor::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\EmailApiController::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\EmailApiController::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\SettingsController::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\SettingsController::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\Preprocessors\TopLevelPreprocessor::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\Renderer::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\Renderer\Renderer::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\BlocksRenderer::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\Renderer\BlocksRenderer::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\BlocksRegistry::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\Renderer\BlocksRegistry::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\Renderer\Preprocessor::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\Renderer\PreprocessManager::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Engine\StylesController::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Engine\StylesController::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Integrations\Core\Initializer::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Integrations\Core\Initializer::class)->setPublic(true);
$container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\EmailEditor::class)->setPublic(true); $container->autowire(\MailPoet\EmailEditor\Integrations\MailPoet\EmailEditor::class)->setPublic(true);

View File

@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer;
use MailPoet\EmailEditor\Engine\Renderer\Preprocessors\Preprocessor;
use MailPoet\EmailEditor\Engine\Renderer\Preprocessors\TopLevelPreprocessor;
class PreprocessManager {
/** @var Preprocessor[] */
private $preprocessors = [];
public function __construct(
TopLevelPreprocessor $topLevelPreprocessor
) {
$this->registerPreprocessor($topLevelPreprocessor);
}
/**
* @param array $parsedBlocks
* @return array
*/
public function preprocess(array $parsedBlocks): array {
foreach ($this->preprocessors as $preprocessor) {
$parsedBlocks = $preprocessor->preprocess($parsedBlocks);
}
return $parsedBlocks;
}
public function registerPreprocessor(Preprocessor $preprocessor): void {
$this->preprocessors[] = $preprocessor;
}
}

View File

@@ -0,0 +1,7 @@
<?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer\Preprocessors;
interface Preprocessor {
public function preprocess(array $parsedBlocks): array;
}

View File

@@ -1,9 +1,8 @@
<?php declare(strict_types = 1); <?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer; namespace MailPoet\EmailEditor\Engine\Renderer\Preprocessors;
class Preprocessor {
class TopLevelPreprocessor implements Preprocessor {
const SINGLE_COLUMN_TEMPLATE = [ const SINGLE_COLUMN_TEMPLATE = [
'blockName' => 'core/columns', 'blockName' => 'core/columns',
'attrs' => [], 'attrs' => [],
@@ -14,16 +13,12 @@ class Preprocessor {
]], ]],
]; ];
public function preprocess(array $parsedBlocks): array {
return $this->addTopLevelColumns($parsedBlocks);
}
/** /**
* In the editor we allow putting content blocks directly into the root level of the email. * In the editor we allow putting content blocks directly into the root level of the email.
* But for rendering purposes it is more convenient to have them wrapped in a single column. * But for rendering purposes it is more convenient to have them wrapped in a single column.
* This method walks through the first level of blocks and wraps non column blocks into a single column. * This method walks through the first level of blocks and wraps non column blocks into a single column.
*/ */
private function addTopLevelColumns(array $parsedBlocks): array { public function preprocess(array $parsedBlocks): array {
$wrappedParsedBlocks = []; $wrappedParsedBlocks = [];
$nonColumnsBlocksBuffer = []; $nonColumnsBlocksBuffer = [];
foreach ($parsedBlocks as $block) { foreach ($parsedBlocks as $block) {

View File

@@ -14,8 +14,8 @@ class Renderer {
/** @var BlocksRenderer */ /** @var BlocksRenderer */
private $blocksRenderer; private $blocksRenderer;
/** @var Preprocessor */ /** @var PreprocessManager */
private $preprocessor; private $preprocessManager;
/** @var StylesController */ /** @var StylesController */
private $stylesController; private $stylesController;
@@ -28,12 +28,12 @@ class Renderer {
*/ */
public function __construct( public function __construct(
\MailPoetVendor\CSS $cssInliner, \MailPoetVendor\CSS $cssInliner,
Preprocessor $preprocessor, PreprocessManager $preprocessManager,
BlocksRenderer $blocksRenderer, BlocksRenderer $blocksRenderer,
StylesController $stylesController StylesController $stylesController
) { ) {
$this->cssInliner = $cssInliner; $this->cssInliner = $cssInliner;
$this->preprocessor = $preprocessor; $this->preprocessManager = $preprocessManager;
$this->blocksRenderer = $blocksRenderer; $this->blocksRenderer = $blocksRenderer;
$this->stylesController = $stylesController; $this->stylesController = $stylesController;
} }
@@ -42,7 +42,7 @@ class Renderer {
$parser = new \WP_Block_Parser(); $parser = new \WP_Block_Parser();
$parsedBlocks = $parser->parse($post->post_content); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps $parsedBlocks = $parser->parse($post->post_content); // phpcs:ignore Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
$parsedBlocks = $this->preprocessor->preprocess($parsedBlocks, $this->stylesController->getEmailLayoutStyles()); $parsedBlocks = $this->preprocessManager->preprocess($parsedBlocks);
$renderedBody = $this->blocksRenderer->render($parsedBlocks); $renderedBody = $this->blocksRenderer->render($parsedBlocks);
$styles = (string)file_get_contents(dirname(__FILE__) . '/' . self::TEMPLATE_STYLES_FILE); $styles = (string)file_get_contents(dirname(__FILE__) . '/' . self::TEMPLATE_STYLES_FILE);

View File

@@ -0,0 +1,20 @@
<?php declare(strict_types = 1);
namespace unit\EmailEditor\Engine\Renderer;
use MailPoet\EmailEditor\Engine\Renderer\PreprocessManager;
use MailPoet\EmailEditor\Engine\Renderer\Preprocessors\TopLevelPreprocessor;
class PreprocessManagerTest extends \MailPoetUnitTest {
public function testItCallsPreprocessorsProperly(): void {
$topLevel = $this->createMock(TopLevelPreprocessor::class);
$topLevel->expects($this->once())->method('preprocess')->willReturn([]);
$secondPreprocessor = $this->createMock(TopLevelPreprocessor::class);
$secondPreprocessor->expects($this->once())->method('preprocess')->willReturn([]);
$preprocessManager = new PreprocessManager($topLevel);
$preprocessManager->registerPreprocessor($secondPreprocessor);
expect($preprocessManager->preprocess([]))->equals([]);
}
}

View File

@@ -1,8 +1,10 @@
<?php declare(strict_types = 1); <?php declare(strict_types = 1);
namespace MailPoet\EmailEditor\Engine\Renderer; namespace unit\EmailEditor\Engine\Renderer\Preprocessors;
class PreprocessorTest extends \MailPoetUnitTest { use MailPoet\EmailEditor\Engine\Renderer\Preprocessors\TopLevelPreprocessor;
class TopLevelPreprocessorTest extends \MailPoetUnitTest {
private $paragraphBlock = [ private $paragraphBlock = [
'blockName' => 'core/paragraph', 'blockName' => 'core/paragraph',
@@ -20,12 +22,12 @@ class PreprocessorTest extends \MailPoetUnitTest {
]], ]],
]; ];
/** @var Preprocessor */ /** @var TopLevelPreprocessor */
private $preprocessor; private $preprocessor;
public function _before() { public function _before() {
parent::_before(); parent::_before();
$this->preprocessor = new Preprocessor(); $this->preprocessor = new TopLevelPreprocessor();
} }
public function testItWrapsSingleTopLevelBlockIntoColumns() { public function testItWrapsSingleTopLevelBlockIntoColumns() {