Cgiapp2
[ class tree: Cgiapp2 ] [ index: Cgiapp2 ] [ all elements ]

Source for file Savant2.class.php

Documentation is available at Savant2.class.php

  1. <?php
  2. /**
  3. * Cgiapp2 - Framework for building reusable web-applications
  4. *
  5. * A PHP5 port of perl's CGI::Application, a framework for building reusable web
  6. * applications.
  7. *
  8. * @package Cgiapp2
  9. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>; based on
  10. * CGI::Application, by Jesse Erlbaum <jesse@erlbaum.net>, et. al.
  11. * @copyright (c) 2004 - present, Matthew Weier O'Phinney
  12. * @license BSD License (http://www.opensource.org/licenses/bsd-license.php)
  13. * @category Tools and Utilities
  14. * @tutorial Cgiapp2/Cgiapp2.cls
  15. * @version $Id:$
  16. */
  17.  
  18. /**
  19. * Cgiapp2 plugin
  20. */
  21. require_once 'Cgiapp2.class.php';
  22.  
  23. /**
  24. * Implements Cgiapp2_Plugin_Template_Interface
  25. */
  26. require_once 'Cgiapp2/Plugin/Template/Interface.class.php';
  27.  
  28. /**
  29. * Savant2 Template Plugin for Cgiapp2
  30. *
  31. * Implements {@link Cgiapp2_Plugin_Template_Interface} to create a Savant2
  32. * plugin for {@link Cgiapp2}.
  33. *
  34. * Additionally, Cgiapp2_Plugin_Savant2 implements a singleton and dynamic proxy.
  35. * This allows you to do the following:
  36. * <code>
  37. * Cgiapp2_Plugin_Savant2::getInstance()->loadFilter('trimwhitespace');
  38. * </code>
  39. *
  40. * Registers with Cgiapp2's tmpl_path, tmpl_assign, and tmpl_fetch hooks;
  41. * registration is done with the Cgiapp2 class.
  42. *
  43. * @package Cgiapp2
  44. * @author Matthew Weier O'Phinney <mweierophinney@gmail.com>
  45. * @version @release-version@
  46. */
  47. class Cgiapp2_Plugin_Savant2 implements Cgiapp2_Plugin_Template_Interface
  48. {
  49. /**
  50. * Path to template storage root
  51. *
  52. * @var string
  53. * @access protected
  54. */
  55. protected $tmpl_path;
  56.  
  57. /**
  58. * Hold template instance
  59. *
  60. * @var string
  61. * @access private
  62. * @static
  63. */
  64. private static $instance = false;
  65.  
  66. /**
  67. * Savant2 instance
  68. *
  69. * @var object
  70. * @access protected
  71. */
  72. protected $t;
  73.  
  74. /**
  75. * Constructor
  76. *
  77. * Creates an instance and initializes the {@link $tmpl_path} property.
  78. *
  79. * @param mixed $tmpl_path
  80. * @param mixed $extra_params
  81. * @access private
  82. * @return void
  83. */
  84. private function __construct($tmpl_path, $extra_params)
  85. {
  86. $savant2 = new Savant2($extra_params);
  87.  
  88. $this->t = $savant2;
  89. $this->tmpl_path = $tmpl_path;
  90. $this->setPath('template', $tmpl_path);
  91. }
  92.  
  93. /**
  94. * Dynamic proxy for Savant2 methods
  95. *
  96. * Allows calling Savant2 methods as part of the Cgiapp2_Plugin_Savant2
  97. * object. Since the class also implements the singleton pattern, you may
  98. * easily call Savant2 methods from anywhere:
  99. * <code>
  100. * Cgiapp2_Plugin_Savant2::getInstance()->loadFilter('trimwhitespace');
  101. * </code>
  102. *
  103. * @param mixed $method
  104. * @param mixed $args
  105. * @access public
  106. * @return void
  107. */
  108. public function __call($method, $args)
  109. {
  110. if (method_exists($this->t, $method)) {
  111. return call_user_func_array(array($this->t, $method), $args);
  112. }
  113. }
  114.  
  115. /**
  116. * Proxy: retrieve Savant2 property values
  117. *
  118. * @access public
  119. * @param string $key
  120. * @return mixed
  121. */
  122. public function __get($key)
  123. {
  124. if (isset($this->t->$key)) {
  125. return $this->t->$key;
  126. }
  127.  
  128. return;
  129. }
  130.  
  131. /**
  132. * Proxy: set Savant2 property values
  133. *
  134. * @access public
  135. * @param mixed $key
  136. * @param mixed $val
  137. * @return void
  138. */
  139. public function __set($key, $val)
  140. {
  141. $this->t->$key = $val;
  142.  
  143. return;
  144. }
  145.  
  146. /**
  147. * Singleton
  148. *
  149. * Returns false if unable to find instance, Savant2, or missing arguments.
  150. * You can access this at any time:
  151. * <code>
  152. * $tpl = Cgiapp2_Plugin_Savant2::getInstance();
  153. * </code>
  154. *
  155. * @static
  156. * @access public
  157. * @param mixed $cgiapp Cgiapp2 instance object
  158. * @param mixed $tmpl_path Path to template root directory
  159. * @param mixed $extra_params Optional; extra parameters with which to
  160. * initialize Savant2
  161. * @return void
  162. * @throws Exception if unable to insantiate Savant2
  163. */
  164. public static function getInstance()
  165. {
  166. // Return instance if it exists already
  167. if (false !== ($instance = self::$instance)) {
  168. return $instance;
  169. }
  170.  
  171. // Get arguments and any extra params
  172. $args = func_get_args();
  173. if (2 > count($args)) {
  174. throw new Exception('No instance found, and missing arguments to create one');
  175. return false;
  176. }
  177. $extra_params = null;
  178. $cgiapp = array_shift($args);
  179. $tmpl_path = array_shift($args);
  180. if (0 < count($args)) {
  181. $extra_params = array_shift($args);
  182. }
  183.  
  184. // Set Savant2 path and include Savant2 class
  185. if (false !== ($SAVANT2_PATH = $cgiapp->param('SAVANT2_PATH'))) {
  186. $PATH = ini_get('include_path');
  187. ini_set('include_path', $SAVANT2_PATH . ':' . $PATH);
  188. }
  189. @include_once 'Savant2.php';
  190. if (!class_exists('Savant2')) {
  191. throw new Exception('Savant2 not found');
  192. return false;
  193. }
  194.  
  195. self::$instance = new Cgiapp2_Plugin_Savant2($tmpl_path, $extra_params);
  196. return self::$instance;
  197. }
  198.  
  199. /**
  200. * Set the template path
  201. *
  202. * Uses $tmpl_path to add a path via Savant2's setPath() method.
  203. *
  204. * @access public
  205. * @param mixed $tmpl_path
  206. * @return void
  207. */
  208. public function setTmplPath($tmpl_path)
  209. {
  210. $this->tmpl_path = $tmpl_path;
  211. $this->setPath('template', $tmpl_path);
  212. }
  213.  
  214. /**
  215. * Initialize a template instance and/or set the template path
  216. *
  217. * If no Cgiapp2_Plugin_Savant2 instance currently exists, it is first
  218. * initialized via {@link getInstance()} using the template path, extra
  219. * parameters, and Cgiapp2-based object instance. Savant2 is looked for in
  220. * the Cgiapp2 instance parameter 'SAVANT2_PATH'; if not found, or an error
  221. * occurs initializing Savant2, a warning is raised.
  222. *
  223. * init() is used by Savant2 to set the template path. If the template path
  224. * has not changed, nothing is done.
  225. *
  226. * @static
  227. * @access public
  228. * @param mixed $cgiapp
  229. * @param mixed $tmpl_path
  230. * @param mixed $extra_params
  231. * @return bool
  232. */
  233. public static function init(Cgiapp2 $cgiapp, $tmpl_path, $extra_params = null)
  234. {
  235. // Get savant2 object
  236. $instance = self::getInstance($cgiapp, $tmpl_path, $extra_params);
  237.  
  238. if ($instance->tmpl_path != $tmpl_path) {
  239. $instance->setTmplPath($tmpl_path);
  240. }
  241.  
  242. return true;
  243. }
  244.  
  245. /**
  246. * Assign a variable or variables to a template
  247. *
  248. * assign() can be used to assign data to a template. Internally, it calls
  249. * Savant2's assign() method.
  250. *
  251. * You can also send it an associative array of variable names => values,
  252. * and all elements included will be sent to the template.
  253. *
  254. * @static
  255. * @access public
  256. * @return bool
  257. * @throws Exception when bad data passed
  258. */
  259. public static function assign(Cgiapp2 $cgiapp)
  260. {
  261. $args = func_get_args();
  262. $cgiapp = array_shift($args);
  263. $argc = count($args);
  264.  
  265. if (1 == $argc) {
  266. $values = array_shift($args);
  267. if (Cgiapp2::is_assoc_array($values)) {
  268. self::getInstance()->t->assign($values);
  269. } else {
  270. throw new Exception('Bad array passed to Cgiapp2_Plugin_Savant2::assign()');
  271. return false;
  272. }
  273. } elseif (2 == $argc) {
  274. $key = array_shift($args);
  275. if (is_string($key)) {
  276. $val = array_shift($args);
  277. self::getInstance()->t->assign(trim($key), $val);
  278. } else {
  279. throw new Exception('Attempting to assign non-string key');
  280. return false;
  281. }
  282. } else {
  283. throw new Exception('Bad number or type of arguments passed to Cgiapp2_Plugin_Savant2::assign()');
  284. return false;
  285. }
  286.  
  287. return true;
  288. }
  289.  
  290. /**
  291. * Fetch template contents
  292. *
  293. * If not template file is provided, returns an empty string. If unable to
  294. * locate a Cgiapp2_Plugin_Savant2 instance, returns an empty string and
  295. * raises a warning.
  296. *
  297. * Internally, calls Savant2's fetch() method:
  298. * <code>
  299. * $savant2->fetch('some.tpl');
  300. * </code>
  301. *
  302. * @static
  303. * @access public
  304. * @param mixed $cgiapp
  305. * @param mixed $tmpl_file
  306. * @return string
  307. */
  308. public static function fetch(Cgiapp2 $cgiapp, $tmpl_file)
  309. {
  310. if (empty($tmpl_file) || !is_string($tmpl_file)) {
  311. return '';
  312. }
  313.  
  314. $output = self::getInstance()->t->fetch($tmpl_file);
  315. return $output;
  316. }
  317. }
  318.  
  319. /**
  320. * Register callbacks with tmpl_path, tmpl_assign, and tmpl_fetch callback hooks
  321. * of Cgiapp2
  322. */
  323. Cgiapp2::add_callback('tmpl_path', array('Cgiapp2_Plugin_Savant2', 'init'), 'Cgiapp2');
  324. Cgiapp2::add_callback('tmpl_assign', array('Cgiapp2_Plugin_Savant2', 'assign'), 'Cgiapp2');
  325. Cgiapp2::add_callback('tmpl_fetch', array('Cgiapp2_Plugin_Savant2', 'fetch'), 'Cgiapp2');
  326. ?>

Documentation generated on Sat, 03 Jun 2006 10:48:53 -0400 by phpDocumentor 1.3.0RC5