| 1: | <?php
|
| 2: | namespace Opencart\System\Library\Template;
|
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: |
|
| 8: | class Twig {
|
| 9: | |
| 10: | |
| 11: |
|
| 12: | protected string $root;
|
| 13: | |
| 14: | |
| 15: |
|
| 16: | protected \Twig\Loader\FilesystemLoader $loader;
|
| 17: | |
| 18: | |
| 19: |
|
| 20: | protected string $directory;
|
| 21: | |
| 22: | |
| 23: |
|
| 24: | protected array $path = [];
|
| 25: |
|
| 26: | |
| 27: | |
| 28: |
|
| 29: | public function __construct() {
|
| 30: |
|
| 31: | $this->root = substr(DIR_OPENCART, 0, -1);
|
| 32: |
|
| 33: |
|
| 34: |
|
| 35: |
|
| 36: | $this->loader = new \Twig\Loader\FilesystemLoader('./', $this->root);
|
| 37: | }
|
| 38: |
|
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: |
|
| 47: | public function addPath(string $namespace, string $directory = ''): void {
|
| 48: | if (!$directory) {
|
| 49: | $this->directory = $namespace;
|
| 50: | } else {
|
| 51: | $this->path[$namespace] = $directory;
|
| 52: | }
|
| 53: | }
|
| 54: |
|
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: |
|
| 64: | public function render(string $filename, array $data = [], string $code = ''): string {
|
| 65: | $file = $this->directory . $filename . '.twig';
|
| 66: |
|
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: |
|
| 76: |
|
| 77: | $namespace = '';
|
| 78: |
|
| 79: | $parts = explode('/', $filename);
|
| 80: |
|
| 81: | foreach ($parts as $part) {
|
| 82: | if (!$namespace) {
|
| 83: | $namespace .= $part;
|
| 84: | } else {
|
| 85: | $namespace .= '/' . $part;
|
| 86: | }
|
| 87: |
|
| 88: | if (isset($this->path[$namespace])) {
|
| 89: | $file = $this->path[$namespace] . substr($filename, strlen($namespace) + 1) . '.twig';
|
| 90: | }
|
| 91: | }
|
| 92: |
|
| 93: |
|
| 94: | $file = substr($file, strlen($this->root) + 1);
|
| 95: |
|
| 96: | if ($code) {
|
| 97: |
|
| 98: | $loader = new \Twig\Loader\ArrayLoader([$file => $code]);
|
| 99: | } else {
|
| 100: | $loader = $this->loader;
|
| 101: | }
|
| 102: |
|
| 103: | try {
|
| 104: |
|
| 105: | $config = [
|
| 106: | 'charset' => 'utf-8',
|
| 107: | 'autoescape' => false,
|
| 108: | 'debug' => true,
|
| 109: | 'auto_reload' => true,
|
| 110: | 'cache' => DIR_CACHE . 'template/'
|
| 111: | ];
|
| 112: |
|
| 113: | $twig = new \Twig\Environment($loader, $config);
|
| 114: |
|
| 115: | if ($config['debug']) {
|
| 116: | $twig->addExtension(new \Twig\Extension\DebugExtension());
|
| 117: | }
|
| 118: |
|
| 119: | return $twig->render($file, $data);
|
| 120: | } catch (\Twig\Error\SyntaxError $e) {
|
| 121: | throw new \Exception('Error: Could not load template ' . $filename . '!');
|
| 122: | }
|
| 123: | }
|
| 124: | }
|
| 125: | |