PageRenderTime 32ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/phing/lib/Capsule.php

https://github.com/IDCI-Consulting/WebsiteEval
PHP | 266 lines | 101 code | 36 blank | 129 comment | 25 complexity | f668c4479bb7cc40154bcd49ed543eee MD5 | raw file
  1. <?php
  2. /**
  3. * Capsule is a simple "template" engine that essentially provides an isolated context
  4. * for PHP scripts.
  5. *
  6. * There is no special templating language, and therefore no limitations to what
  7. * can be accomplished within templates. The main purpose of Capsule is to separate
  8. * the business logic from display / output logic.
  9. *
  10. * @author Hans Lellelid <hans@xmpl.org>
  11. * @version $Revision: 1.9 $ $Date: 2006-09-14 22:19:08 +0200 (Thu, 14 Sep 2006) $
  12. */
  13. class Capsule {
  14. /**
  15. * Look for templates here (if relative path provided).
  16. * @var string
  17. */
  18. protected $templatePath;
  19. /**
  20. * Where should output files be written?
  21. * (This is named inconsistently to be compatible w/ Texen.)
  22. * @var string
  23. */
  24. protected $outputDirectory;
  25. /**
  26. * The variables that can be used by the templates.
  27. * @var array Hash of variables.
  28. */
  29. public $vars = array();
  30. /**
  31. * Has template been initialized.
  32. */
  33. protected $initialized = false;
  34. /**
  35. * Stores the pre-parse() include_path.
  36. * @var string
  37. */
  38. private $old_include_path;
  39. function __construct() {
  40. }
  41. /**
  42. * Clears one or several or all variables.
  43. * @param mixed $which String name of var, or array of names.
  44. * @return void
  45. */
  46. function clear($which = null) {
  47. if ($which === null) {
  48. $this->vars = array();
  49. } elseif (is_array($which)) {
  50. foreach($which as $var) {
  51. unset($this->vars[$var]);
  52. }
  53. } else {
  54. unset($this->vars[$which]);
  55. }
  56. }
  57. /**
  58. * Set the basepath to use for template lookups.
  59. * @param string $v
  60. */
  61. function setTemplatePath($v) {
  62. $this->templatePath = rtrim($v, DIRECTORY_SEPARATOR.'/');
  63. }
  64. /**
  65. * Get the basepath to use for template lookups.
  66. * @return string
  67. */
  68. function getTemplatePath() {
  69. return $this->templatePath;
  70. }
  71. /**
  72. * Set a basepath to use for output file creation.
  73. * @param string $v
  74. */
  75. function setOutputDirectory($v) {
  76. $this->outputDirectory = rtrim($v, DIRECTORY_SEPARATOR.'/');
  77. }
  78. /**
  79. * Get basepath to use for output file creation.
  80. * @return string
  81. */
  82. function getOutputDirectory() {
  83. return $this->outputDirectory;
  84. }
  85. /**
  86. * Low overhead (no output buffering) method to simply dump template
  87. * to buffer.
  88. *
  89. * @param string $__template
  90. * @return void
  91. * @throws Exception - if template cannot be found
  92. */
  93. function display($__template) {
  94. // Prepend "private" variable names with $__ in this function
  95. // to keep namespace conflict potential to a minimum.
  96. // Alias this class to $generator.
  97. $generator = $this;
  98. if (isset($this->vars['this'])) {
  99. throw new Exception("Assigning a variable named \$this to a context conflicts with class namespace.");
  100. }
  101. // extract variables into local namespace
  102. extract($this->vars);
  103. // prepend template path to include path,
  104. // so that include "path/relative/to/templates"; can be used within templates
  105. $__old_inc_path = ini_get('include_path');
  106. ini_set('include_path', $this->templatePath . PATH_SEPARATOR . $__old_inc_path);
  107. @ini_set('track_errors', true);
  108. include $__template;
  109. @ini_restore('track_errors');
  110. // restore the include path
  111. ini_set('include_path', $__old_inc_path);
  112. if (!empty($php_errormsg)) {
  113. throw new Exception("Unable to parse template " . $__template . ": " . $php_errormsg);
  114. }
  115. }
  116. /**
  117. * Fetches the results of a tempalte parse and either returns
  118. * the string or writes results to a specified output file.
  119. *
  120. * @param string $template The template filename (relative to templatePath or absolute).
  121. * @param string $outputFile If specified, contents of template will also be written to this file.
  122. * @param boolean $append Should output be appended to source file?
  123. * @return string The "parsed" template output.
  124. * @throws Exception - if template not found.
  125. */
  126. function parse($template, $outputFile = null, $append = false) {
  127. // main work done right here:
  128. // hopefully this works recursively ... fingers crossed.
  129. ob_start();
  130. try {
  131. $this->display($template);
  132. } catch (Exception $e) {
  133. ob_end_flush(); // flush the output on error (so we can see up to what point it parsed everything)
  134. throw $e;
  135. }
  136. $output = ob_get_contents();
  137. ob_end_clean();
  138. if ($outputFile !== null) {
  139. $outputFile = $this->resolvePath($outputFile, $this->outputDirectory);
  140. $flags = null;
  141. if ($append) $flags = FILE_APPEND;
  142. if (!file_put_contents($outputFile, $output, $flags) && $output != "") {
  143. throw new Exception("Unable to write output to " . $outputFile);
  144. }
  145. }
  146. return $output;
  147. }
  148. /**
  149. * This returns a "best guess" path for the given file.
  150. *
  151. * @param string $file File name or possibly absolute path.
  152. * @param string $basepath The basepath that should be prepended if $file is not absolute.
  153. * @return string "Best guess" path for this file.
  154. */
  155. protected function resolvePath($file, $basepath) {
  156. if ( !($file{0} == DIRECTORY_SEPARATOR || $file{0} == '/')
  157. // also account for C:\ style path
  158. && !($file{1} == ':' && ($file{2} == DIRECTORY_SEPARATOR || $file{2} == '/'))) {
  159. if ($basepath != null) {
  160. $file = $basepath . DIRECTORY_SEPARATOR . $file;
  161. }
  162. }
  163. return $file;
  164. }
  165. /**
  166. * Gets value of specified var or NULL if var has not been put().
  167. * @param string $name Variable name to retrieve.
  168. * @return mixed
  169. */
  170. function get($name) {
  171. if (!isset($this->vars[$name])) return null;
  172. return $this->vars[$name];
  173. }
  174. /**
  175. * Merges in passed hash to vars array.
  176. *
  177. * Given an array like:
  178. *
  179. * array( 'myvar' => 'Hello',
  180. * 'myvar2' => 'Hello')
  181. *
  182. * Resulting template will have access to $myvar and $myvar2.
  183. *
  184. * @param array $vars
  185. * @param boolean $recursiveMerge Should matching keys be recursively merged?
  186. * @return void
  187. */
  188. function putAll($vars, $recursiveMerge = false) {
  189. if ($recursiveMerge) {
  190. $this->vars = array_merge_recursive($this->vars, $vars);
  191. } else {
  192. $this->vars = array_merge($this->vars, $vars);
  193. }
  194. }
  195. /**
  196. * Adds a variable to the context.
  197. *
  198. * Resulting template will have access to ${$name$} variable.
  199. *
  200. * @param string $name
  201. * @param mixed $value
  202. */
  203. function put($name, $value) {
  204. $this->vars[$name] = $value;
  205. }
  206. /**
  207. * Put a variable into the context, assigning it by reference.
  208. * This means that if the template modifies the variable, then it
  209. * will also be modified in the context.
  210. *
  211. * @param $name
  212. * @param &$value
  213. */
  214. function putRef($name, &$value) {
  215. $this->vars[$name] = &$value;
  216. }
  217. /**
  218. * Makes a copy of the value and puts it into the context.
  219. * This is primarily to force copying (cloning) of objects, rather
  220. * than the default behavior which is to assign them by reference.
  221. * @param string $name
  222. * @param mixed $value
  223. */
  224. function putCopy($name, $value) {
  225. if (is_object($value)) {
  226. $value = clone $value;
  227. }
  228. $this->vars[$name] = $value;
  229. }
  230. }