Starting from the end of my earlier post I will go on and try to come up with an implementation of a (somewhat) MVC pattern for themes that will allow me to write templates for WordPress themes using a syntax like
// file index.php
<?php
Theme::show('head'); // calls wp_head too
Theme::show('headerBanner', array('color' => '#FFF', 'bgImage' => 'unicornsAndRainbows')); // because I like white banner background
Theme::show('sidebar', array('left'));
Theme::show('content', array('titleHeaderSize' => 'h3', 'showExcperpts' => true, 'showRelated' => true, ' coloredReadMore' => true));
Theme::show('contactFooter' , array('mail', 'skype', 'twitter'));
Theme::show('footer', array('color' => '#FFF')); //calls wp_footer too
?>
The setup and a first walking skeleton
After creating a Github repository for the project I’ve considered the code above for a while and creted the bare-bones implementation to make it work. Since here my aim is to create a framework, and not a single theme, the first thing that comes to my mind is that the Theme
object called in code is a local-to-the-theme implementation of a more general-purpose class, say Main
(the fantasy flows in me).
The following UML diagram shows my idea of a first implementation way better than words could do
[image of uml diagram] Since the base classes defined in the library are abstract I won’t be testing them directly and will use some other classes extending them (MainChild
, ControllerChild
, ViewChild
) to test them. The added benefit of doing so is that I will have an immediate feedback about how much work I save using the framework seeing how much or how little and specific code I have to implement in the extending classes to make things work. To have a direction to work toward I will generate an integration
test suite in Codeception running, at terminal
codecept generate:suite integration Integration
and will then setup a first integration test file
codecept generate:test integration ShowReturnsSomething
and will go on to write the smallest possible code to fulfill this test
<?php
use Codeception\Util\Stub;
class ShowReturnsSomethingTest extends \Codeception\TestCase\Test
{
/**
* @var \Integration
*/
protected $integration;
protected main, controller, view;
protected function _before()
{
}
protected function _after()
{
}
// tests
public function testThatCallingMainShowProperlyReturnsString()
{
$this->expectOutputString('First');
TestMain::show('first');
}
}