Last active
December 27, 2015 17:49
-
-
Save duskohu/7364973 to your computer and use it in GitHub Desktop.
TemplateFilesFormatter for https://github.com/NasExt/Templating
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
<?php | |
namespace Nas\Templating; | |
use NasExt\Templating\FilesList; | |
use NasExt\Templating\IFilesFormatterLogger; | |
use NasExt\Templating\ITemplateFilesFormatter; | |
use Nette\Object; | |
/** | |
* TemplateFilesFormatter | |
* | |
* @author Dusan Hudak | |
*/ | |
class TemplateFilesFormatter extends Object implements ITemplateFilesFormatter | |
{ | |
const MODULE_SUFFIX = 'Module'; | |
const ADMIN_MODULE = 'Admin'; | |
const FRONT_MODULE = 'Front'; | |
/** @var bool */ | |
public $useModuleSuffix = TRUE; | |
/** @var \SplPriorityQueue */ | |
private $dirs; | |
/** @var IFilesFormatterLogger|NULL */ | |
private $logger; | |
public function __construct() | |
{ | |
$this->dirs = new \SplPriorityQueue; | |
$this->logger = NULL; | |
} | |
/** | |
* @param string $dir | |
* @param int $priority | |
* @return TemplateFilesFormatter | |
*/ | |
public function addDir($dir, $priority = 5) | |
{ | |
$this->dirs->insert($dir, $priority); | |
return $this; | |
} | |
/** | |
* @param IFilesFormatterLogger $logger | |
* @return TemplateFilesFormatter | |
*/ | |
public function setLogger(IFilesFormatterLogger $logger) | |
{ | |
$this->logger = $logger; | |
return $this; | |
} | |
/** | |
* Formats layout template file names | |
* @param string $presenterName | |
* @param string $layout | |
* @return FilesList | |
*/ | |
public function formatLayoutTemplateFiles($presenterName, $layout = 'layout') | |
{ | |
$path = str_replace(':', '/', substr($presenterName, 0, strrpos($presenterName, ':'))); | |
$subPath = substr($presenterName, strrpos($presenterName, ':') !== FALSE ? strrpos($presenterName, ':') + 1 : 0); | |
if ($this->useModuleSuffix && $path) { | |
$path .= '/'; | |
$path = str_replace('/', self::MODULE_SUFFIX . '/', $path); | |
$path = rtrim($path, '/'); | |
} | |
$generator = function ($dir) use ($path, $subPath, $layout) { | |
$files = array(); | |
$files[] = "$dir/$path/$subPath/@$layout.latte"; | |
$modulePath = $path; | |
do { | |
$files[] = "$dir/$modulePath/@$layout.latte"; | |
} while ($modulePath = substr($modulePath, 0, strrpos($modulePath, '/'))); | |
if (strrpos($path, TemplateFilesFormatter::ADMIN_MODULE) !== FALSE) { | |
$files[] = $dir . '/' . substr($path, strrpos($path, TemplateFilesFormatter::ADMIN_MODULE)) . "/@$layout.latte"; | |
} | |
if (strrpos($path, TemplateFilesFormatter::FRONT_MODULE) !== FALSE) { | |
$files[] = $dir . '/' . substr($path, strrpos($path, TemplateFilesFormatter::FRONT_MODULE)) . "/@$layout.latte"; | |
} | |
$file = $dir . "/@$layout.latte"; | |
if (!in_array($file, $files)) { | |
$files[] = $file; | |
} | |
return $files; | |
}; | |
$files = array(); | |
$dirs = clone $this->dirs; | |
foreach ($dirs as $dir) { | |
$files = array_merge($files, $generator($dir)); | |
} | |
return new FilesList("$presenterName:$layout", $files, $this->logger); | |
} | |
/** | |
* Formats view template file names | |
* @param string $presenterName | |
* @param string $presenterView | |
* @return FilesList | |
*/ | |
public function formatTemplateFiles($presenterName, $presenterView) | |
{ | |
$path = str_replace(':', '/', substr($presenterName, 0, strrpos($presenterName, ':'))); | |
$subPath = substr($presenterName, strrpos($presenterName, ':') !== FALSE ? strrpos($presenterName, ':') + 1 : 0); | |
if ($path) { | |
$path .= '/'; | |
} | |
if ($this->useModuleSuffix && $path) { | |
$path = str_replace('/', self::MODULE_SUFFIX . '/', $path); | |
} | |
$generator = function ($dir) use ($presenterName, $path, $subPath, $presenterView) { | |
$files = array(); | |
if (strpos($presenterName, ':') !== FALSE) { | |
$files[] = $dir . '/' . $path . "$subPath/$presenterView.latte"; | |
$files[] = $dir . '/' . $path . "$subPath.$presenterView.latte"; | |
} | |
return $files; | |
}; | |
$files = array(); | |
$dirs = clone $this->dirs; | |
foreach ($dirs as $dir) { | |
$files = array_merge($files, $generator($dir)); | |
} | |
return new FilesList("$presenterName:$presenterView", $files, $this->logger); | |
} | |
/** | |
* Formats component template file names | |
* @param string $presenterName | |
* @param string $presenterView | |
* @param string $controlClass | |
* @return FilesList | |
*/ | |
public function formatComponentTemplateFiles($presenterName, $presenterView, $controlClass) | |
{ | |
$path = str_replace(':', '/', substr($presenterName, 0, strrpos($presenterName, ':'))); | |
$subPath = substr($presenterName, strrpos($presenterName, ':') !== FALSE ? strrpos($presenterName, ':') + 1 : 0); | |
if ($this->useModuleSuffix && $path) { | |
$path .= '/'; | |
$path = str_replace('/', self::MODULE_SUFFIX . '/', $path); | |
$path = rtrim($path, '/'); | |
} | |
$generator = function ($dir) use ($controlClass, $path, $subPath, $presenterView) { | |
$files = array(); | |
$files[] = $dir . '/' . $path . "/$subPath/controls/$presenterView.$controlClass.latte"; | |
$files[] = $dir . '/' . $path . "/$subPath/controls/$controlClass.latte"; | |
$modulePath = $path; | |
do { | |
$files[] = "$dir/$modulePath/controls/$controlClass.latte"; | |
} while ($modulePath = substr($modulePath, 0, strrpos($modulePath, '/'))); | |
if (strrpos($path, TemplateFilesFormatter::ADMIN_MODULE) !== FALSE) { | |
$files[] = $dir . '/' . substr($path, strrpos($path, TemplateFilesFormatter::ADMIN_MODULE)) . "/controls/$controlClass.latte"; | |
} | |
if (strrpos($path, TemplateFilesFormatter::FRONT_MODULE) !== FALSE) { | |
$files[] = $dir . '/' . substr($path, strrpos($path, TemplateFilesFormatter::FRONT_MODULE)) . "/controls/$controlClass.latte"; | |
} | |
$file = $dir . "/controls/$controlClass.latte"; | |
if (!in_array($file, $files)) { | |
$files[] = $file; | |
} | |
return $files; | |
}; | |
$files = array(); | |
$dirs = clone $this->dirs; | |
foreach ($dirs as $dir) { | |
$files = array_merge($files, $generator($dir)); | |
} | |
return new FilesList("$presenterName:$presenterView:$controlClass", $files, $this->logger); | |
} | |
/** | |
* Format FileTemplate Files | |
* @param string $template | |
* @return FilesList | |
*/ | |
public function formatFileTemplateFiles($template) | |
{ | |
$files = array(); | |
$dirs = clone $this->dirs; | |
foreach ($dirs as $dir) { | |
$files[] = $dir . "/$template"; | |
} | |
return new FilesList(substr(strrchr($template, '/'), 1), $files, $this->logger); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment