winter/modules/system/traits/ViewMaker.php

237 lines
7.4 KiB
PHP
Raw Normal View History

<?php namespace System\Traits;
2014-05-14 23:24:20 +10:00
use File;
use Lang;
use Block;
2015-01-28 18:03:35 +11:00
use SystemException;
2014-05-14 23:24:20 +10:00
/**
* View Maker Trait
* Adds view based methods to a class
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
trait ViewMaker
{
/**
* @var array A list of variables to pass to the page.
*/
public $vars = [];
/**
* @var string Specifies a path to the views directory.
*/
protected $viewPath;
2014-05-14 23:24:20 +10:00
/**
* @var string Specifies a path to the layout directory.
*/
protected $layoutPath;
2014-05-14 23:24:20 +10:00
/**
* @var string Layout to use for the view.
*/
public $layout;
/**
* @var bool Prevents the use of a layout.
*/
public $suppressLayout = false;
/**
* Render a partial file contents located in the views folder.
* @param string $partial The view to load.
* @param array $params Parameter variables to pass to the view.
* @param bool $throwException Throw an exception if the partial is not found.
* @return mixed Partial contents or false if not throwing an exception.
2014-05-14 23:24:20 +10:00
*/
public function makePartial($partial, $params = [], $throwException = true)
2014-05-14 23:24:20 +10:00
{
2014-10-18 11:58:50 +02:00
if (!File::isPathSymbol($partial) && realpath($partial) === false) {
$folder = strpos($partial, '/') !== false ? dirname($partial) . '/' : '';
$partial = $folder . '_' . strtolower(basename($partial)).'.htm';
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
$partialPath = $this->getViewPath($partial);
if (!File::exists($partialPath)) {
2014-10-18 11:58:50 +02:00
if ($throwException) {
throw new SystemException(Lang::get('backend::lang.partial.not_found_name', ['name' => $partialPath]));
2014-11-01 14:47:46 +11:00
}
else {
return false;
2014-10-18 11:58:50 +02:00
}
}
2014-05-14 23:24:20 +10:00
return $this->makeFileContents($partialPath, $params);
}
/**
* Loads a view with the name specified. Applies layout if its name is provided by the parent object.
* The view file must be situated in the views directory, and has the extension "htm".
* @param string $view Specifies the view name, without extension. Eg: "index".
*/
public function makeView($view)
{
$viewPath = $this->getViewPath(strtolower($view) . '.htm');
$contents = $this->makeFileContents($viewPath);
return $this->makeViewContent($contents);
}
/**
* Renders supplied contents inside a layout.
* @param string $contents The inner contents as a string.
* @param string $name Specifies the layout name.
* If this parameter is omitted, the $layout property will be used.
* @return string The layout contents
*/
public function makeViewContent($contents, $layout = null)
{
2014-10-18 11:58:50 +02:00
if ($this->suppressLayout || $this->layout == '') {
2014-05-14 23:24:20 +10:00
return $contents;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
// Append any undefined block content to the body block
Block::set('undefinedBlock', $contents);
Block::append('body', Block::get('undefinedBlock'));
return $this->makeLayout();
}
/**
* Render a layout.
* @param string $name Specifies the layout name.
* If this parameter is omitted, the $layout property will be used.
* @param array $params Parameter variables to pass to the view.
* @param bool $throwException Throw an exception if the layout is not found
2014-05-14 23:24:20 +10:00
* @return string The layout contents
*/
public function makeLayout($name = null, $params = [], $throwException = true)
2014-05-14 23:24:20 +10:00
{
$layout = ($name === null) ? $this->layout : $name;
2014-10-18 11:58:50 +02:00
if ($layout == '') {
2014-05-14 23:24:20 +10:00
return;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
$layoutPath = $this->getViewPath($layout . '.htm', $this->layoutPath);
if (!File::exists($layoutPath)) {
2014-10-18 11:58:50 +02:00
if ($throwException) {
throw new SystemException(Lang::get('cms::lang.layout.not_found_name', ['name' => $layoutPath]));
2014-11-01 14:47:46 +11:00
}
else {
return false;
2014-10-18 11:58:50 +02:00
}
}
2014-05-14 23:24:20 +10:00
return $this->makeFileContents($layoutPath, $params);
}
/**
* Renders a layout partial
* @param string $partial The view to load.
* @param array $params Parameter variables to pass to the view.
* @return string The layout partial contents
*/
public function makeLayoutPartial($partial, $params = [])
{
2014-10-18 11:58:50 +02:00
if (!File::isLocalPath($partial) && !File::isPathSymbol($partial)) {
$folder = strpos($partial, '/') !== false ? dirname($partial) . '/' : '';
$partial = $folder . '_' . strtolower(basename($partial));
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
return $this->makeLayout($partial, $params);
}
/**
* Locates a file based on it's definition. If the file starts with
* an "at symbol", it will be returned in context of the application base path,
* otherwise it will be returned in context of the view path.
* @param string $fileName File to load.
* @param mixed $viewPath Explicitly define a view path.
* @return string Full path to the view file.
*/
public function getViewPath($fileName, $viewPath = null)
{
2014-10-18 11:58:50 +02:00
if (!isset($this->viewPath)) {
2014-05-14 23:24:20 +10:00
$this->viewPath = $this->guessViewPath();
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-18 11:58:50 +02:00
if (!$viewPath) {
2014-05-14 23:24:20 +10:00
$viewPath = $this->viewPath;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
$fileName = File::symbolizePath($fileName);
2014-05-14 23:24:20 +10:00
2014-10-18 11:58:50 +02:00
if (File::isLocalPath($fileName) || realpath($fileName) !== false) {
2014-05-14 23:24:20 +10:00
return $fileName;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-18 11:58:50 +02:00
if (!is_array($viewPath)) {
2014-05-14 23:24:20 +10:00
$viewPath = [$viewPath];
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
foreach ($viewPath as $path) {
$_fileName = File::symbolizePath($path) . '/' . $fileName;
2014-10-18 11:58:50 +02:00
if (File::isFile($_fileName)) {
2014-05-14 23:24:20 +10:00
break;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
}
return $_fileName;
}
/**
* Includes a file path using output buffering.
* Ensures that vars are available.
* @param string $filePath Absolute path to the view file.
* @param array $extraParams Parameters that should be available to the view.
* @return string
*/
public function makeFileContents($filePath, $extraParams = [])
{
2014-10-18 11:58:50 +02:00
if (!strlen($filePath) || !File::isFile($filePath)) {
2014-05-14 23:24:20 +10:00
return;
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
2014-10-18 11:58:50 +02:00
if (!is_array($extraParams)) {
2014-05-14 23:24:20 +10:00
$extraParams = [];
2014-10-18 11:58:50 +02:00
}
2014-05-14 23:24:20 +10:00
$vars = array_merge($this->vars, $extraParams);
ob_start();
extract($vars);
include $filePath;
return ob_get_clean();
}
/**
* Guess the package path for the called class.
* @param string $suffix An extra path to attach to the end
* @param bool $isPublic Returns public path instead of an absolute one
* @return string
*/
public function guessViewPath($suffix = '', $isPublic = false)
{
$class = get_called_class();
return $this->guessViewPathFrom($class, $suffix, $isPublic);
}
/**
* Guess the package path from a specified class.
* @param string $class Class to guess path from.
* @param string $suffix An extra path to attach to the end
* @param bool $isPublic Returns public path instead of an absolute one
* @return string
*/
public function guessViewPathFrom($class, $suffix = '', $isPublic = false)
{
2014-09-29 13:12:34 +10:00
$classFolder = strtolower(class_basename($class));
2014-05-14 23:24:20 +10:00
$classFile = realpath(dirname(File::fromClass($class)));
$guessedPath = $classFile ? $classFile . '/' . $classFolder . $suffix : null;
return ($isPublic) ? File::localToPublic($guessedPath) : $guessedPath;
}
2014-10-18 11:58:50 +02:00
}