PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/vendor/phing/tasks/ext/pdo/PDOSQLExecFormatterElement.php

https://bitbucket.org/ealexandru/jobeet
PHP | 312 lines | 117 code | 37 blank | 158 comment | 10 complexity | dff3de6f8ed3176e38e5010152ea8b72 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /**
  3. * $Id: FormatterElement.php 148 2007-02-13 11:15:53Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/system/io/PhingFile.php';
  22. require_once 'phing/tasks/ext/pdo/PlainPDOResultFormatter.php';
  23. require_once 'phing/tasks/ext/pdo/XMLPDOResultFormatter.php';
  24. /**
  25. * A class to represent the nested <formatter> element for PDO SQL results.
  26. *
  27. * This class is inspired by the similarly-named class in the PHPUnit tasks.
  28. *
  29. * @author Hans Lellelid <hans@xmpl.org>
  30. * @package phing.tasks.ext.pdo
  31. * @since 2.3.0
  32. */
  33. class PDOSQLExecFormatterElement
  34. {
  35. /**
  36. * @var PDOResultFormatter
  37. */
  38. private $formatter;
  39. /**
  40. * The type of the formatter (used for built-in formatter classes).
  41. * @var string
  42. */
  43. private $type = "";
  44. /**
  45. * Whether to use file (or write output to phing log).
  46. * @var boolean
  47. */
  48. private $useFile = true;
  49. /**
  50. * Output file for formatter.
  51. * @var PhingFile
  52. */
  53. private $outfile;
  54. /**
  55. * Print header columns.
  56. * @var boolean
  57. */
  58. private $showheaders = true;
  59. /**
  60. * Whether to format XML output.
  61. * @var boolean
  62. */
  63. private $formatoutput = true;
  64. /**
  65. * Encoding for XML output.
  66. * @var string
  67. */
  68. private $encoding;
  69. /**
  70. * Column delimiter.
  71. * Defaults to ','
  72. * @var string
  73. */
  74. private $coldelimiter = ",";
  75. /**
  76. * Row delimiter.
  77. * Defaults to PHP_EOL.
  78. * @var string
  79. */
  80. private $rowdelimiter = PHP_EOL;
  81. /**
  82. * Append to an existing file or overwrite it?
  83. * @var boolean
  84. */
  85. private $append = false;
  86. /**
  87. * Parameters for a custom formatter.
  88. * @var array Parameter[]
  89. */
  90. private $formatterParams = array();
  91. /**
  92. * @var PDOSQLExecTask
  93. */
  94. private $parentTask;
  95. /**
  96. * Construct a new PDOSQLExecFormatterElement with parent task.
  97. * @param PDOSQLExecTask $parentTask
  98. */
  99. public function __construct(PDOSQLExecTask $parentTask)
  100. {
  101. $this->parentTask = $parentTask;
  102. }
  103. /**
  104. * Supports nested <param> element (for custom formatter classes).
  105. * @return Parameter
  106. */
  107. public function createParam() {
  108. $num = array_push($this->parameters, new Parameter());
  109. return $this->parameters[$num-1];
  110. }
  111. /**
  112. * Gets a configured output writer.
  113. * @return Writer
  114. */
  115. private function getOutputWriter()
  116. {
  117. if ($this->useFile) {
  118. $of = $this->getOutfile();
  119. if (!$of) {
  120. $of = new PhingFile($this->formatter->getPreferredOutfile());
  121. }
  122. return new FileWriter($of, $this->append);
  123. } else {
  124. return $this->getDefaultOutput();
  125. }
  126. }
  127. /**
  128. * Configures wrapped formatter class with any attributes on this element.
  129. */
  130. public function prepare() {
  131. if (!$this->formatter) {
  132. throw new BuildException("No formatter specified (use type or classname attribute)", $this->getLocation());
  133. }
  134. $out = $this->getOutputWriter();
  135. print "Setting output writer to: " . get_class($out) . "\n";
  136. $this->formatter->setOutput($out);
  137. if ($this->formatter instanceof PlainPDOResultFormatter) {
  138. // set any options that apply to the plain formatter
  139. $this->formatter->setShowheaders($this->showheaders);
  140. $this->formatter->setRowdelim($this->rowdelimiter);
  141. $this->formatter->setColdelim($this->coldelimiter);
  142. } elseif ($this->formatter instanceof XMLPDOResultFormatter) {
  143. // set any options that apply to the xml formatter
  144. $this->formatter->setEncoding($this->encoding);
  145. $this->formatter->setFormatOutput($this->formatoutput);
  146. }
  147. foreach($this->formatterParams as $param) {
  148. $param = new Parameter();
  149. $method = 'set' . $param->getName();
  150. if (!method_exists($this->formatter, $param->getName())) {
  151. throw new BuildException("Formatter " . get_class($this->formatter) . " does not have a $method method.", $this->getLocation());
  152. }
  153. call_user_func(array($this->formatter, $method), $param->getValue());
  154. }
  155. }
  156. /**
  157. * Sets the formatter type.
  158. * @param string $type
  159. */
  160. function setType($type) {
  161. $this->type = $type;
  162. if ($this->type == "xml") {
  163. $this->formatter = new XMLPDOResultFormatter();
  164. } elseif ($this->type == "plain") {
  165. $this->formatter = new PlainPDOResultFormatter();
  166. } else {
  167. throw new BuildException("Formatter '" . $this->type . "' not implemented");
  168. }
  169. }
  170. /**
  171. * Set classname for a custom formatter (must extend PDOResultFormatter).
  172. * @param string $className
  173. */
  174. function setClassName($className) {
  175. $classNameNoDot = Phing::import($className);
  176. $this->formatter = new $classNameNoDot();
  177. }
  178. /**
  179. * Set whether to write formatter results to file.
  180. * @param boolean $useFile
  181. */
  182. function setUseFile($useFile) {
  183. $this->useFile = (boolean) $useFile;
  184. }
  185. /**
  186. * Return whether to write formatter results to file.
  187. * @return boolean
  188. */
  189. function getUseFile() {
  190. return $this->useFile;
  191. }
  192. /**
  193. * Sets the output file for the formatter results.
  194. * @param PhingFile $outFile
  195. */
  196. function setOutfile(PhingFile $outfile) {
  197. $this->outfile = $outfile;
  198. }
  199. /**
  200. * Get the output file.
  201. * @return PhingFile
  202. */
  203. function getOutfile() {
  204. return $this->outfile;
  205. /*
  206. } else {
  207. return new PhingFile($this->formatter->getPreferredOutfile());
  208. }*/
  209. }
  210. /**
  211. * whether output should be appended to or overwrite
  212. * an existing file. Defaults to false.
  213. * @param boolean $append
  214. */
  215. public function setAppend($append) {
  216. $this->append = (boolean) $append;
  217. }
  218. /**
  219. * Whether output should be appended to file.
  220. * @return boolean
  221. */
  222. public function getAppend() {
  223. return $this->append;
  224. }
  225. /**
  226. * Print headers for result sets from the
  227. * statements; optional, default true.
  228. * @param boolean $showheaders
  229. */
  230. public function setShowheaders($showheaders) {
  231. $this->showheaders = (boolean) $showheaders;
  232. }
  233. /**
  234. * Sets the column delimiter.
  235. * @param string $v
  236. */
  237. public function setColdelim($v) {
  238. $this->coldelimiter = $v;
  239. }
  240. /**
  241. * Sets the row delimiter.
  242. * @param string $v
  243. */
  244. public function setRowdelim($v) {
  245. $this->rowdelimiter = $v;
  246. }
  247. /**
  248. * Set the DOM document encoding.
  249. * @param string $v
  250. */
  251. public function setEncoding($v) {
  252. $this->encoding = $v;
  253. }
  254. /**
  255. * @param boolean $v
  256. */
  257. public function setFormatOutput($v) {
  258. $this->formatOutput = (boolean) $v;
  259. }
  260. /**
  261. * Gets a default output writer for this task.
  262. * @return Writer
  263. */
  264. private function getDefaultOutput()
  265. {
  266. return new LogWriter($this->parentTask);
  267. }
  268. /**
  269. * Gets the formatter that has been configured based on this element.
  270. * @return PDOResultFormatter
  271. */
  272. function getFormatter() {
  273. return $this->formatter;
  274. }
  275. }