Skip to content
Snippets Groups Projects
Commit f88dfe61 authored by dyszczo's avatar dyszczo
Browse files

view builder

parent 08393dd1
No related branches found
No related tags found
2 merge requests!8feat/woo template,!4view builder
Pipeline #6053 passed with stages
in 1 minute and 28 seconds
## [1.1.0] - 2019-09-23
### Added
- PluginViewBuilder to facilitate building and rendering views for plugins
\ No newline at end of file
<?php
namespace WPDesk\View;
use WPDesk\View\Renderer\SimplePhpRenderer;
use WPDesk\View\Resolver\ChainResolver;
use WPDesk\View\Resolver\DirResolver;
use WPDesk\View\Resolver\WPThemeResolver;
/**
* Facilitates building of the default plugin renderer.
*
* @package WPDesk\View
*/
class PluginViewBuilder {
/** @var string */
private $plugin_dir;
/** @var string[] */
private $template_dirs;
/**
* @param string $plugin_dir Plugin directory path(absolute path)
* @param string|string[] $template_dir Directory or list of directories with templates to render
*/
public function __construct( $plugin_dir, $template_dir = 'templates' ) {
$this->plugin_dir = $plugin_dir;
if ( ! is_array( $template_dir ) ) {
$this->template_dirs = [ $template_dir ];
} else {
$this->template_dirs = $template_dir;
}
}
/**
* Creates simple renderer that search for the templates in plugin dir and in theme/child dir.
*
* For example if your plugin dir is /plugin, template dir is /templates, theme is /theme, and a child theme is /child
* the templates will be loaded from(order is important):
* - /child/plugin/*.php
* - /theme/plugin/*.php
* - /plugin/templates/*.php
*
* @return SimplePhpRenderer
*/
public function createSimpleRenderer() {
$resolver = new ChainResolver();
$resolver->appendResolver( new WPThemeResolver( basename( $this->plugin_dir ) ) );
foreach ( $this->template_dirs as $dir ) {
$dir = trailingslashit( $this->plugin_dir ) . trailingslashit( $dir );
$resolver->appendResolver( new DirResolver( $dir ) );
}
return new SimplePhpRenderer( $resolver );
}
/**
* Load templates using simple renderer.
*
* @param string $name Name of the template
* @param string $path Additional path of the template ie. for path "path" the templates would be loaded from /plugin/templates/path/*.php
* @param array $args Arguments for templates to use
*
* @return string Rendered template.
*/
public function loadTemplate( $name, $path = '.', $args = array() ) {
$renderer = $this->createSimpleRenderer();
return $renderer->render( trailingslashit( $path ) . $name, $args );
}
}
\ No newline at end of file
......@@ -9,6 +9,15 @@ class TestDirResolver extends \PHPUnit\Framework\TestCase
const TEMPLATE_FILE = 'some_template.php';
const TEMPLATE_SUBDIR = 'templates';
public function setUp()
{
\WP_Mock::setUp();
}
public function tearDown()
{
\WP_Mock::tearDown();
}
public function testCanFindInDirPath()
{
......
......@@ -32,7 +32,7 @@ class TestThemeResolver extends \PHPUnit\Framework\TestCase
\WP_Mock::userFunction('trailingslashit', [
'return' => function ($string) {
return untrailingslashit($string) . '/';
return rtrim($string, '/\\') . '/';
}
]);
......
<?php
use WPDesk\View\PluginViewBuilder;
class TestPluginViewBuilder extends \PHPUnit\Framework\TestCase {
public function setUp() {
\WP_Mock::setUp();
\WP_Mock::userFunction('trailingslashit', [
'return' => function ($string) {
return rtrim($string, '/\\') . '/';
}
]);
}
public function tearDown()
{
\WP_Mock::tearDown();
}
public function testCanRenderUsingDir() {
$builder = new PluginViewBuilder( __DIR__ . '/stub', 'template' );
$renderer = $builder->createSimpleRenderer();
$val = 'val to render';
$args = [ 'singleArg' => $val ];
$content = $renderer->render( 'file', $args );
$this->assertRegExp( '/template content/', $content, 'Content from stub/template/file.php should be renderer' );
$this->assertRegExp( "/{$val}/", $content, 'Content from stub/template/file.php should contain $val' );
$contentUsingOtherMethod = $builder->loadTemplate( 'file', '.', $args );
$this->assertEquals( $content, $contentUsingOtherMethod,
'Content should be the same no matter the method of render' );
}
}
\ No newline at end of file
template content <?php echo $singleArg; ?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment