Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
wanze committed Mar 18, 2018
2 parents 1ddd682 + e34147e commit d87dad3
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 153 deletions.
63 changes: 24 additions & 39 deletions TemplateEngine.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

require_once(__DIR__ . '/TemplateEngineInterface.php');

/**
* TemplateEngine
*
Expand All @@ -9,25 +11,25 @@
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
*
*/
abstract class TemplateEngine extends Wire
abstract class TemplateEngine extends Wire implements TemplateEngineInterface
{

/**
* Stores module configuration per implemented TemplateEngine
* Stores module configuration per implemented TemplateEngine.
*
* @var array
*/
protected static $loaded_config = array();
protected static $loadedConfig = array();

/**
* Filename of template file
* Filename of template file.
*
* @var string
*/
protected $filename = '';

/**
* Instance to the TemplateEngineFactory module
* Instance of the TemplateEngineFactory module.
*
* @var TemplateEngineFactory
*/
Expand Down Expand Up @@ -69,12 +71,8 @@ public function init()


/**
* Init engine, derived classes must use this method to setup the engine
*/
abstract public function initEngine();


/**
* Magic setter to pass variables to the template engine.
*
* @param $key
* @param $value
*/
Expand All @@ -84,17 +82,9 @@ public function __set($key, $value)
}


/**
* Set a key/value pair to the template engine
*
* @param $key
* @param $value
*/
abstract public function set($key, $value);


/**
* Alias for setArray
* Alias for setArray.
*
* @param array $data
*/
Expand All @@ -105,7 +95,7 @@ public function setMultiple($data = array())


/**
* Set multiple key/value pairs to the template engine
* Set multiple key/value pairs to the template engine.
*
* @param array $data
*/
Expand All @@ -118,14 +108,9 @@ public function setArray($data = array())


/**
* Render markup from template engine
* Return the default configuration for all template engines. Derived classes should
* overwrite this method and provide their own configuration options.
*
* @return mixed
*/
abstract public function render();


/**
* @return array
*/
public static function getDefaultConfig()
Expand All @@ -139,8 +124,8 @@ public static function getDefaultConfig()


/**
* ProcessWire does call this method and set config values from database
* In our context, the config is loaded and available already in the constructor so just leave empty
* ProcessWire does call this method and set config values from database.
* In our context, the config is loaded and available already in the constructor so just leave empty.
*
* @param array $data
*/
Expand All @@ -150,7 +135,7 @@ public function setConfigData(array $data = array())


/**
* Return config all implemented engines share in common
* Return config all implemented engines share in common.
*
* @param array $data
* @return InputfieldWrapper
Expand Down Expand Up @@ -188,26 +173,26 @@ public static function getModuleConfigInputfields(array $data)


/**
* Get a config value
* Get a config value.
*
* @param $key
* @return string|null
*/
public function getConfig($key)
{
return (isset(self::$loaded_config[$this->className][$key])) ? self::$loaded_config[$this->className][$key] : null;
return (isset(self::$loadedConfig[$this->className][$key])) ? self::$loadedConfig[$this->className][$key] : null;
}


/**
* Set a config value (runtime only and for all instances of the derived TemplateEngine class)
* Set a config value (runtime only and for all instances of the derived TemplateEngine class).
*
* @param $key
* @param $value
*/
public function setConfig($key, $value)
{
self::$loaded_config[$this->className][$key] = $value;
self::$loadedConfig[$this->className][$key] = $value;
}


Expand Down Expand Up @@ -235,7 +220,7 @@ public function setFilename($filename)


/**
* Get the path where templates are stored
* Get the path where templates are stored.
*
* @return string
*/
Expand All @@ -248,14 +233,14 @@ public function getTemplatesPath()


/**
* Load configuration once for all instances of TemplateEngine
* Load configuration once for all instances of TemplateEngine.
*
*/
protected function initConfig()
{
if (!isset(self::$loaded_config[$this->className])) {
if (!isset(self::$loadedConfig[$this->className])) {
$configs = $this->wire('modules')->getModuleConfigData($this);
self::$loaded_config[$this->className] = array_merge($this->getDefaultConfig(), $configs);
self::$loadedConfig[$this->className] = array_merge($this->getDefaultConfig(), $configs);
}
}
}
16 changes: 5 additions & 11 deletions TemplateEngineCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,41 +10,35 @@
* @author Stefan Wanzenried <[email protected]>
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License, version 2
*/

