readme.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /*
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. require 'src/Carbon/Carbon.php';
  11. use Carbon\Carbon;
  12. date_default_timezone_set('America/Toronto');
  13. $readme = file_get_contents('readme.src.md');
  14. $pre_src = 'use Carbon\Carbon; ';
  15. // {{intro::exec(echo Carbon::now()->subMinutes(2)->diffForHumans();)}}
  16. preg_match_all('@{{(\w*)::(\w+)\((.+)\)}}@sU', $readme, $matches, PREG_SET_ORDER);
  17. foreach ($matches as $match) {
  18. list($orig, $name, $cmd, $src) = $match;
  19. $src = trim($src, "\n\r");
  20. ob_start();
  21. $result = eval($pre_src . $src);
  22. $ob = ob_get_clean();
  23. if ($result === false) {
  24. echo "Failed lint check.". PHP_EOL . PHP_EOL;
  25. $error = error_get_last();
  26. if ($error != null) {
  27. echo $error['message'] . ' on line ' . $error['line'] . PHP_EOL . PHP_EOL;
  28. }
  29. echo "---- eval'd source ---- " . PHP_EOL . PHP_EOL;
  30. $i = 1;
  31. foreach (preg_split("/$[\n\r]^/m", $src) as $ln) {
  32. printf('%3s : %s%s', $i++, $ln, PHP_EOL);
  33. }
  34. exit(1);
  35. }
  36. // remove the extra newline from a var_dump
  37. if (strpos($src, 'var_dump(') === 0) {
  38. $ob = trim($ob);
  39. }
  40. // Add any necessary padding to lineup comments
  41. if (preg_match('@/\*pad\(([0-9]+)\)\*/@', $src, $matches)) {
  42. $src = preg_replace('@/\*pad\(([0-9]+)\)\*/@', '', $src);
  43. $src = str_pad($src, intval($matches[1]));
  44. }
  45. // Inject the source code
  46. $readme = str_replace($orig, $src, $readme);
  47. // Inject the eval'd result
  48. if ($cmd == 'exec') {
  49. $readme = str_replace('{{'.$name.'_eval}}', $ob, $readme);
  50. }
  51. }
  52. // allow for escaping a command
  53. $readme = str_replace('\{\{', '{{', $readme);
  54. file_put_contents('readme.md', $readme);