Write a class that uses YUI Compressor to create a compressed version of all local javascript referenced in a page, excluding inline javascript.
## Deliverables
This class is designed to create efficient cached / compressed versions of local, non inline javascript on a page. Files are identified through a regex on the page HTML and compressed into either [login to view URL] or page_name.min.js. Sitewide are all those files that appear on many pages and can reasonably be expected to be cached on the user's computer, while [login to view URL] files contain the remaining js specific to the currently viewed page.
1: Extract all #<script ([a-z/'=\"]+) src([^>]+)></script># references for local javascript in string $html_page, EXCEPT those matching filenames in array $exclude_files and those specified in array $this->sitewide_files
2: Join the files found in step 1 into a single temporary file; Join the files in $this->sitewide into a second temporary file
3: Run that file through YUICompressor to create a compressed version at $output_path
4: Remove all matches in $html_page from step #1 above and replace with a single reference to the new compressed file at string $position
In addition:
- If the cached version exists and younger than $max_age, ignore steps 2 & 3
- $this->sitewide_files would be an array of files that are used on most pages through the site
Vars:
int $max_age - max age before recaching required
bool $force_new_cache - normally false, set to true for debugging and to override caching
string $output_path - e.g. '/my/directory/to/write/'
array $sitewide - array of filenames to include in the [login to view URL] file (e.g. "[login to view URL]","[login to view URL]")
string $html_page - either full string of rendered HTML OR a URL to fetch with CURL (fopen sucks)
string $position - 'head' inserts references to new js files just before closing head tag, 'body' just before closing body tag, default 'head'
Methods:
function identifyScripts()
/**
* @param string $path Optional directory path to write joined files to, otherwise just write in $this->temp_directory
*/
function joinScritps($path)
/**
* @param string $path Optional directory path to write compressed files to, otherwise write in $this->output_path
*/
compressScripts($path)
/**
* @return string $filename If compressing sitewide files, return [login to view URL], otherwise return something like str_replace("/","_",$_SERVER[REQUEST_URI]).".min"."js" - some unique filename that is auto generated from the current page (note - if a URL was passed to identifyScripts, generate filename from the URI in that rather than current URI)
*/
compressedFilename()
/**
* @param string $file full path to file to check
* @return bool If file exists and doesn't need recreating (i.e. is younger than $this->max_age, return true
*/
function isCached($file)
/**
* @param string $html_page - see above
*/
function compress($html_page)
That's pretty much it, thus:
$comp = new CompressJavascript();
$comp->html_page= ..
$comp->compress()
will do the business...
Any questions, let me know. The above might not be the best way to do this, if you have another, let me know. The goal is to give it a page, suck out all the local js, compress it and return a page with references to the new compressed files to send to the browser.