Skip to content

Commit

Permalink
Merge pull request #772 from LuckyCyborg/master
Browse files Browse the repository at this point in the history
Introducing Core\Template, a View specialized to handle Template files
  • Loading branch information
daveismynamecom committed Apr 23, 2016
2 parents e45f50e + 69729c9 commit f9e6f73
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 75 deletions.
7 changes: 4 additions & 3 deletions system/Core/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

use Core\Language;
use Core\View;
use Core\Template;
use Helpers\Hooks;

/**
Expand Down Expand Up @@ -126,9 +127,9 @@ protected function after($data)
//
// Execute the default Template-based rendering of the given View instance.

if (! $data->isTemplate() && ($this->layout !== false)) {
// The View instance is NOT a Template, but have a Layout is specified.
View::makeTemplate($this->layout, array(), $this->template)
if ((! $data instanceof Template) && ($this->layout !== false)) {
// The View instance is NOT a Template, but we have a Layout specified.
Template::make($this->layout, array(), $this->template)
->withContent($data)
->display();
} else {
Expand Down
67 changes: 67 additions & 0 deletions system/Core/Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Template - a View specialized for handling the Template files.
*
* @author Virgil-Adrian Teaca - [email protected]
* @version 3.0
*/

namespace Core;

use Core\View;


class Template extends View
{
/**
* Constructor
* @param mixed $path
* @param array $data
*
* @throws \UnexpectedValueException
*/
public function __construct($path, array $data = array())
{
parent::__construct($path, $data);
}

/**
* Create a Template instance
*
* @param string $path
* @param array $data
* @param string $custom
* @return Template
*/
public static function make($path, array $data = array(), $custom = TEMPLATE)
{
// Prepare the file path.
$filePath = str_replace('/', DS, "Templates/$custom/$path.php");

return new Template(APPDIR .$filePath, $data);
}

/**
* Magic Method for handling dynamic functions.
*
* This method handles calls to dynamic with helpers.
*/
public static function __callStatic($method, $params)
{
// No Compatibility Layer exists there; nothing to do.
}

/**
* Compat Layer - Render a Module View file.
*
* @param string $path path to file from Modules folder
* @param array $data array of data
* @param array $error array of errors
*
* @throws \Exception
*/
public static function renderModule($path, $data = false, $error = false)
{
throw new \Exception('renderModule is not available on ' .static::class);
}
}
111 changes: 39 additions & 72 deletions system/Core/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Helpers\Inflector;
use Helpers\Response;
use Core\Template;

use ArrayAccess;

Expand All @@ -23,22 +24,17 @@ class View implements ArrayAccess
/**
* @var array Array of shared data
*/
private static $shared = array();
protected static $shared = array();

/**
* @var string The path to the View file on disk.
*/
private $path = null;
protected $path = null;

/**
* @var array Array of local data.
*/
private $data = array();

/**
* @var bool Flag for the View instances built as Template.
*/
private $template = false;
protected $data = array();

/**
* Constructor
Expand All @@ -58,7 +54,7 @@ public function __construct($path, array $data = array())
}

/**
* Make a Standard View instance
* Create a View instance
*
* @param string $path
* @param array $data
Expand All @@ -77,37 +73,6 @@ public static function make($path, array $data = array(), $module = null)
return new View(APPDIR .$filePath, $data);
}

/**
* Make a Template View instance
*
* @param string $path
* @param array $data
* @param string $custom
* @return View
*/
public static function makeTemplate($path, array $data = array(), $custom = TEMPLATE)
{
// Prepare the file path.
$filePath = str_replace('/', DS, "Templates/$custom/$path.php");

// Get a View instance first, to set it as Template.
$object = new View(APPDIR .$filePath, $data);

$object->template = true;

return $object;
}

/**
* Return true if this View instance is built as Template.
*
* @return bool
*/
public function isTemplate()
{
return $this->template;
}

/**
* Render the View and return the result.
*
Expand Down Expand Up @@ -358,7 +323,7 @@ public function __call($method, $params)
*/
public static function __callStatic($method, $params)
{
// Process the compat Methods associatated to Headers management.
// Process the compat Methods associated to Headers management.
switch ($method) {
case 'addHeader':
case 'addHeaders':
Expand All @@ -372,55 +337,57 @@ public static function __callStatic($method, $params)
// Flag for sending, or not, the Headers, default being true.
$withHeaders = true;

// The called Method into View instance, default being 'make'.
$classMethod = 'make';
// The called Class name for getting an instance.
$className = static::class;

// Prepare the required information.
if ($method == 'render') {
if (count($params) == 4) {
// There is a withHeaders parameter.
$withHeaders = array_pop($params);
}
} else if ($method == 'renderModule') {
// Get the path from parameters, defaulting to a fake one.
$path = ! empty($params) ? array_shift($params) : 'undefined';

// Extract the path and module parameters from the composite path.
if (preg_match('#^(.+)/Views/(.*)$#i', $path, $matches)) {
$origPath = $matches[0];
$module = $matches[1];
$path = $matches[2];
} else {
$origPath = $path;
$module = 'undefined';
}

// Display the update suggestion and go out?
//echo "Please replace <b>View::renderModule('$origPath', \$data)</b> with <b>View::render('$path', \$data, '$module')</b>";

// Get the data from parameters, if exits.
$data = ! empty($params) ? array_shift($params) : array();

// Rebuild $params from the collected information.
$params = array($path, $data, $module);
} else if ($method == 'renderTemplate') {
$classMethod = 'makeTemplate';
$className = Template::class;
} else if ($method != 'fetch') {
return null;
}

// Create a View instance, using the given classMethod and parameters.
$view = call_user_func_array(array(static::class, $classMethod), $params);
$object = call_user_func_array(array($className, 'make'), $params);

if ($method == 'fetch') {
return $view->fetch();
// Render the object and return the captured output.
return $object->fetch();
} else if ($withHeaders) {
// Render the object with sending the Headers first.
return $object->display();
}

if ($withHeaders) {
Response::sendHeaders();
// Render the View object.
return $object->render();
}

/**
* Compat Layer - Render a Module View file.
*
* @param string $path path to file from Modules folder
* @param array $data array of data
* @param array $error array of errors
*/
public static function renderModule($path, $data = false, $error = false)
{
echo '<p>Please use <b>View::render()</b> instead of <b>View::renderModule()</b></p>';

if (($error !== false) && ! isset($data['error'])) {
// Adjust the $error parameter handling, injecting it into $data.
$data['error'] = $error;
}

// Finally, render the View.
return $view->render();
if (preg_match('#^(.+)/Views/(.*)$#i', $path, $matches)) {
// Render the Module's View using the standard way.
$object = call_user_func(array(static::class, 'make'), $matches[2], $data, $matches[1]);

$object->display();
}
}
}

0 comments on commit f9e6f73

Please sign in to comment.