PageRenderTime 47ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/core/lib/PatternLab/Generator.php

https://gitlab.com/daveson/lacitypatterns
PHP | 174 lines | 89 code | 44 blank | 41 comment | 24 complexity | e8694a16ba7ece527c7cd397381276a7 MD5 | raw file
  1. <?php
  2. /*!
  3. * Pattern Lab Generator Class - v0.7.12
  4. *
  5. * Copyright (c) 2013-2014 Dave Olsen, http://dmolsen.com
  6. * Licensed under the MIT license
  7. *
  8. * Compiles and moves all files in the source/patterns dir to public/patterns dir ONCE.
  9. * Vast majority of logic is in builder.lib.php
  10. *
  11. */
  12. namespace PatternLab;
  13. class Generator extends Builder {
  14. /**
  15. * Use the Builder __construct to gather the config variables
  16. */
  17. public function __construct($config = array()) {
  18. // construct the parent
  19. parent::__construct($config);
  20. }
  21. /**
  22. * Pulls together a bunch of functions from builder.lib.php in an order that makes sense
  23. * @param {Boolean} decide if CSS should be parsed and saved. performance hog.
  24. * @param {Boolean} decide if static files like CSS and JS should be moved
  25. */
  26. public function generate($enableCSS = false, $moveStatic = true, $noCacheBuster = false) {
  27. $timePL = true; // track how long it takes to generate a PL site
  28. if ($timePL) {
  29. $mtime = microtime();
  30. $mtime = explode(" ",$mtime);
  31. $mtime = $mtime[1] + $mtime[0];
  32. $starttime = $mtime;
  33. }
  34. $this->noCacheBuster = $noCacheBuster;
  35. if ($enableCSS) {
  36. // enable CSS globally throughout PL
  37. $this->enableCSS = true;
  38. // initialize CSS rule saver
  39. $this->initializeCSSRuleSaver();
  40. print "CSS generation enabled. This could take a few seconds...\n";
  41. }
  42. // gather up all of the data to be used in patterns
  43. $this->gatherData();
  44. // gather all of the various pattern info
  45. $this->gatherPatternInfo();
  46. // clean the public directory to remove old files
  47. if (($this->cleanPublic == "true") && $moveStatic) {
  48. $this->cleanPublic();
  49. }
  50. // render out the patterns and move them to public/patterns
  51. $this->generatePatterns();
  52. // render out the index and style guide
  53. $this->generateMainPages();
  54. // make sure data exists
  55. if (!is_dir($this->pd."/data")) {
  56. mkdir($this->pd."/data");
  57. }
  58. // iterate over the data files and regenerate the entire site if they've changed
  59. $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->sd."/_data/"), \RecursiveIteratorIterator::SELF_FIRST);
  60. // make sure dots are skipped
  61. $objects->setFlags(\FilesystemIterator::SKIP_DOTS);
  62. foreach($objects as $name => $object) {
  63. $fileName = str_replace($this->sd."/_data".DIRECTORY_SEPARATOR,"",$name);
  64. if (($fileName[0] != "_") && $object->isFile()) {
  65. $this->moveStaticFile("_data/".$fileName,"","_data","data");
  66. }
  67. }
  68. // move all of the files unless pattern only is set
  69. if ($moveStatic) {
  70. // iterate over all of the other files in the source directory
  71. $objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->sd."/"), \RecursiveIteratorIterator::SELF_FIRST);
  72. // make sure dots are skipped
  73. $objects->setFlags(\FilesystemIterator::SKIP_DOTS);
  74. foreach($objects as $name => $object) {
  75. // clean-up the file name and make sure it's not one of the pattern lab files or to be ignored
  76. $fileName = str_replace($this->sd.DIRECTORY_SEPARATOR,"",$name);
  77. if (($fileName[0] != "_") && (!in_array($object->getExtension(),$this->ie)) && (!in_array($object->getFilename(),$this->id))) {
  78. // catch directories that have the ignored dir in their path
  79. $ignoreDir = $this->ignoreDir($fileName);
  80. // check to see if it's a new directory
  81. if (!$ignoreDir && $object->isDir() && !is_dir($this->pd."/".$fileName)) {
  82. mkdir($this->pd."/".$fileName);
  83. }
  84. // check to see if it's a new file or a file that has changed
  85. if (!$ignoreDir && $object->isFile() && (!file_exists($this->pd."/".$fileName))) {
  86. $this->moveStaticFile($fileName);
  87. }
  88. }
  89. }
  90. }
  91. // update the change time so the auto-reload will fire (doesn't work for the index and style guide)
  92. $this->updateChangeTime();
  93. print "your site has been generated...\n";
  94. // print out how long it took to generate the site
  95. if ($timePL) {
  96. $mtime = microtime();
  97. $mtime = explode(" ",$mtime);
  98. $mtime = $mtime[1] + $mtime[0];
  99. $endtime = $mtime;
  100. $totaltime = ($endtime - $starttime);
  101. $mem = round((memory_get_peak_usage(true)/1024)/1024,2);
  102. print "site generation took ".$totaltime." seconds and used ".$mem."MB of memory...\n";
  103. }
  104. }
  105. /**
  106. * Randomly prints a saying after the generate is complete
  107. */
  108. public function printSaying() {
  109. $randomNumber = rand(0,60);
  110. $sayings = array(
  111. "have fun storming the castle",
  112. "be well, do good work, and keep in touch",
  113. "may the sun shine, all day long",
  114. "smile",
  115. "namaste",
  116. "walk as if you are kissing the earth with your feet",
  117. "to be beautiful means to be yourself",
  118. "i was thinking of the immortal words of socrates, who said \"...i drank what?\"",
  119. "let me take this moment to compliment you on your fashion sense, particularly your slippers",
  120. "42",
  121. "he who controls the spice controls the universe",
  122. "the greatest thing you'll ever learn is just to love and be loved in return",
  123. "nice wand",
  124. "i don't have time for a grudge match with every poseur in a parka"
  125. );
  126. if (isset($sayings[$randomNumber])) {
  127. print $sayings[$randomNumber]."...\n";
  128. }
  129. }
  130. }