-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v1.6.1: implements TwigTemplatePluginTrait (#5)
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |