No Description
php
caching
psr-6
redis
memcached
apcu

RoboFile.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php declare(strict_types=1);
  2. if ( ! function_exists('glob_recursive'))
  3. {
  4. // Does not support flag GLOB_BRACE
  5. function glob_recursive($pattern, $flags = 0)
  6. {
  7. $files = glob($pattern, $flags);
  8. foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir)
  9. {
  10. $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
  11. }
  12. return $files;
  13. }
  14. }
  15. /**
  16. * This is project's console commands configuration for Robo task runner.
  17. *
  18. * @see http://robo.li/
  19. */
  20. class RoboFile extends \Robo\Tasks {
  21. /**
  22. * Directories used by analysis tools
  23. *
  24. * @var array
  25. */
  26. protected $taskDirs = [
  27. 'build/logs',
  28. 'build/pdepend',
  29. 'build/phpdox',
  30. ];
  31. /**
  32. * Directories to remove with the clean task
  33. *
  34. * @var array
  35. */
  36. protected $cleanDirs = [
  37. 'coverage',
  38. 'docs',
  39. 'phpdoc',
  40. 'build/logs',
  41. 'build/phpdox',
  42. 'build/pdepend'
  43. ];
  44. /**
  45. * Do static analysis tasks
  46. */
  47. public function analyze()
  48. {
  49. $this->prepare();
  50. $this->lint();
  51. $this->phploc(TRUE);
  52. $this->phpcs(TRUE);
  53. $this->dependencyReport();
  54. $this->phpcpdReport();
  55. }
  56. /**
  57. * Run all tests, generate coverage, generate docs, generate code statistics
  58. */
  59. public function build()
  60. {
  61. $this->analyze();
  62. $this->coverage();
  63. $this->docs();
  64. }
  65. /**
  66. * Cleanup temporary files
  67. */
  68. public function clean()
  69. {
  70. $cleanFiles = [
  71. 'build/humbug.json',
  72. 'build/humbug-log.txt',
  73. ];
  74. array_map(function ($file) {
  75. @unlink($file);
  76. }, $cleanFiles);
  77. // So the task doesn't complain,
  78. // make any 'missing' dirs to cleanup
  79. array_map(function ($dir) {
  80. if ( ! is_dir($dir))
  81. {
  82. `mkdir -p {$dir}`;
  83. }
  84. }, $this->cleanDirs);
  85. $this->_cleanDir($this->cleanDirs);
  86. $this->_deleteDir($this->cleanDirs);
  87. }
  88. /**
  89. * Run unit tests and generate coverage reports
  90. */
  91. public function coverage()
  92. {
  93. $this->taskPhpUnit()
  94. ->configFile('build/phpunit.xml')
  95. ->printed(true)
  96. ->run();
  97. }
  98. /**
  99. * Generate documentation with phpdox
  100. */
  101. public function docs()
  102. {
  103. $cmd_parts = [
  104. 'cd build',
  105. '../vendor/bin/phpdox',
  106. 'cd ..'
  107. ];
  108. $this->_run($cmd_parts, ' && ');
  109. }
  110. /**
  111. * Verify that source files are valid
  112. */
  113. public function lint()
  114. {
  115. $files = $this->getAllSourceFiles();
  116. $chunks = array_chunk($files, 6);
  117. foreach($chunks as $chunk)
  118. {
  119. $this->parallelLint($chunk);
  120. }
  121. }
  122. /**
  123. * Run mutation tests with humbug
  124. *
  125. * @param bool $stats - if true, generates stats rather than running mutation tests
  126. */
  127. public function mutate($stats = FALSE)
  128. {
  129. $test_parts = [
  130. 'vendor/bin/humbug'
  131. ];
  132. $stat_parts = [
  133. 'vendor/bin/humbug',
  134. '--skip-killed=yes',
  135. '-v',
  136. './build/humbug.json'
  137. ];
  138. $cmd_parts = ($stats) ? $stat_parts : $test_parts;
  139. $this->_run($cmd_parts);
  140. }
  141. /**
  142. * Run the phpcs tool
  143. *
  144. * @param bool $report - if true, generates reports instead of direct output
  145. */
  146. public function phpcs($report = FALSE)
  147. {
  148. $report_cmd_parts = [
  149. 'vendor/bin/phpcs',
  150. '--standard=./build/phpcs.xml',
  151. '--report-checkstyle=./build/logs/phpcs.xml',
  152. ];
  153. $normal_cmd_parts = [
  154. 'vendor/bin/phpcs',
  155. '--standard=./build/phpcs.xml',
  156. ];
  157. $cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
  158. $this->_run($cmd_parts);
  159. }
  160. /**
  161. * Run the phploc tool
  162. *
  163. * @param bool $report - if true, generates reports instead of direct output
  164. */
  165. public function phploc($report = FALSE)
  166. {
  167. // Command for generating reports
  168. $report_cmd_parts = [
  169. 'vendor/bin/phploc',
  170. '--count-tests',
  171. '--log-csv=build/logs/phploc.csv',
  172. '--log-xml=build/logs/phploc.xml',
  173. 'src',
  174. 'tests'
  175. ];
  176. // Command for generating direct output
  177. $normal_cmd_parts = [
  178. 'vendor/bin/phploc',
  179. '--count-tests',
  180. 'src',
  181. 'tests'
  182. ];
  183. $cmd_parts = ($report) ? $report_cmd_parts : $normal_cmd_parts;
  184. $this->_run($cmd_parts);
  185. }
  186. /**
  187. * Create temporary directories
  188. */
  189. public function prepare()
  190. {
  191. array_map([$this, '_mkdir'], $this->taskDirs);
  192. }
  193. /**
  194. * Lint php files and run unit tests
  195. */
  196. public function test()
  197. {
  198. $this->lint();
  199. $this->taskPHPUnit()
  200. ->configFile('phpunit.xml')
  201. ->printed(true)
  202. ->run();
  203. }
  204. /**
  205. * Watches for file updates, and automatically runs appropriate actions
  206. */
  207. public function watch()
  208. {
  209. $this->taskWatch()
  210. ->monitor('composer.json', function() {
  211. $this->taskComposerUpdate()->run();
  212. })
  213. ->monitor('src', function () {
  214. $this->taskExec('test')->run();
  215. })
  216. ->monitor('tests', function () {
  217. $this->taskExec('test')->run();
  218. })
  219. ->run();
  220. }
  221. /**
  222. * Create pdepend reports
  223. */
  224. protected function dependencyReport()
  225. {
  226. $cmd_parts = [
  227. 'vendor/bin/pdepend',
  228. '--jdepend-xml=build/logs/jdepend.xml',
  229. '--jdepend-chart=build/pdepend/dependencies.svg',
  230. '--overview-pyramid=build/pdepend/overview-pyramid.svg',
  231. 'src'
  232. ];
  233. $this->_run($cmd_parts);
  234. }
  235. /**
  236. * Get the total list of source files, including tests
  237. *
  238. * @return array
  239. */
  240. protected function getAllSourceFiles()
  241. {
  242. $files = array_merge(
  243. glob_recursive('build/*.php'),
  244. glob_recursive('src/*.php'),
  245. glob_recursive('tests/*.php'),
  246. glob('*.php')
  247. );
  248. sort($files);
  249. return $files;
  250. }
  251. /**
  252. * Run php's linter in one parallel task for the passed chunk
  253. *
  254. * @param array $chunk
  255. */
  256. protected function parallelLint(array $chunk)
  257. {
  258. $task = $this->taskParallelExec()
  259. ->timeout(5)
  260. ->printed(FALSE);
  261. foreach($chunk as $file)
  262. {
  263. $task = $task->process("php -l {$file}");
  264. }
  265. $task->run();
  266. }
  267. /**
  268. * Generate copy paste detector report
  269. */
  270. protected function phpcpdReport()
  271. {
  272. $cmd_parts = [
  273. 'vendor/bin/phpcpd',
  274. '--log-pmd build/logs/pmd-cpd.xml',
  275. 'src'
  276. ];
  277. $this->_run($cmd_parts);
  278. }
  279. /**
  280. * Shortcut for joining an array of command arguments
  281. * and then running it
  282. *
  283. * @param array $cmd_parts - command arguments
  284. * @param string $join_on - what to join the command arguments with
  285. */
  286. protected function _run(array $cmd_parts, $join_on = ' ')
  287. {
  288. $this->taskExec(implode($join_on, $cmd_parts))->run();
  289. }
  290. }