Guest User

Untitled

a guest
Jan 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.37 KB | None | 0 0
  1. <?php
  2.  
  3. require_once 'phing/Task.php';
  4. require_once 'phing/util/FileUtils.php';
  5. require_once 'System.php';
  6. require_once "phing/tasks/ext/markdown/markdown.php";
  7.  
  8. class TemplateTask extends Task {
  9.  
  10. /**
  11. * @var string Taskname for logger
  12. */
  13. protected $taskName = 'Template';
  14.  
  15. /**
  16. * Input file in markdown format.
  17. * Required
  18. *
  19. * @var string
  20. */
  21. protected $file = null;
  22.  
  23. /**
  24. * Output file or directory. May be omitted.
  25. * When it ends with a slash, it is considered to be a directory
  26. *
  27. * @var string
  28. */
  29. protected $destination = null;
  30.  
  31. /**
  32. * templatefiles directory. required.
  33. *
  34. * @var string
  35. */
  36. protected $templatesDir;
  37.  
  38. /**
  39. * default template content. required.
  40. *
  41. * @var string
  42. */
  43. protected $template_content;
  44.  
  45. /**
  46. * default template placeholders. at least one required.
  47. *
  48. * @var array
  49. */
  50. protected $template_placeholders = array();
  51.  
  52. protected $filesets = array(); // all fileset objects assigned to this task
  53. protected $mapperElement = null;
  54.  
  55. /**
  56. * all filterchains objects assigned to this task
  57. *
  58. * @var array
  59. */
  60. protected $filterChains = array();
  61.  
  62. /**
  63. * mode to create directories with
  64. *
  65. * @var integer
  66. */
  67. protected $mode = 0755;
  68.  
  69. /**
  70. * Only render files whole source files are newer than the
  71. * target files
  72. *
  73. * @var boolean
  74. */
  75. protected $uptodate = false;
  76.  
  77. /**
  78. * The main entry point method.
  79. *
  80. * @return void
  81. */
  82. public function main()
  83. {
  84. if( ! $this->templatesDir){
  85. throw new BuildException('The templates directory is required');
  86. }
  87.  
  88. $this->setTemplatePlaceholders('default');
  89.  
  90. if (count($this->filterChains)) {
  91. $this->fileUtils = new FileUtils();
  92. }
  93.  
  94. if ($this->file != '') {
  95. $file = $this->file;
  96. $targetFile = $this->getTargetFile($file, $this->destination);
  97. $this->render($file, $targetFile);
  98. return;
  99. }
  100.  
  101. if (!count($this->filesets)) {
  102. throw new BuildException(
  103. '"file" attribute or "fileset" subtag required'
  104. );
  105. }
  106.  
  107. // process filesets
  108. $mapper = null;
  109. if ($this->mapperElement !== null) {
  110. $mapper = $this->mapperElement->getImplementation();
  111. }
  112.  
  113. $project = $this->getProject();
  114. foreach ($this->filesets as $fs) {
  115. $ds = $fs->getDirectoryScanner($project);
  116. $fromDir = $fs->getDir($project);
  117. $srcFiles = $ds->getIncludedFiles();
  118.  
  119. foreach ($srcFiles as $src) {
  120. $file = new PhingFile($fromDir, $src);
  121. if ($mapper !== null) {
  122. $results = $mapper->main($file);
  123. if ($results === null) {
  124. throw new BuildException(
  125. sprintf(
  126. 'No filename mapper found for "%s"',
  127. $file
  128. )
  129. );
  130. }
  131. $targetFile = reset($results);
  132. } else {
  133. $targetFile = $this->getTargetFile($file, $this->destination);
  134. }
  135. $this->render($file, $targetFile);
  136. }
  137. }
  138. }
  139.  
  140. /*
  141. *
  142. */
  143. protected function setTemplatePlaceholders($templateDir)
  144. {
  145. if($templateDir == 'default'){
  146. if( ! file_exists($this->templatesDir.DIRECTORY_SEPARATOR.'template.html')){
  147. throw new BuildException('A template.html file must in the templates directory');
  148. }
  149.  
  150. $this->template_content = file_get_contents($this->templatesDir.DIRECTORY_SEPARATOR.'template.html');
  151.  
  152. preg_match_all('/\[[A-Z]+\]/',$this->template_content,$placeholders);
  153.  
  154. if( ! $placeholders){
  155. throw new BuildException('At least a [CONTENT] placeholder is required');
  156. }
  157.  
  158. $this->template_placeholders = $placeholders[0];
  159.  
  160. return true;
  161. }else{
  162.  
  163. }
  164. }
  165.  
  166. /**
  167. * Renders a single file and applies filters on it
  168. *
  169. * @param string $tool conversion tool to use
  170. * @param string $source markdown source file
  171. * @param string $targetFile target file name
  172. *
  173. * @return void
  174. */
  175. protected function render($source, $targetFile)
  176. {
  177. if (count($this->filterChains) == 0) {
  178. return $this->renderFile($source, $targetFile);
  179. }
  180.  
  181. $tmpTarget = tempnam(sys_get_temp_dir(), 'tmp-');
  182. $this->renderFile($source, $tmpTarget);
  183.  
  184. $this->fileUtils->copyFile(
  185. new PhingFile($tmpTarget),
  186. new PhingFile($targetFile),
  187. true, false, $this->filterChains,
  188. $this->getProject(), $this->mode
  189. );
  190. unlink($tmpTarget);
  191. }
  192.  
  193. /**
  194. * Renders a single file with the markdown tool.
  195. *
  196. * @param string $tool conversion tool to use
  197. * @param string $source markdown source file
  198. * @param string $targetFile target file name
  199. *
  200. * @return void
  201. *
  202. * @throws BuildException When the conversion fails
  203. */
  204. protected function renderFile($source, $targetFile)
  205. {
  206. if ($this->uptodate && file_exists($targetFile)
  207. && filemtime($source) <= filemtime($targetFile)
  208. ) {
  209. //target is up to date
  210. return;
  211. }
  212. //work around a bug in php by replacing /./ with /
  213. $targetDir = str_replace('/./', '/', dirname($targetFile));
  214. if (!is_dir($targetDir)) {
  215. mkdir($targetDir, $this->mode, true);
  216. }
  217.  
  218. $arOutput = $this->wrapTemplate($source);
  219.  
  220. $retval = file_put_contents($targetFile,$arOutput);
  221.  
  222. if ( ! $retval) {
  223. $this->log('File not rendered.', Project::MSG_INFO);
  224. throw new BuildException('Rendering template failed');
  225. }
  226. $this->log('File rendered.', Project::MSG_DEBUG);
  227. }
  228.  
  229. protected function wrapTemplate($sourcefile)
  230. {
  231. $template_content = $this->template_content;
  232. $template_placeholders = $this->template_placeholders;
  233.  
  234. if(strpos($sourcefile,DIRECTORY_SEPARATOR) !== false){
  235.  
  236. }
  237.  
  238. $placeholders = array_fill_keys($template_placeholders,'');
  239. $placeholders['[CONTENT]'] = file_get_contents($sourcefile);
  240.  
  241. return strtr($template_content,$placeholders);
  242. }
  243.  
  244. /**
  245. * Determines and returns the target file name from the
  246. * input file and the configured destination name.
  247. *
  248. * @param string $file Input file
  249. * @param string $destination Destination file or directory name,
  250. * may be null
  251. *
  252. * @return string Target file name
  253. *
  254. * @uses $format
  255. * @uses $targetExt
  256. */
  257. public function getTargetFile($file, $destination = null)
  258. {
  259. if ($destination != ''
  260. && substr($destination, -1) !== '/'
  261. && substr($destination, -1) !== '\\'
  262. ) {
  263. return $destination;
  264. }
  265.  
  266. return $destination . $file ;
  267. }
  268.  
  269. /**
  270. * The setter for the attribute "templatesdir"
  271. *
  272. * @param string $templatesDir directory of the templatefiles
  273. *
  274. * @return void
  275. */
  276. public function setTemplatesDir($templatesDir)
  277. {
  278. $this->templatesDir = $templatesDir;
  279. $this->template = $templatesDir.'/template.html';
  280. }
  281.  
  282. /**
  283. * The setter for the attribute "uptodate"
  284. *
  285. * @param string $uptodate True/false
  286. *
  287. * @return void
  288. */
  289. public function setUptodate($uptodate)
  290. {
  291. $this->uptodate = (boolean)$uptodate;
  292. }
  293.  
  294. /**
  295. * Nested creator, creates a FileSet for this task
  296. *
  297. * @return object The created fileset object
  298. */
  299. public function createFileSet()
  300. {
  301. $num = array_push($this->filesets, new FileSet());
  302. return $this->filesets[$num-1];
  303. }
  304.  
  305. /**
  306. * Nested creator, creates one Mapper for this task
  307. *
  308. * @return Mapper The created Mapper type object
  309. *
  310. * @throws BuildException
  311. */
  312. public function createMapper()
  313. {
  314. if ($this->mapperElement !== null) {
  315. throw new BuildException(
  316. 'Cannot define more than one mapper', $this->location
  317. );
  318. }
  319. $this->mapperElement = new Mapper($this->project);
  320. return $this->mapperElement;
  321. }
  322.  
  323. /**
  324. * Creates a filterchain, stores and returns it
  325. *
  326. * @return FilterChain The created filterchain object
  327. */
  328. public function createFilterChain()
  329. {
  330. $num = array_push($this->filterChains, new FilterChain($this->project));
  331. return $this->filterChains[$num-1];
  332. }
  333. }
Add Comment
Please, Sign In to add comment