From a5f9ecd3282251b49c510edc4c8fbd8445f26251 Mon Sep 17 00:00:00 2001 From: Jonas Thelemann Date: Sat, 25 Jun 2016 04:03:11 +0200 Subject: [PATCH] Created Custom Server (markdown) --- Custom-Server.md | 153 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 Custom-Server.md diff --git a/Custom-Server.md b/Custom-Server.md new file mode 100644 index 0000000..38dc10b --- /dev/null +++ b/Custom-Server.md @@ -0,0 +1,153 @@ +Please see the UserGuide if you're just getting started with Minify. This guide is for more advanced users who wish to implement an HTTP server in PHP using the [Minify](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/Minify.php) class. + +The basic steps are: + + 1. Set up the include\_path + 1. Set up caching + 1. Choose a Minify controller + 1. Set up service and controller options + 1. Handle the request + +## Set up the include\_path + +``` +set_include_path('/path/to/min/lib' . PATH_SEPARATOR . get_include_path()); +``` + +## Set up caching + +Minify ships with [cache classes](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/Minify/Cache/) for files, APC, and memcache. Choose one, instantiate it, and pass it to Minify: +``` +require 'Minify.php'; +require 'Minify/Cache/File.php'; + +Minify::setCache(new Minify_Cache_File()); // files in directory chosen by Solar_Dir +``` + +## Choose a Minify controller + +Minify uses controller classes to analyze HTTP requests and determine which sources will make up the response. (The content isn't always returned; if the browser's cache is valid, Minify can return a 304 header instead). + +The [Files](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/Minify/Controller/Files.php#9) controller doesn't care about the HTTP request. It just specifies a given array of sources (file paths or [source objects](CustomSource.md)). + +The [Groups](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/Minify/Controller/Groups.php) controller uses `$_SERVER['PATH_INFO']` to choose from an array of source lists. There's an example at the end of this page. + +The Files controller is simple, so we'll use it here. + +## Set up service and controller options + +A single array is used for configuration, and is passed to the `Minify::serve` method. `serve` passes it to the controller, which picks off the keys it needs and returns it to `serve` for Minify's own built-in [options](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/Minify.php#88). This architecture allows the controller to set defaults for `serve`. + +The Files controller only needs one key: `files`: the array of sources to be combined and served. + +The only `serve` option we'll set is `maxAge` (the default is a measly 1800 seconds). + +``` +$options = array( + // options for the controller + 'files' => array('//js/file1.js', '//js/file2.js', $src), + // options for Minify::serve + 'maxAge' => 86400 +); +``` +(In the above $src is a [Minify\_Source object](CustomSource.md), which allows you to serve non-file content, and/or apply settings to individual sources.) + +## Handle the request + +``` +Minify::serve('Files', $options); +``` + +That's it... + +Minify's default application (sometimes referred to as "min") is implemented this way, in the file [min/index.php](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/index.php). Most of its request handling is encapsulated in its own [MinApp](http://code.google.com/p/minify/source/browse/tags/release_2.1.3/min/lib/Minify/Controller/MinApp.php#15) controller. + +# The Request Cycle + +In handling a request, `Minify::serve` does a number of operations: + + 1. creates a controller object, unless you pass it a readymade object (let's call this `$ctrl`). + 1. calls `$ctrl->setupSources($options)`, which analyzes the request and sets `$ctrl->sources` accordingly to an array of Minify\_Source objects. + 1. calls `$ctrl->analyzeSources($options)`, which set `$options` keys for `contentType` and `lastModifiedTime`, based on the sources. + 1. calls `$ctrl->mixInDefaultOptions($options)`, to mix the controller's default options with the user's. + 1. determines if the browser accepts gzip + 1. validates the browser cache (optionally responding with a 304) + 1. validates the server cache. If it needs refreshing, `Minify::_combineMinify` is called, which... + * calls `$source->getContent` on each source + * the content is combined before or after minification (depending on individual source options) + 1. sets up headers to be sent + 1. either returns the headers and content in an array (if `quiet` is set), or sends it to the browser. + +# Using the Groups controller + +``` +$options = array( + 'groups' => array( + 'js' => array('//js/file1.js', '//js/file2.js'), + 'css' => array('//css/file1.css', '//css/file2.css'), + ), +); +Minify::serve('Groups', $options); +``` + +With the above, if you request `http://example.org/myServer.php/css`, Apache will set `$_SERVER['PATH_INFO'] = '/css'` and the sources in `$options['groups']['css']` will be served. + + \ No newline at end of file