1
0
Fork 0
mirror of synced 2024-06-28 19:20:25 +12:00
appwrite/src/Appwrite/Template/Template.php

182 lines
4.4 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
namespace Appwrite\Template;
2019-05-09 18:54:39 +12:00
use Exception;
use Utopia\View;
2019-05-09 18:54:39 +12:00
class Template extends View
{
2021-07-24 02:35:21 +12:00
2021-07-26 19:05:08 +12:00
/**
* @var string
*/
protected string $content = '';
/**
* fromFile
*
* Creates a new Template() from the file at $path
*
* @param string $path
*
* @return self
*
*/
public static function fromFile(string $path): self
{
if (!\is_readable($path)) {
throw new Exception("$path view template is not readable.");
}
$template = new Template();
return $template->setPath($path);
}
2021-07-24 02:35:21 +12:00
/**
2021-07-26 19:05:08 +12:00
* fromString
2021-07-24 02:35:21 +12:00
*
2021-07-26 19:05:08 +12:00
* Creates a new Template() using a raw string
2021-07-24 02:35:21 +12:00
*
2021-07-26 19:05:08 +12:00
* @param string $content
2021-07-24 02:35:21 +12:00
*
* @return self
*
*/
2021-07-26 19:05:08 +12:00
public static function fromString(string $content): self
2021-07-24 02:35:21 +12:00
{
2021-07-26 19:05:08 +12:00
if (empty($content)) {
throw new Exception('Empty string');
2021-07-24 02:35:21 +12:00
}
2021-07-24 02:35:21 +12:00
$template = new Template();
2021-07-26 19:05:08 +12:00
$template->content = $content;
return $template;
2021-07-24 02:35:21 +12:00
}
2019-05-09 18:54:39 +12:00
/**
* Render.
2019-05-09 18:54:39 +12:00
*
* Render view .phtml template file if template has not been set as rendered yet using $this->setRendered(true).
* In case path is not readable throws Exception.
*
* @return string
*
2019-05-09 18:54:39 +12:00
* @throws Exception
*/
2020-02-16 05:54:45 +13:00
public function render($minify = true)
2019-05-09 18:54:39 +12:00
{
if ($this->rendered) { // Don't render any template
return '';
}
if (\is_readable($this->path)) {
$template = \file_get_contents($this->path); // Include template file
2021-07-26 19:05:08 +12:00
} else if (!empty($this->content)) {
$template = $this->print($this->content, self::FILTER_NL2P);
} else {
throw new Exception('"'.$this->path.'" template is not readable or not found');
2019-05-09 18:54:39 +12:00
}
// First replace the variables inside the params. Then replace the variables in the template
$this->params = array_merge($this->params, \str_replace(\array_keys($this->params), \array_values($this->params), $this->params));
$template = \str_replace(\array_keys($this->params), \array_values($this->params), $template);
2019-05-09 18:54:39 +12:00
return $template;
}
/**
* Parse URL.
2019-05-09 18:54:39 +12:00
*
* Parse URL string to array
*
* @param $url
*
2019-05-09 18:54:39 +12:00
* @return mixed On seriously malformed URLs, parse_url() may return FALSE.
*/
public static function parseURL($url)
2019-05-09 18:54:39 +12:00
{
return \parse_url($url);
2019-05-09 18:54:39 +12:00
}
/**
* Un-Parse URL.
2019-05-09 18:54:39 +12:00
*
* Convert PHP array to query string
*
* @param $url
*
2019-05-09 18:54:39 +12:00
* @return string
*/
public static function unParseURL(array $url)
2019-05-09 18:54:39 +12:00
{
$scheme = isset($url['scheme']) ? $url['scheme'].'://' : '';
$host = isset($url['host']) ? $url['host'] : '';
$port = isset($url['port']) ? ':'.$url['port'] : '';
2019-05-09 18:54:39 +12:00
$user = isset($url['user']) ? $url['user'] : '';
$pass = isset($url['pass']) ? ':'.$url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
2019-05-09 18:54:39 +12:00
$path = isset($url['path']) ? $url['path'] : '';
$query = isset($url['query']) && !empty($url['query']) ? '?'.$url['query'] : '';
2019-05-09 18:54:39 +12:00
$fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
2019-05-09 18:54:39 +12:00
return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
2019-05-09 18:54:39 +12:00
}
/**
* Merge Query.
2019-05-09 18:54:39 +12:00
*
* Merge array of params to query string
*
* @param $query1
* @param array $query2
*
2019-05-09 18:54:39 +12:00
* @return string
*/
public static function mergeQuery($query1, array $query2)
2019-05-09 18:54:39 +12:00
{
$parsed = [];
\parse_str($query1, $parsed);
2019-05-09 18:54:39 +12:00
$parsed = \array_merge($parsed, $query2);
2019-05-09 18:54:39 +12:00
return \http_build_query($parsed);
2019-05-09 18:54:39 +12:00
}
2020-07-11 01:29:15 +12:00
/**
* From Camel Case
*
* @var string $input
*
* @return string
*/
public static function fromCamelCaseToSnake($input): string
{
\preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
$ret = $matches[0];
foreach ($ret as &$match) {
$match = $match == \strtoupper($match) ? \strtolower($match) : \lcfirst($match);
}
return \implode('_', $ret);
}
/**
* From Camel Case to Dash Case
*
* @var string $input
*
* @return string
*/
public static function fromCamelCaseToDash($input): string
{
return \str_replace([' ', '_'], '-', \strtolower(\preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $input)));
}
2021-07-24 02:35:21 +12:00
}