1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Views;
13
14 15 16
17 class TemplateResolver extends \ICanBoogie\Object
18 {
19 20 21 22 23
24 protected $id;
25
26 27 28 29 30
31 protected $type;
32
33 34 35 36 37
38 protected $module_id;
39
40 public function __construct($id, $type, $module_id)
41 {
42 $this->id = $id;
43 $this->type = $type;
44 $this->module_id = $module_id;
45 }
46
47 protected function lazy_get_templates()
48 {
49 global $core;
50
51 $id = $this->id;
52 $type = $this->type;
53 $templates = array();
54
55 $templates_base = array();
56
57 $parts = explode('/', $id);
58 $module_id = array_shift($parts);
59 $type = array_pop($parts);
60
61 while (count($parts))
62 {
63 $templates_base[] = implode('--', $parts) . '--' . $type;
64
65 array_pop($parts);
66 }
67
68 $templates_base[] = $type;
69
70 $templates_base = array_unique($templates_base);
71
72 $descriptors = $core->modules->descriptors;
73 $descriptor = $descriptors[$this->module_id];
74
75 while ($descriptor)
76 {
77 foreach ($templates_base as $template)
78 {
79 $pathname = \ICanBoogie\DOCUMENT_ROOT . 'protected/all/templates/views/' . \ICanBoogie\normalize($descriptor[Module::T_ID]) . '--' . $template;
80 $templates[] = $pathname;
81
82 $pathname = $descriptor[Module::T_PATH] . 'views/' . $template;
83 $templates[] = $pathname;
84 }
85
86 $descriptor = $descriptor[Module::T_EXTENDS] ? $descriptors[$descriptor[Module::T_EXTENDS]] : null;
87 }
88
89 foreach ($templates_base as $template)
90 {
91 $pathname = \ICanBoogie\DOCUMENT_ROOT . 'protected/all/templates/views/' . $template;
92 $templates[] = $pathname;
93 }
94
95 return $templates;
96 }
97
98 public function __invoke()
99 {
100 $templates = $this->templates;
101
102 $handled = array('php', 'html');
103
104 foreach ($templates as $template)
105 {
106 foreach ($handled as $extension)
107 {
108 $pathname = $template . '.' . $extension;
109
110
111
112 if (file_exists($pathname))
113 {
114 return $pathname;
115 }
116 }
117 }
118 }
119 }