PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DocBlox/Transformer/Transformation.php

https://github.com/androa/Docblox
PHP | 265 lines | 96 code | 25 blank | 144 comment | 2 complexity | 457922bedab4ad25481afc86cd1a84a6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * DocBlox
  4. *
  5. * PHP Version 5
  6. *
  7. * @category DocBlox
  8. * @package Transformer
  9. * @author Mike van Riel <mike.vanriel@naenius.com>
  10. * @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
  11. * @license http://www.opensource.org/licenses/mit-license.php MIT
  12. * @link http://docblox-project.org
  13. */
  14. /**
  15. * Class representing a single Transformation.
  16. *
  17. * @category DocBlox
  18. * @package Transformer
  19. * @author Mike van Riel <mike.vanriel@naenius.com>
  20. * @license http://www.opensource.org/licenses/mit-license.php MIT
  21. * @link http://docblox-project.org
  22. */
  23. class DocBlox_Transformer_Transformation extends DocBlox_Core_Abstract
  24. {
  25. /** @var string */
  26. protected $query = '';
  27. /** @var DocBlox_Transformer_Writer_Abstract */
  28. protected $writer = null;
  29. /** @var string */
  30. protected $source = '';
  31. /** @var string */
  32. protected $artifact = '';
  33. /** @var string[] */
  34. protected $parameters = array();
  35. /** @var DocBlox_Transformer */
  36. protected $transformer = null;
  37. /**
  38. * Constructs a new Transformation object and populates the required parameters.
  39. *
  40. * @param DocBlox_Transformer $transformer The parent transformer.
  41. * @param string $query What information to use as datasource for the writer's source.
  42. * @param string $writer What type of transformation to apply (XSLT, PDF, etc).
  43. * @param string $source Which template or type of source to use.
  44. * @param string $artifact What is the filename of the result (relative to the generated root)
  45. */
  46. public function __construct(DocBlox_Transformer $transformer, $query, $writer, $source, $artifact)
  47. {
  48. $this->setTransformer($transformer);
  49. $this->setQuery($query);
  50. $this->setWriter($writer);
  51. $this->setSource($source);
  52. $this->setArtifact($artifact);
  53. }
  54. /**
  55. * Sets the transformer object responsible for maintaining the transformations.
  56. *
  57. * @param DocBlox_Transformer $transformer
  58. *
  59. * @return void
  60. */
  61. public function setTransformer(DocBlox_Transformer $transformer)
  62. {
  63. $this->transformer = $transformer;
  64. }
  65. /**
  66. * Returns the transformer object which is responsible for maintaining this transformation.
  67. *
  68. * @return DocBlox_Transformer
  69. */
  70. public function getTransformer()
  71. {
  72. return $this->transformer;
  73. }
  74. /**
  75. * Sets the query.
  76. *
  77. * @param string $query
  78. *
  79. * @return void
  80. */
  81. public function setQuery($query)
  82. {
  83. $this->query = $query;
  84. }
  85. /**
  86. * Returns the set query.
  87. *
  88. * @return string
  89. */
  90. public function getQuery()
  91. {
  92. return $this->query;
  93. }
  94. /**
  95. * Sets the writer type and instantiates a writer.
  96. *
  97. * @param string $writer
  98. *
  99. * @return void
  100. */
  101. public function setWriter($writer)
  102. {
  103. $this->writer = DocBlox_Transformer_Writer_Abstract::getInstanceOf($writer);
  104. }
  105. /**
  106. * Returns an instantiated writer object.
  107. *
  108. * @return DocBlox_Transformer_Writer_Abstract|null
  109. */
  110. public function getWriter()
  111. {
  112. return $this->writer;
  113. }
  114. /**
  115. * Sets the source / type which the writer will use to generate artifacts from.
  116. *
  117. * @param string $source
  118. *
  119. * @return void
  120. */
  121. public function setSource($source)
  122. {
  123. $this->source = $source;
  124. }
  125. /**
  126. * Returns the name of the source / type used in the transformation process.
  127. *
  128. * @return string
  129. */
  130. public function getSource()
  131. {
  132. return $this->source;
  133. }
  134. /**
  135. * Returns the source as a path instead of a regular value.
  136. *
  137. * This method applies the following rules to the value of $source:
  138. *
  139. * 1. if the template_path parameter is set and that combined with the
  140. * source gives an existing file; return that.
  141. * 2. if the value exists as a file (either relative to the current working
  142. * directory or absolute), do a realpath and return it.
  143. * 3. Otherwise prepend it with the DocBlox data folder, if that does
  144. * not exist: throw an exception
  145. *
  146. * @throws Exception if no valid file could be found.
  147. *
  148. * @return string
  149. */
  150. public function getSourceAsPath()
  151. {
  152. // externally loaded templates set this parameter so that theme
  153. // resources may be placed in the same folder as the template.
  154. if ($this->getParameter('template_path') !== null)
  155. {
  156. $path = rtrim($this->getParameter('template_path'), '/\\');
  157. if (file_exists($path . DIRECTORY_SEPARATOR . $this->source)) {
  158. return realpath($path . DIRECTORY_SEPARATOR . $this->source);
  159. }
  160. }
  161. if (file_exists($this->source)) {
  162. return realpath($this->source);
  163. }
  164. $file = rtrim($this->getConfig()->paths->data, '/\\')
  165. . DIRECTORY_SEPARATOR . $this->source;
  166. if (!file_exists($file))
  167. {
  168. throw new Exception('The source path does not exist: '.$file);
  169. }
  170. return realpath($file);
  171. }
  172. /**
  173. * Filename of the resulting artifact relative to the root.
  174. *
  175. * If the query results in a set of artifacts (multiple nodes / array); then this string must contain an identifying
  176. * variable as returned by the writer.
  177. *
  178. * @param string $artifact
  179. *
  180. * @return void
  181. */
  182. public function setArtifact($artifact)
  183. {
  184. $this->artifact = $artifact;
  185. }
  186. /**
  187. * Returns the name of the artifact.
  188. *
  189. * @return string
  190. */
  191. public function getArtifact()
  192. {
  193. return $this->artifact;
  194. }
  195. /**
  196. * Sets an array of parameters (key => value).
  197. *
  198. * @param string[] $parameters
  199. *
  200. * @return void
  201. */
  202. public function setParameters(array $parameters)
  203. {
  204. $this->parameters = $parameters;
  205. }
  206. /**
  207. * Returns all parameters for this transformation.
  208. *
  209. * @return string[]
  210. */
  211. public function getParameters()
  212. {
  213. return $this->parameters;
  214. }
  215. /**
  216. * Returns a specific parameter, or $default if none exists.
  217. *
  218. * @param string $name
  219. * @param mixed $default
  220. *
  221. * @return string
  222. */
  223. public function getParameter($name, $default = null)
  224. {
  225. return isset($this->parameters[$name]) ? $this->parameters[$name] : $default;
  226. }
  227. /**
  228. * Executes the transformation.
  229. *
  230. * @param string $structure_file The location of the structure file.
  231. *
  232. * @return void
  233. */
  234. public function execute($structure_file)
  235. {
  236. $this->getWriter()->transform($structure_file, $this);
  237. }
  238. }