interface TemplateEngineCache {

/**
* Get cached output of template or null if no cache exists
*
* Get cached output of template or null if no cache exists.
* @return string|null
*/
public function getCache();


/**
* Cache output of current template
*
* Cache output of current template.
*/
public function storeCache();


/**
* Clear cache of current template
*
* Clear cache of current template.
*/
public function clearCache();


/**
* Clear cache completely, also cache of all other templates
*
* Clear cache completely, also cache of all other templates.
*/
public function clearAllCache();


/**
* Returns true if a cache exists for the template
*
* Returns true if a cache exists for the template.
* @return bool
*/
public function isCached();
Expand Down
48 changes: 23 additions & 25 deletions TemplateEngineChunk.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,31 @@ class TemplateEngineChunk extends WireData
/**
* @var string
*/
protected $chunk_file;
protected $chunkFile;

/**
* @var
*/
protected $template_file;
protected $templateFile;


/**
* @param string $chunk_file Path to the chunk file to load, relative to site/templates/ without suffix
* @param string $template_file Path to the corresponding template file (view) relative to the path where the engine stores its templates
* @param string $chunkFile Path to the chunk file to load, relative to site/templates/ without suffix
* @param string $templateFile Path to the corresponding template file (view) relative to the path where the engine stores its templates
* @throws WireException
*/
public function __construct($chunk_file, $template_file = '')
public function __construct($chunkFile, $templateFile = '')
{
$this->setChunkFile($chunk_file);
$this->setTemplateFile($template_file);
$this->setChunkFile($chunkFile);
$this->setTemplateFile($templateFile);
}


/**
* Set a value
*
* @param string $key
* @param mixed $value
* @throws WireException
* @return $this
*
*/
public function set($key, $value)
{
Expand Down Expand Up @@ -95,33 +93,33 @@ public function render()


/**
* @param string $chunk_file The chunk file to load, relative to site/templates/ without suffix
* @param string $chunkFile The chunk file to load, relative to site/templates/ without suffix
* @throws WireException
* @return $this
*/
public function setChunkFile($chunk_file)
public function setChunkFile($chunkFile)
{
if (!is_file($this->getChunkPath($chunk_file))) {
throw new WireException("Chunk file does not exist: '{$chunk_file}'");
if (!is_file($this->getChunkPath($chunkFile))) {
throw new WireException("Chunk file does not exist: '{$chunkFile}'");
}
$this->chunk_file = $chunk_file;
$this->chunkFile = $chunkFile;

return $this;
}


/**
* @param string $template_file The template file (view) that should be used to render this chunk
* @param string $templateFile The template file (view) that should be used to render this chunk
* @throws WireException
* @return $this
*/
public function setTemplateFile($template_file)
public function setTemplateFile($templateFile)
{
$template_file = ($template_file) ? $template_file : $this->chunk_file;
$this->template_file = $template_file;
$this->view = $this->wire('factory')->load($template_file);
$templateFile = ($templateFile) ? $templateFile : $this->chunkFile;
$this->templateFile = $templateFile;
$this->view = $this->wire('factory')->load($templateFile);
if ($this->view === null) {
throw new WireException("View for chunk {$this->chunk_file} does not exist");
throw new WireException("View for chunk {$this->chunkFile} does not exist");
}

return $this;
Expand All @@ -135,20 +133,20 @@ public function setTemplateFile($template_file)
*/
protected function ___getChunk()
{
$chunk = new TemplateFile($this->getChunkPath($this->chunk_file));
$chunk = new TemplateFile($this->getChunkPath($this->chunkFile));
$chunk->setArray($this->getArray());

return $chunk;
}


/**
* @param string $chunk_file
* @param string $chunkFile
* @return string
*/
protected function getChunkPath($chunk_file)
protected function getChunkPath($chunkFile)
{
return $this->wire('config')->paths->templates . $chunk_file . '.' . $this->wire('config')->templateExtension;
return $this->wire('config')->paths->templates . $chunkFile . '.' . $this->wire('config')->templateExtension;
}

}
Loading

0 comments on commit d87dad3

Please sign in to comment.