2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
namespace Appwrite\Template;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Utopia\View;
|
|
|
|
|
|
|
|
class Template extends View
|
|
|
|
{
|
|
|
|
/**
|
2019-09-07 05:04:26 +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-09-07 05:04:26 +12:00
|
|
|
*
|
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 '';
|
|
|
|
}
|
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
if (\is_readable($this->path)) {
|
|
|
|
$template = \file_get_contents($this->path); // Include template file
|
2019-09-07 05:04:26 +12:00
|
|
|
} else {
|
|
|
|
throw new Exception('"'.$this->path.'" template is not readable or not found');
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
$template = \str_replace(\array_keys($this->params), \array_values($this->params), $template);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
return $template;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Parse URL.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Parse URL string to array
|
|
|
|
*
|
|
|
|
* @param $url
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return mixed On seriously malformed URLs, parse_url() may return FALSE.
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function parseURL($url)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
return \parse_url($url);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +12:00
|
|
|
* Un-Parse URL.
|
2019-05-09 18:54:39 +12:00
|
|
|
*
|
|
|
|
* Convert PHP array to query string
|
|
|
|
*
|
|
|
|
* @param $url
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function unParseURL(array $url)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2019-09-07 05:04:26 +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
|
|
|
|
2019-09-07 05:04:26 +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
|
|
|
|
2019-09-07 05:04:26 +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
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
$fragment = isset($url['fragment']) ? '#'.$url['fragment'] : '';
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2019-09-07 05:04:26 +12:00
|
|
|
return $scheme.$user.$pass.$host.$port.$path.$query.$fragment;
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-09-07 05:04:26 +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-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function mergeQuery($query1, array $query2)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
|
|
|
$parsed = [];
|
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
\parse_str($query1, $parsed);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:05:43 +12:00
|
|
|
$parsed = \array_merge($parsed, $query2);
|
2019-05-09 18:54:39 +12:00
|
|
|
|
2020-06-20 23:05:43 +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)));
|
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|