PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/MiniMVC/MiniMVC/View.php

https://github.com/tquensen/MiniMVC
PHP | 336 lines | 266 code | 39 blank | 31 comment | 49 complexity | 8ad425c4c3e46130f1fd99589c48c0cb MD5 | raw file
  1. <?php
  2. /**
  3. * MiniMVC_View is the default view class
  4. *
  5. * @property MiniMVC_Helpers $helper
  6. * @property MiniMVC_Translation $t
  7. */
  8. class MiniMVC_View
  9. {
  10. protected $vars = array();
  11. protected $registry = null;
  12. protected $module = null;
  13. protected $file = null;
  14. protected $app = null;
  15. protected $content = null;
  16. protected $helper = null;
  17. protected $t = null;
  18. protected $cache = null;
  19. protected $cacheContent = null;
  20. /**
  21. *
  22. * @param mixed $module the name of the associated module or null
  23. * @param mixed $defaultFile the default file to use
  24. */
  25. public function __construct($module = null, $defaultFile = null, $app = null)
  26. {
  27. $this->registry = MiniMVC_Registry::getInstance();
  28. $this->module = $module;
  29. $this->file = $defaultFile;
  30. $this->app = $app ? $app : $this->registry->settings->get('currentApp');
  31. $this->helper = $this->registry->helper;
  32. if ($module) {
  33. $this->t = $this->helper->i18n->get($this->module);
  34. }
  35. }
  36. /**
  37. *
  38. * @return mixed returns the name of the associated module or null
  39. */
  40. public function getModule()
  41. {
  42. return $this->module;
  43. }
  44. /**
  45. *
  46. * @param string $var the name of the var to set
  47. * @param mixed $value the value to store
  48. */
  49. public function __set($var, $value)
  50. {
  51. $this->vars[$var] = $value;
  52. }
  53. /**
  54. *
  55. * @param string $var the name of the var
  56. * @return mixed the stored value
  57. */
  58. public function __get($var)
  59. {
  60. if ($var == 'helper' || $var == 'h')
  61. {
  62. return $this->helper;
  63. }
  64. elseif($var == 't')
  65. {
  66. return $this->t;
  67. }
  68. elseif($var == 'o')
  69. {
  70. return $this->helper->text;
  71. }
  72. return (isset($this->vars[$var])) ? $this->vars[$var] : '';
  73. }
  74. public function parse()
  75. {
  76. if ($this->cacheContent) {
  77. return $this->cacheContent;
  78. }
  79. if ($this->file === null) {
  80. $return = (string) $this->content;
  81. } else {
  82. $_file = $this->file;
  83. $_app = $this->app;
  84. $_theme = $this->registry->layout->getTheme();
  85. $_format = $this->registry->layout->getFormat();
  86. $_formatString = ($_format) ? '.'.$_format : '';
  87. $_path = null;
  88. if ($_cache = $this->registry->cache->get('viewCached/'.$_app.'_'.$_theme.'_'.$this->module.'_'.str_replace('/', '__', $_file.$_formatString))) {
  89. $_path = $_cache;
  90. } else {
  91. if ($this->module != '_default')
  92. {
  93. if ($_theme && is_file(APPPATH.$_app.'/view/'.$_theme.'/'.$this->module.'/'.$_file.$_formatString.'.php')) {
  94. $_path = APPPATH.$_app.'/view/'.$_theme.'/'.$this->module.'/'.$_file.$_formatString.'.php';
  95. } elseif ($_theme && is_file(VIEWPATH.$_theme.'/'.$this->module.'/'.$_file.$_formatString.'.php')) {
  96. $_path = VIEWPATH.$_theme.'/'.$this->module.'/'.$_file.$_formatString.'.php';
  97. } elseif ($_theme && is_file(THEMEPATH.$_theme.'/view/'.$this->module.'/'.$_file.$_formatString.'.php')) {
  98. $_path = THEMEPATH.$_theme.'/view/'.$this->module.'/'.$_file.$_formatString.'.php';
  99. } elseif (is_file(APPPATH.$_app.'/view/'.$this->module.'/'.$_file.$_formatString.'.php')) {
  100. $_path = APPPATH.$_app.'/view/'.$this->module.'/'.$_file.$_formatString.'.php';
  101. } elseif (is_file(VIEWPATH.$this->module.'/'.$_file.$_formatString.'.php')) {
  102. $_path = VIEWPATH.$this->module.'/'.$_file.$_formatString.'.php';
  103. } elseif(is_file(MODULEPATH.$this->module.'/view/'.$_file.$_formatString.'.php')) {
  104. $_path = MODULEPATH.$this->module.'/view/'.$_file.$_formatString.'.php';
  105. }
  106. if (!$_path && !$_format) {
  107. $_defaultFormat = $this->registry->settings->get('config/defaultFormat');
  108. if ($_theme && is_file(APPPATH.$_app.'/view/'.$_theme.'/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php')) {
  109. $_path = APPPATH.$_app.'/view/'.$_theme.'/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php';
  110. } elseif ($_theme && is_file(VIEWPATH.$_theme.'/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php')) {
  111. $_path = VIEWPATH.$_theme.'/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php';
  112. } elseif ($_theme && is_file(THEMEPATH.$_theme.'/view/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php')) {
  113. $_path = THEMEPATH.$_theme.'/view/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php';
  114. } elseif (is_file(APPPATH.$_app.'/view/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php')) {
  115. $_path = APPPATH.$_app.'/view/'.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php';
  116. } elseif (is_file(VIEWPATH.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php')) {
  117. $_path = VIEWPATH.$this->module.'/'.$_file.'.'.$_defaultFormat.'.php';
  118. } elseif(is_file(MODULEPATH.$this->module.'/view/'.$_file.'.'.$_defaultFormat.'.php')) {
  119. $_path = MODULEPATH.$this->module.'/view/'.$_file.'.'.$_defaultFormat.'.php';
  120. }
  121. }
  122. }
  123. else
  124. {
  125. if ($_theme && is_file(APPPATH.$_app.'/view/'.$_theme.'/'.$_file.$_formatString.'.php')) {
  126. $_path = APPPATH.$_app.'/view/'.$_theme.'/'.$_file.$_formatString.'.php';
  127. } elseif ($_theme && is_file(VIEWPATH.$_theme.'/'.$_file.$_formatString.'.php')) {
  128. $_path = VIEWPATH.$_theme.'/'.$_file.$_formatString.'.php';
  129. } elseif ($_theme && is_file(THEMEPATH.$_theme.'/view/'.$_file.$_formatString.'.php')) {
  130. $_path = THEMEPATH.$_theme.'/view/'.$_file.$_formatString.'.php';
  131. } elseif (is_file(APPPATH.$_app.'/view/'.$_file.$_formatString.'.php')) {
  132. $_path = APPPATH.$_app.'/view/'.$_file.$_formatString.'.php';
  133. } elseif (is_file(VIEWPATH.$_file.$_formatString.'.php')) {
  134. $_path = VIEWPATH.$_file.$_formatString.'.php';
  135. }
  136. if (!$_path) {
  137. if (!$_format) {
  138. $_defaultFormat = $this->registry->settings->get('config/defaultFormat');
  139. if ($_theme && is_file(APPPATH.$_app.'/view/'.$_theme.'/'.$_file.'.'.$_defaultFormat.'.php')) {
  140. $_path = APPPATH.$_app.'/view/'.$_theme.'/'.$_file.'.'.$_defaultFormat.'.php';
  141. } elseif ($_theme && is_file(VIEWPATH.$_theme.'/'.$_file.'.'.$_defaultFormat.'.php')) {
  142. $_path = VIEWPATH.$_theme.'/'.$_file.'.'.$_defaultFormat.'.php';
  143. } elseif ($_theme && is_file(THEMEPATH.$_theme.'/view/'.$_file.'.'.$_defaultFormat.'.php')) {
  144. $_path = THEMEPATH.$_theme.'/view/'.$_file.'.'.$_defaultFormat.'.php';
  145. } elseif (is_file(APPPATH.$_app.'/view/'.$_file.'.'.$_defaultFormat.'.php')) {
  146. $_path = APPPATH.$_app.'/view/'.$_file.'.'.$_defaultFormat.'.php';
  147. } elseif (is_file(VIEWPATH.$_file.'.'.$_defaultFormat.'.php')) {
  148. $_path = VIEWPATH.$_file.'.'.$_defaultFormat.'.php';
  149. }
  150. } else {
  151. $_defaultLayout = $this->registry->settings->get('config/defaultLayout', 'default');
  152. if ($_defaultLayout != $_file) {
  153. if ($_theme && is_file(APPPATH.$_app.'/view/'.$_theme.'/'.$_defaultLayout.$_formatString.'.php')) {
  154. $_path = APPPATH.$_app.'/view/'.$_theme.'/'.$_defaultLayout.$_formatString.'.php';
  155. } elseif ($_theme && is_file(VIEWPATH.$_theme.'/'.$_defaultLayout.$_formatString.'.php')) {
  156. $_path = VIEWPATH.$_theme.'/'.$_defaultLayout.$_formatString.'.php';
  157. } elseif ($_theme && is_file(THEMEPATH.$_theme.'/'.$_defaultLayout.$_formatString.'.php')) {
  158. $_path = THEMEPATH.$_theme.'/'.$_defaultLayout.$_formatString.'.php';
  159. } elseif (is_file(APPPATH.$_app.'/view/'.$_defaultLayout.$_formatString.'.php')) {
  160. $_path = APPPATH.$_app.'/view/'.$_defaultLayout.$_formatString.'.php';
  161. } elseif (is_file(VIEWPATH.$_defaultLayout.$_formatString.'.php')) {
  162. $_path = VIEWPATH.$_defaultLayout.$_formatString.'.php';
  163. }
  164. }
  165. }
  166. }
  167. }
  168. if (!$_path)
  169. {
  170. throw new Exception('View "'.$_file.$_formatString.'" for module '.$this->module.' not found!', 404);
  171. }
  172. $this->registry->cache->set('viewCached/'.$_app.'_'.$_theme.'_'.$this->module.'_'.str_replace('/', '__', $_file.$_formatString), $_path);
  173. }
  174. extract($this->vars);
  175. $h = $this->helper;
  176. $t = $this->t;
  177. $o = $this->helper->text;
  178. try {
  179. ob_start();
  180. include ($_path);
  181. $return = ob_get_clean();
  182. } catch (Exception $e) {
  183. ob_end_clean();
  184. throw $e;
  185. }
  186. }
  187. if ($this->cache) {
  188. $this->cache->save($return);
  189. }
  190. return $return;
  191. }
  192. public function setFile($file = null, $module = null) {
  193. $this->file = $file;
  194. if ($module !== null) {
  195. $this->module = $module;
  196. }
  197. return $this;
  198. }
  199. public function setModule($module = '_default') {
  200. $this->module = $module;
  201. return $this;
  202. }
  203. public function setApp($app = null) {
  204. $this->app = $app ? $app : $this->registry->settings->get('currentApp');
  205. return $this;
  206. }
  207. public function getCache()
  208. {
  209. return $this->cache;
  210. }
  211. public function setCache($cache)
  212. {
  213. $this->cache = $cache;
  214. return $this->cache->check();
  215. }
  216. public function selectCache($dependencies = array(), $tokens = array(), $bindToUrl = true, $ttl = 0)
  217. {
  218. $this->cache = $this->registry->helper->cache->get(
  219. $dependencies, $tokens, $bindToUrl
  220. );
  221. if ($ttl) {
  222. $this->cache->setTTL($ttl);
  223. }
  224. return $this->checkCache();
  225. }
  226. public function checkCache()
  227. {
  228. if (!$this->cache) {
  229. return false;
  230. }
  231. return $this->cache->check();
  232. }
  233. public function deleteCache($tokens = array())
  234. {
  235. $this->registry->helper->cache->delete($tokens);
  236. }
  237. public function prepareCache()
  238. {
  239. if (!$this->cache || !$content = $this->cache->load()) {
  240. throw new Exception('Cache for View "'.$_file.$_formatString.'" for module '.$this->module.' not found!', 404);
  241. }
  242. $this->cacheContent = $content;
  243. return $this;
  244. }
  245. /**
  246. *
  247. * @param string $file the file to use
  248. * @param mixed $module the name of the module that contains the file or null to use the current module
  249. * @return MiniMVC_View returns this view class
  250. */
  251. public function prepare($file = null, $module = null)
  252. {
  253. $this->setFile($file, $module);
  254. return $this;
  255. }
  256. public function prepareEmpty()
  257. {
  258. $this->file = null;
  259. $this->content = '';
  260. return $this;
  261. }
  262. public function prepareText($text = '')
  263. {
  264. $this->file = null;
  265. $this->content = $text;
  266. return $this;
  267. }
  268. public function prepareJSON($data = null)
  269. {
  270. $this->file = null;
  271. $this->content = json_encode(($data === null) ? $this->vars : $data);
  272. return $this;
  273. }
  274. public function prepareXML($data = null)
  275. {
  276. $xml = new XmlWriter();
  277. $xml->openMemory();
  278. $xml->startDocument('1.0', 'UTF-8');
  279. $xml->startElement('root');
  280. $this->writeXML($xml, ($data === null) ? $this->vars : $data);
  281. $xml->endElement();
  282. $this->file = null;
  283. $this->content = $xml->outputMemory(true);
  284. return $this;
  285. }
  286. private function writeXML(XMLWriter $xml, $data){
  287. foreach($data as $key => $value){
  288. if(is_array($value)){
  289. $xml->startElement($key);
  290. $this->writeXML($xml, $value);
  291. $xml->endElement();
  292. continue;
  293. }
  294. $xml->writeElement($key, $value);
  295. }
  296. }
  297. }