ContentNegotiator updates
January 13th, 2009
I’ve revisited a class I wrote last year and made a few updates. You can find the new version on my Snippets page.
Updates on 1/13/2009
- ContentNegotiator is now a singleton. You may only instantiate one instance of the class.
- Constructor no longer kicks off output buffering. I decided this was a “side-effect” and should be moved to a separate method.
- prolog() method no longer emits content-type headers. The prolog() method now echoes only the XML prolog and DOCTYPE.
- sendHeaders() added to emit content-type headers to the browser.
- negotiate() added to begin output buffering for source code negotiation.
- close() method added to flush output buffering. I found some issues with relying on PHP to close buffering at the end of the page execution. It is better to close the buffer manually at the end of the page.
Updates on 1/14/2009
- Broke up the constructor a little bit more.
- Chooses a document type better. It now falls down the big if statement before content type is selected and again if it needs to be changed. This fixes a bug where it sometimes picked a slightly different document type then it guessed it would.
- The $version and $flavor properties now reflect the actual document type chosen. ContentNegotiator does not use these after it picks the document type, but at least they’re there in case someone wants to dump them.
- Implemented a way to specify the fallback document type. If you pass the first parameter as an array, it will pick the first index if the browser accepts it and fallback on the second. Using this it is now possible to send IE XHTML 1.1 even though IE does not accept the XHTML content type.
Use: $contneg->getInstance(array(‘XHTML 1.1′, ‘XHTML 1.1′));