PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/application/libraries/dwoo/Dwoo/Adapters/ZendFramework/View.php

http://github.com/daylightstudio/FUEL-CMS
PHP | 501 lines | 210 code | 53 blank | 238 comment | 25 complexity | 03307236608de835153846cced353c0a MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Dwoo adapter for ZendFramework
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * In no event will the authors be held liable for any damages arising from the
  7. * use of this software.
  8. *
  9. * @author Denis Arh <denis@arh.cc>
  10. * @author Stephan Wentz <stephan@wentz.it>
  11. * @author Jordi Boggiano <j.boggiano@seld.be>
  12. * @copyright Copyright (c) 2008, Jordi Boggiano
  13. * @license http://dwoo.org/LICENSE Modified BSD License
  14. * @link http://dwoo.org/
  15. * @version 1.1.0
  16. * @date 2009-07-18
  17. * @package Dwoo
  18. */
  19. class Dwoo_Adapters_ZendFramework_View extends Zend_View_Abstract
  20. {
  21. /**
  22. * @var Dwoo
  23. */
  24. protected $_engine = null;
  25. /**
  26. * @var Dwoo_Data
  27. */
  28. protected $_dataProvider = null;
  29. /**
  30. * @var Dwoo_Compiler
  31. */
  32. protected $_compiler = null;
  33. /**
  34. * Changing Filter's scope to play nicely
  35. *
  36. * @var array
  37. */
  38. protected $_filter = array();
  39. /**
  40. * @var string
  41. */
  42. protected $_templateFileClass = 'Dwoo_Template_File';
  43. /**
  44. * @var array
  45. */
  46. protected $_templateFileSettings = array();
  47. /**
  48. * @var Dwoo_IPluginProxy
  49. */
  50. protected $_pluginProxy = null;
  51. /**
  52. * Constructor method.
  53. * See setOptions for $opt details
  54. *
  55. * @see setOptions
  56. * @param array|Zend_Config List of options or Zend_Config instance
  57. */
  58. public function __construct($opt = array())
  59. {
  60. if (is_array($opt)) {
  61. $this->setOptions($opt);
  62. } elseif ($opt instanceof Zend_Config) {
  63. $this->setConfig($opt);
  64. }
  65. $this->init();
  66. }
  67. /**
  68. * Set object state from options array
  69. * - engine = engine class name|engine object|array of options for engine
  70. * - dataProvider = data provider class name|data provider object|array of options for data provider
  71. * - compiler = compiler class name|compiler object|array of options for compiler
  72. * - templateFile =
  73. *
  74. * Array of options:
  75. * - type class name or object for engine, dataProvider or compiler
  76. * - any set* method (compileDir for setCompileDir ...)
  77. *
  78. * @param array $options
  79. * @return Dwoo_Adapters_ZendFramework_View
  80. */
  81. public function setOptions(array $opt = array())
  82. {
  83. // Making sure that everything is loaded.
  84. $classes = array('engine', 'dataProvider', 'compiler');
  85. // Setting options to Dwoo objects...
  86. foreach ($opt as $type => $settings) {
  87. if (!method_exists($this, 'set' . $type)) {
  88. throw new Dwoo_Exception("Unknown type $type");
  89. }
  90. if (is_string($settings) || is_object($settings)) {
  91. call_user_func(array($this, 'set' . $type), $settings);
  92. } elseif (is_array($settings)) {
  93. // Set requested class
  94. if (array_key_exists('type', $settings)) {
  95. call_user_func(array($this, 'set' . $type), $settings['type']);
  96. }
  97. if (in_array($type, $classes)) {
  98. // Call get so that the class is initialized
  99. $rel = call_user_func(array($this, 'get' . $type));
  100. // Call set*() methods so that all the settings are set.
  101. foreach ($settings as $method => $value) {
  102. if (method_exists($rel, 'set' . $method)) {
  103. call_user_func(array($rel, 'set' . $method), $value);
  104. }
  105. }
  106. } elseif ('templateFile' == $type) {
  107. // Remember the settings for the templateFile
  108. $this->_templateFileSettings = $settings;
  109. }
  110. }
  111. }
  112. }
  113. /**
  114. * Set object state from Zend_Config object
  115. *
  116. * @param Zend_Config $config
  117. * @return Dwoo_Adapters_ZendFramework_View
  118. */
  119. public function setConfig(Zend_Config $config)
  120. {
  121. return $this->setOptions($config->toArray());
  122. }
  123. /**
  124. * Called before template rendering
  125. *
  126. * Binds plugin proxy to the Dwoo.
  127. *
  128. * @see Dwoo_Adapters_ZendFramework_View::getPluginProxy();
  129. * @see Dwoo_Core::setPluginProxy();
  130. */
  131. protected function preRender()
  132. {
  133. $this->getEngine()->setPluginProxy($this->getPluginProxy());
  134. }
  135. /**
  136. * Wraper for Dwoo_Data::__set()
  137. * allows to assign variables using the object syntax
  138. *
  139. * @see Dwoo_Data::__set()
  140. * @param string $name the variable name
  141. * @param string $value the value to assign to it
  142. */
  143. public function __set($name, $value)
  144. {
  145. $this->getDataProvider()->__set($name, $value);
  146. }
  147. /**
  148. * Sraper for Dwoo_Data::__get() allows to read variables using the object
  149. * syntax
  150. *
  151. * @see Dwoo_Data::__get()
  152. * @param string $name the variable name
  153. * @return mixed
  154. */
  155. public function __get($name)
  156. {
  157. return $this->getDataProvider()->__get($name);
  158. }
  159. /**
  160. * Wraper for Dwoo_Data::__isset()
  161. * supports calls to isset($dwooData->var)
  162. *
  163. * @see Dwoo_Data::__isset()
  164. * @param string $name the variable name
  165. */
  166. public function __isset($name)
  167. {
  168. return $this->getDataProvider()->__isset($name);
  169. }
  170. /**
  171. * Wraper for Dwoo_Data::_unset()
  172. * supports unsetting variables using the object syntax
  173. *
  174. * @see Dwoo_Data::__unset()
  175. * @param string $name the variable name
  176. */
  177. public function __unset($name)
  178. {
  179. $this->getDataProvider()->__unset($name);
  180. }
  181. /**
  182. * Catches clone request and clones data provider
  183. */
  184. public function __clone() {
  185. $this->setDataProvider(clone $this->getDataProvider());
  186. }
  187. /**
  188. * Returns plugin proxy interface
  189. *
  190. * @return Dwoo_IPluginProxy
  191. */
  192. public function getPluginProxy()
  193. {
  194. if (!$this->_pluginProxy) {
  195. $this->_pluginProxy = new Dwoo_Adapters_ZendFramework_PluginProxy($this);
  196. }
  197. return $this->_pluginProxy;
  198. }
  199. /**
  200. * Sets plugin proxy
  201. *
  202. * @param Dwoo_IPluginProxy
  203. * @return Dwoo_Adapters_ZendFramework_View
  204. */
  205. public function setPluginProxy(Dwoo_IPluginProxy $pluginProxy)
  206. {
  207. $this->_pluginProxy = $pluginProxy;
  208. return $this;
  209. }
  210. /**
  211. * Sets template engine
  212. *
  213. * @param string|Dwoo Object or name of the class
  214. */
  215. public function setEngine($engine)
  216. {
  217. // if param given as an object
  218. if ($engine instanceof Dwoo) {
  219. $this->_engine = $engine;
  220. }
  221. //
  222. elseif (is_subclass_of($engine, 'Dwoo') || 'Dwoo' === $engine) {
  223. $this->_engine = new $engine();
  224. }
  225. else {
  226. throw new Dwoo_Exception("Custom engine must be a subclass of Dwoo");
  227. }
  228. }
  229. /**
  230. * Return the Dwoo template engine object
  231. *
  232. * @return Dwoo
  233. */
  234. public function getEngine()
  235. {
  236. if (null === $this->_engine) {
  237. $this->_engine = new Dwoo_Adapters_ZendFramework_Dwoo();
  238. }
  239. return $this->_engine;
  240. }
  241. /**
  242. * Sets Dwoo data object
  243. *
  244. * @param string|Dwoo_Data Object or name of the class
  245. */
  246. public function setDataProvider($data)
  247. {
  248. if ($data instanceof Dwoo_IDataProvider) {
  249. $this->_dataProvider = $data;
  250. }
  251. elseif (is_subclass_of($data, 'Dwoo_Data') || 'Dwoo_Data' == $data) {
  252. $this->_dataProvider = new $data();
  253. }
  254. else {
  255. throw new Dwoo_Exception("Custom data provider must be a subclass of Dwoo_Data or instance of Dwoo_IDataProvider");
  256. }
  257. }
  258. /**
  259. * Return the Dwoo data object
  260. *
  261. * @return Dwoo_Data
  262. */
  263. public function getDataProvider()
  264. {
  265. if (null === $this->_dataProvider) {
  266. $this->_dataProvider = new Dwoo_Data;
  267. }
  268. return $this->_dataProvider;
  269. }
  270. /**
  271. * Sets Dwoo compiler
  272. *
  273. * @param string|Dwoo_Compiler Object or name of the class
  274. */
  275. public function setCompiler($compiler)
  276. {
  277. // if param given as an object
  278. if ($compiler instanceof Dwoo_ICompiler) {
  279. $this->_compiler = $compiler;
  280. }
  281. // if param given as a string
  282. elseif (is_subclass_of($compiler, 'Dwoo_Compiler') || 'Dwoo_Compiler' == $compiler) {
  283. $this->_compiler = new $compiler;
  284. }
  285. else {
  286. throw new Dwoo_Exception("Custom compiler must be a subclass of Dwoo_Compiler or instance of Dwoo_ICompiler");
  287. }
  288. }
  289. /**
  290. * Return the Dwoo compiler object
  291. *
  292. * @return Dwoo_Data
  293. */
  294. public function getCompiler()
  295. {
  296. if (null === $this->_compiler) {
  297. $this->_compiler = Dwoo_Compiler::compilerFactory();
  298. }
  299. return $this->_compiler;
  300. }
  301. /**
  302. * Initializes Dwoo_ITemplate type of class and sets properties from _templateFileSettings
  303. *
  304. * @param string Template location
  305. * @return Dwoo_ITemplate
  306. */
  307. public function getTemplateFile($template) {
  308. $templateFileClass = $this->_templateFileClass;
  309. $dwooTemplateFile = new $templateFileClass($template);
  310. if (!($dwooTemplateFile instanceof Dwoo_ITemplate)) {
  311. throw new Dwoo_Exception("Custom templateFile class must be a subclass of Dwoo_ITemplate");
  312. }
  313. foreach ($this->_templateFileSettings as $method => $value) {
  314. if (method_exists($dwooTemplateFile, 'set' . $method)) {
  315. call_user_func(array($dwooTemplateFile, 'set' . $method), $value);
  316. }
  317. }
  318. return $dwooTemplateFile;
  319. }
  320. /**
  321. * Dwoo_ITemplate type of class
  322. *
  323. * @param string Name of the class
  324. * @return void
  325. */
  326. public function setTemplateFile($tempateFileClass) {
  327. $this->_templateFileClass = $tempateFileClass;
  328. }
  329. /**
  330. * Passes data to Dwoo_Data object
  331. *
  332. * @see Dwoo_Data::assign()
  333. * @param array|string $name
  334. * @param mixed $val
  335. * @return Dwoo_Adapters_ZendFramework_View
  336. */
  337. public function assign($name, $val = null)
  338. {
  339. $this->getDataProvider()->assign($name, $val);
  340. return $this;
  341. }
  342. /**
  343. * Return list of all assigned variables
  344. *
  345. * @return array
  346. */
  347. public function getVars()
  348. {
  349. return $this->getDataProvider()->getData();
  350. }
  351. /**
  352. * Clear all assigned variables
  353. *
  354. * Clears all variables assigned to Zend_View either via {@link assign()} or
  355. * property overloading ({@link __get()}/{@link __set()}).
  356. *
  357. * @return void
  358. * @return Dwoo_Adapters_ZendFramework_View
  359. */
  360. public function clearVars()
  361. {
  362. $this->getDataProvider()->clear();
  363. return $this;
  364. }
  365. /**
  366. * Wraper for parent's render method so preRender method
  367. * can be called (that will bind the plugin proxy to the
  368. * engine.
  369. *
  370. * @see Zend_View_Abstract::render
  371. * @return string The script output.
  372. */
  373. public function render($name)
  374. {
  375. $this->preRender();
  376. return parent::render($name);
  377. }
  378. /**
  379. * Processes a view script and outputs it. Output is then
  380. * passed through filters.
  381. *
  382. * @param string $name The script script name to process.
  383. * @return string The script output.
  384. */
  385. public function _run()
  386. {
  387. echo $this->_engine->get(
  388. $this->getTemplateFile(func_get_arg(0)),
  389. $this->getDataProvider(),
  390. $this->getCompiler()
  391. );
  392. }
  393. /**
  394. * Add plugin path
  395. *
  396. * @param string $dir Directory
  397. * @return Dwoo_Adapters_ZendFramework_View
  398. */
  399. public function addPluginDir($dir)
  400. {
  401. $this->getEngine()->getLoader()->addDirectory($dir);
  402. return $this;
  403. }
  404. /**
  405. * Set compile path
  406. *
  407. * @param string $dir Directory
  408. * @return Dwoo_Adapters_ZendFramework_View
  409. */
  410. public function setCompileDir($dir)
  411. {
  412. $this->getEngine()->setCompileDir($dir);
  413. return $this;
  414. }
  415. /**
  416. * Set cache path
  417. *
  418. * @param string $dir Directory
  419. * @return Dwoo_Adapters_ZendFramework_View
  420. */
  421. public function setCacheDir($dir)
  422. {
  423. $this->getEngine()->setCacheDir($dir);
  424. return $this;
  425. }
  426. /**
  427. * Set cache lifetime
  428. *
  429. * @param string $seconds Lifetime in seconds
  430. * @return Dwoo_Adapters_ZendFramework_View
  431. */
  432. public function setCacheLifetime($seconds)
  433. {
  434. $this->getEngine()->setCacheTime($seconds);
  435. return $this;
  436. }
  437. /**
  438. * Set charset
  439. *
  440. * @param string $charset
  441. * @return Dwoo_Adapters_ZendFramework_View
  442. */
  443. public function setCharset($charset)
  444. {
  445. $this->_engine->setCharset($charset);
  446. return $this;
  447. }
  448. }