Skip to content

Commit

Permalink
v1.6.1: implements TwigTemplatePluginTrait (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Asisyas committed Jan 24, 2023
1 parent 6f30dcc commit d012e15
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
2 changes: 0 additions & 2 deletions src/Plugin/TwigTemplatePluginInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ interface TwigTemplatePluginInterface
public function getTwigTemplatePaths(): array;

public function getTwigNamespace(): ?string;

public function isTwigTemplatesPrepend(): bool;
}
44 changes: 44 additions & 0 deletions src/Plugin/TwigTemplatePluginTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Micro framework package.
*
* (c) Stanislau Komar <kost@micro-php.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Micro\Plugin\Twig\Plugin;

trait TwigTemplatePluginTrait
{
/**
* @return string[]
*/
public function getTwigTemplatePaths(): array
{
$classCurrent = new \ReflectionObject($this);
$filename = $classCurrent->getFileName();

if (false === $filename) {
throw new \RuntimeException('Unable to determine path from where to load twig templates.');
}

return [
\dirname($filename).\DIRECTORY_SEPARATOR.'templates',
];
}

public function getTwigNamespace(): ?string
{
$classCurrent = new \ReflectionObject($this);
if ($classCurrent->isAnonymous()) {
return null;
}

return $classCurrent->getShortName();
}
}
52 changes: 52 additions & 0 deletions tests/Unit/Plugin/TwigTemplatePluginTraitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Micro framework package.
*
* (c) Stanislau Komar <kost@micro-php.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Micro\Plugin\Twig\Test\Unit\Plugin;

use Micro\Plugin\Twig\Plugin\TwigTemplatePluginInterface;
use Micro\Plugin\Twig\Plugin\TwigTemplatePluginTrait;
use PHPUnit\Framework\TestCase;

class TwigTemplatePluginTraitTest extends TestCase
{
public function testRealClass()
{
$plugin = new ExampleTwigTemplatePluginTraitTest();

$ref = new \ReflectionObject($plugin);

$this->assertEquals('ExampleTwigTemplatePluginTraitTest', $plugin->getTwigNamespace());
$this->assertEquals([
\dirname($ref->getFileName()).'/templates',
], $plugin->getTwigTemplatePaths());
}

public function testAnonClass()
{
$plugin = new class() implements TwigTemplatePluginInterface {
use TwigTemplatePluginTrait;
};
$ref = new \ReflectionObject($plugin);

$this->assertNull($plugin->getTwigNamespace());
$this->assertEquals([
\dirname($ref->getFileName()).'/templates',
],
$plugin->getTwigTemplatePaths());
}
}

class ExampleTwigTemplatePluginTraitTest implements TwigTemplatePluginInterface
{
use TwigTemplatePluginTrait;
}

0 comments on commit d012e15

Please sign in to comment.