Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Добавлена поддержка пользовательских расширений для twig-шаблон… #12

Merged
merged 2 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"version":1,"defects":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":4,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testSections":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTemplate":4,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRenderXml":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testPatchXml":4},"times":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":0.057,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testXmlToString":0.029,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetDocx":0.02,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetXml":0.025,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testPatchXml":0.021,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRenderXml":0.083,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRender":0.065,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testLineBreak":0.056,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testCyrillic":0.074,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testForLoop":0.057,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":0.124,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testSections":0.09,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTemplate":0.111}}
{"version":1,"defects":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":4,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testSections":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTemplate":4,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRenderXml":3,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testPatchXml":4,"Tests\\PhpDocxTemplateTest::testRenderXml":3},"times":{"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTable":0.057,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testXmlToString":0.029,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetDocx":0.02,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testGetXml":0.025,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testPatchXml":0.021,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRenderXml":0.083,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testRender":0.065,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testLineBreak":0.056,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testCyrillic":0.074,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testForLoop":0.057,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testImages":0.124,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testSections":0.09,"Doctrine\\Tests\\DBAL\\Query\\PhpDocxTemplateTest::testTemplate":0.111,"Tests\\PhpDocxTemplateTest::testRenderXml":0.045}}
19 changes: 19 additions & 0 deletions src/PhpDocxTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpDocxTemplate;

use DOMDocument;
use Twig\Extension\ExtensionInterface;
use Twig\Loader\ArrayLoader;
use Twig\Environment;
use PhpDocxTemplate\Twig\Impl\{
Expand Down Expand Up @@ -33,6 +34,8 @@ class PhpDocxTemplate
private $picToReplace;
private $picMap;

private $twigExtensions = [];

/**
* Construct an instance of PhpDocxTemplate
*
Expand Down Expand Up @@ -426,6 +429,16 @@ private function cleanTags(array $matches): string
);
}

/**
* Добавить пользовательское расширение
*
* @param ExtensionInterface $ext
*/
public function addTwigExtensions(ExtensionInterface $ext): void
{
$this->twigExtensions[] = $ext;
}

/**
* Render xml
*
Expand All @@ -442,6 +455,12 @@ private function renderXml(string $srcXml, array $context): string
'index' => $srcXml,
]));

/**
* Пользовательские расширения
*/
foreach ($this->twigExtensions as $ext) {
$template->addExtension($ext);
}

$ext = new ImageExtension();
$ext->setRenderer(
Expand Down
7 changes: 3 additions & 4 deletions tests/PhpDocxTemplateTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php

namespace Doctrine\Tests\DBAL\Query;
namespace Tests;

use PHPUnit\Framework\TestCase;
use DOMDocument;
use PhpDocxTemplate\PhpDocxTemplate;
use PhpDocxTemplate\DocxDocument;
use ZipArchive;
use Twig\Loader\ArrayLoader;
use Twig\Environment;

class PhpDocxTemplateTest extends TestCase
{
Expand Down Expand Up @@ -165,6 +163,7 @@ public function testPatchXml(): void
public function testRenderXml(): void
{
$reporter = new PhpDocxTemplate(self::TEMPLATE1);

$this->assertEquals(
$reporter->buildXml(["object" => "world"]),
"<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" .
Expand Down Expand Up @@ -205,7 +204,7 @@ public function testRenderXml(): void
"<w:sectPr w:rsidR=\"0090657C\" w:rsidRPr=\"00FA3F61\"><w:pgSz w:w=\"11906\" w:h=\"16838\"/>" .
"<w:pgMar w:top=\"1134\" w:right=\"850\" w:bottom=\"1134\" w:left=\"1701\" w:header=\"708\" " .
"w:footer=\"708\" w:gutter=\"0\"/><w:cols w:space=\"708\"/><w:docGrid w:linePitch=\"360\"/>" .
"</w:sectPr></w:body></w:document>\n"
"</w:sectPr></w:body></w:document>"
);
$reporter->close();
}
Expand Down