JsMinTask.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * $Id: JsMinTask.php 905 2010-10-05 16:28:03Z mrook $
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information please see
  19. * <http://phing.info>.
  20. */
  21. require_once 'phing/Task.php';
  22. require_once 'phing/tasks/ext/jsmin/JsMin.php';
  23. /**
  24. * Task to minify javascript files.
  25. *
  26. * Requires JSMin which can be found at http://code.google.com/p/jsmin-php/ but
  27. * is bundled with Phing so no additional install of JsMin is required.
  28. *
  29. * @author Frank Kleine <mikey@stubbles.net>
  30. * @version $Id: JsMinTask.php 905 2010-10-05 16:28:03Z mrook $
  31. * @package phing.tasks.ext
  32. * @since 2.3.0
  33. */
  34. class JsMinTask extends Task
  35. {
  36. /**
  37. * the source files
  38. *
  39. * @var FileSet
  40. */
  41. protected $filesets = array();
  42. /**
  43. * Whether the build should fail, if
  44. * errors occured
  45. *
  46. * @var boolean
  47. */
  48. protected $failonerror = false;
  49. /**
  50. * directory to put minified javascript files into
  51. *
  52. * @var string
  53. */
  54. protected $targetDir;
  55. /**
  56. * Nested creator, adds a set of files (nested fileset attribute).
  57. */
  58. public function createFileSet()
  59. {
  60. $num = array_push($this->filesets, new FileSet());
  61. return $this->filesets[$num - 1];
  62. }
  63. /**
  64. * Whether the build should fail, if an error occured.
  65. *
  66. * @param boolean $value
  67. */
  68. public function setFailonerror($value)
  69. {
  70. $this->failonerror = $value;
  71. }
  72. /**
  73. * sets the directory where minified javascript files should be put inot
  74. *
  75. * @param string $targetDir
  76. */
  77. public function setTargetDir($targetDir)
  78. {
  79. $this->targetDir = $targetDir;
  80. }
  81. /**
  82. * The init method: Do init steps.
  83. */
  84. public function init()
  85. {
  86. return true;
  87. }
  88. /**
  89. * The main entry point method.
  90. */
  91. public function main()
  92. {
  93. foreach ($this->filesets as $fs) {
  94. try {
  95. $files = $fs->getDirectoryScanner($this->project)->getIncludedFiles();
  96. $fullPath = realpath($fs->getDir($this->project));
  97. foreach ($files as $file) {
  98. $this->log('Minifying file ' . $file);
  99. try {
  100. $target = $this->targetDir . '/' . str_replace($fullPath, '', str_replace('.js', '-min.js', $file));
  101. if (file_exists(dirname($target)) === false) {
  102. mkdir(dirname($target), 0700, true);
  103. }
  104. file_put_contents($target, JSMin::minify(file_get_contents($fullPath . '/' . $file)));
  105. } catch (JSMinException $jsme) {
  106. $this->log("Could not minify file $file: " . $jsme->getMessage(), Project::MSG_ERR);
  107. }
  108. }
  109. } catch (BuildException $be) {
  110. // directory doesn't exist or is not readable
  111. if ($this->failonerror) {
  112. throw $be;
  113. } else {
  114. $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_WARN);
  115. }
  116. }
  117. }
  118. }
  119. }
  120. ?>