PageRenderTime 75ms CodeModel.GetById 20ms RepoModel.GetById 29ms app.codeStats 0ms

/application/library/Thirdpart/Minify/docs/CustomSource.wiki.md

https://gitlab.com/flyhope/Hiblog
Markdown | 136 lines | 109 code | 27 blank | 0 comment | 0 complexity | 93ceeaf29340e7f7233e39e4840319bb MD5 | raw file
  1. In `groupsConfig.php`, usually you specify source file paths using strings like `/path/to/file.js` or `//js/file1.js` (Minify expands this to `"{$_SERVER['DOCUMENT_ROOT']}/js/file1.js"` ).
  2. Instead of a string, you may substitute an instance of class `Minify_Source`. This allows you to customize how minification is applied, and/or pull content from a non-file location (e.g. a URL).
  3. ### Example: filepath
  4. In the `$spec` array, set the key `filepath` to produce a source based on a file path:
  5. ```php
  6. $src1 = new Minify_Source(array(
  7. 'filepath' => '//js/file1.js',
  8. ));
  9. $src2 = new Minify_Source(array(
  10. 'filepath' => '//js/file2.js',
  11. ));
  12. return [
  13. 'js' => [$src1, $src2]
  14. ];
  15. ```
  16. Note the above is functionally identical to:
  17. ```php
  18. return [
  19. 'js' => ['//js/file1.js', '//js/file2.js'],
  20. ];
  21. ```
  22. ### Example: Specify a different minifier or none at all
  23. To change minifier, set `minifier` to a [callback](http://php.net/manual/en/language.pseudo-types.php)`*` or the empty string (for none):
  24. **`*`Prepare for `groupsConfig.php` to be executed more than once.** (This is likely if you're using the functions in `/min/utils.php`.) In practice this just means making sure functions are conditionally defined if they don't already exist, etc.
  25. ```php
  26. $src1 = new Minify_Source(array(
  27. 'filepath' => '//js/file1.js',
  28. 'minifier' => 'myJsMinifier',
  29. ));
  30. $src2 = new Minify_Source(array(
  31. 'filepath' => '//js/file2.js',
  32. 'minifier' => '', // don't compress
  33. ));
  34. ```
  35. In the above, `JmyJsMinifier()` is only called when the contents of `$src1` is needed.
  36. **`*`Do _not_ use `create_function()` or anonymous functions for the minifier.** The internal names of these function tend to vary, causing endless cache misses, killing performance and filling cache storage up.
  37. ## Non-File Sources
  38. You're not limited to flat js/css files, but without `filepath`, the `$spec` array must contain these keys:
  39. * **`id `** a unique string id for this source. (e.g. `'my source'`)
  40. * **`getContentFunc `** a [callback](http://php.net/manual/en/language.pseudo-types.php) that returns the content. The function is only called when the cache is rebuilt.
  41. * **`contentType `** `Minify::TYPE_JS` or `Minify::TYPE_CSS`
  42. * **`lastModified `** a timestamp indicating when the content last changed. (If you can't determine this quickly, you can "fake" it using a step function, causing the cache to be periodically rebuilt.)
  43. ### Example: Content from a URL
  44. Here we want to fetch javascript from a URL. We don't know when it will change, so we use a stepping expression to re-fetch it every midnight:
  45. ```php
  46. if (! function_exists('src1_fetch')) {
  47. function src1_fetch() {
  48. return file_get_contents('http://example.org/javascript.php');
  49. }
  50. }
  51. $src1 = new Minify_Source([
  52. 'id' => 'source1',
  53. 'getContentFunc' => 'src1_fetch',
  54. 'contentType' => Minify::TYPE_JS,
  55. 'lastModified' => ($_SERVER['REQUEST_TIME'] - $_SERVER['REQUEST_TIME'] % 86400),
  56. ]);
  57. ```
  58. If you know that the URL content only depends on a few local files, you can use the maximum of their `mtime`s as the `lastModified` key:
  59. ```php
  60. $src1 = new Minify_Source([
  61. 'id' => 'source1',
  62. 'getContentFunc' => 'src1_fetch',
  63. 'contentType' => Minify::TYPE_JS,
  64. 'lastModified' => max(
  65. filemtime('/path/to/javascript.php')
  66. ,filemtime('/path/to/javascript_input.css')
  67. ),
  68. ]);
  69. ```
  70. ## Performance Considerations
  71. Be aware that all the code you put in `groupsConfig.php` will be evaluated upon every request like `/min/g=...`, so make it as light as possible.
  72. If you wish to keep `groupsConfig.php` "clean", you can alternately create a separate PHP script that manually sets up sources, caching, options, and calls Minify::serve().
  73. ```php
  74. // myServer.php
  75. /**
  76. * This script implements a Minify server for a single set of sources.
  77. * If you don't want '.php' in the URL, use mod_rewrite...
  78. */
  79. require __DIR__ . '/vendor/autoload.php';
  80. // setup Minify
  81. $cache = new Minify_Cache_File();
  82. $minify = new Minify($cache);
  83. $env = new Minify_Env();
  84. $sourceFactory = new Minify_Source_Factory($env, [], $cache);
  85. $controller = new Minify_Controller_Files($env, $sourceFactory);
  86. function src1_fetch() {
  87. return file_get_contents('http://example.org/javascript.php');
  88. }
  89. // setup sources
  90. $sources = [];
  91. $sources[] = new Minify_Source([
  92. 'id' => 'source1',
  93. 'getContentFunc' => 'src1_fetch',
  94. 'contentType' => Minify::TYPE_JS,
  95. 'lastModified' => max(
  96. filemtime('/path/to/javascript.php'),
  97. filemtime('/path/to/javascript_input.js')
  98. ),
  99. ]);
  100. $sources[] = '//file2.js';
  101. $sources[] = '//file3.js';
  102. // setup serve and controller options
  103. $options = [
  104. 'files' => $sources,
  105. 'maxAge' => 86400,
  106. ];
  107. // handle request
  108. $minify->serve($controller, $options);
  109. ```