1: <?php
2:
3: /**
4: * This file is part of the Minion package.
5: * For the full copyright and license information, please view the LICENSE
6: * file that was distributed with this source code.
7: *
8: * @license MIT License
9: */
10:
11: namespace Minion;
12:
13: /**
14: * {@internal Tool used onl by Minion. May be but it is NOT RECOMMENDED to be used outside package }}
15: *
16: * Class Utils.
17: *
18: * @package Minion
19: * @author Damian SzczerbiĆski <dszczer@gmail.com>
20: */
21: abstract class Utils
22: {
23: /**
24: * @internal
25: *
26: * Check if template path is valid (template exists).
27: *
28: * @param \Twig_Environment $env Twig environment
29: * @param string $template Template path
30: *
31: * @return bool
32: */
33: public static function templateExists(\Twig_Environment $env, $template) {
34: try {
35: $env->loadTemplate($template);
36: } catch(\Twig_Error_Loader $err) {
37: return false;
38: } catch(\Twig_Error_Syntax $err) {
39: return true;
40: }
41:
42: return true;
43: }
44:
45: /**
46: * @internal
47: *
48: * Fix path to match runtime system requirements, for e.g. directory separator.
49: *
50: * @param string $path Path to fix
51: *
52: * @return string Fixed path
53: */
54: public static function fixPath($path) {
55: // fix directory separators
56: $fixed = \mb_ereg_replace('[\\\\\\/]+', DIRECTORY_SEPARATOR, $path);
57:
58: return \file_exists($fixed) ? \realpath($fixed) : $fixed;
59: }
60:
61: /**
62: * @internal
63: *
64: * @param string $path Absolute or relative path to template file
65: * @param array $data Array with data visible inside file scope
66: *
67: * @return string Compiled template content
68: *
69: * @throws \InvalidArgumentException Path is not valid or does not exist
70: */
71: public static function renderPhpTemplate($path, array $data = []) {
72: \ob_start();
73: \extract($data);
74: $real = \realpath($path);
75:
76: if($real === false)
77: throw new \InvalidArgumentException("Path '$path' is not valid or does not exist");
78:
79: include $real;
80:
81: return \ob_get_clean();
82: }
83: }