/lib/Scaffold.php

https://github.com/mathiasrw/Scaffold · PHP · 250 lines · 107 code · 26 blank · 117 comment · 6 complexity · 27a751021c6037a7df2c219aa7864c2a MD5 · raw file

  1. <?php
  2. /**
  3. * Scaffold
  4. * Handles for Scaffold_Source objects
  5. *
  6. * @package Scaffold
  7. * @author Anthony Short <anthonyshort@me.com>
  8. * @copyright 2009-2010 Anthony Short. All rights reserved.
  9. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  10. * @link https://github.com/anthonyshort/csscaffold/master
  11. */
  12. class Scaffold extends Scaffold_Extension_Observable
  13. {
  14. // =========================================
  15. // = Public Variables =
  16. // =========================================
  17. /**
  18. * Is Scaffold in production mode?
  19. * @access public
  20. * @var string
  21. */
  22. public $production;
  23. /**
  24. * Handles the caching of parsed sources
  25. * @access public
  26. * @var Scaffold_Cache
  27. */
  28. public $cache;
  29. /**
  30. * Responds to the browser
  31. * @access public
  32. * @var Scaffold_Reponse
  33. */
  34. public $response;
  35. /**
  36. * Helper classes
  37. * @access public
  38. * @var Scaffold_Helper
  39. */
  40. public $helper;
  41. // =========================================
  42. // = Protected Variables =
  43. // =========================================
  44. /**
  45. * HTTP output type
  46. * @var string
  47. */
  48. protected $_output_type = 'text/css';
  49. // =========================================
  50. // = Constructors & Initialization Methods =
  51. // =========================================
  52. /**
  53. * @access public
  54. * @param $cache Scaffold_Cache
  55. * @param $response Scaffold_Response
  56. * @param $loader Scaffold_Loader
  57. * @param $production boolean
  58. * @return void
  59. */
  60. public function __construct(Scaffold_Cache $cache, Scaffold_Response $response, $production = false)
  61. {
  62. $this->cache = $cache;
  63. $this->response = $response;
  64. $this->production = $production;
  65. }
  66. // ============================
  67. // = Public Methods =
  68. // ============================
  69. /**
  70. * Compiles the CSS using the engine and caches the result
  71. * @access public
  72. * @param $source Scaffold_Source
  73. * @return array
  74. */
  75. public function compile(Scaffold_Source $source)
  76. {
  77. # Hook before anything is done
  78. $this->notify('pre_compile',array($source,$this));
  79. # Try and load it from the cache
  80. $cached = $this->cache->get($source->id);
  81. # Can't load it from the cache, we're in dev mode, or the original file has changed
  82. if( $cached === false OR $this->production === false OR $source->last_modified > $cached->last_modified)
  83. {
  84. // Run it through the extensions
  85. $source = $this->parse($source);
  86. // Hook before saving it to the cache
  87. $this->notify('pre_cache',array($source,$this));
  88. // Save it to the cache
  89. $this->save($source);
  90. // Load it for reals this time
  91. $cached = $this->cache->get($source->id);
  92. }
  93. $source->contents = $cached->contents;
  94. $source->last_modified = $cached->last_modified;
  95. $source->expires = $cached->expires;
  96. return $source;
  97. }
  98. /**
  99. * Compiles the CSS using the engine and caches the result
  100. * @access public
  101. * @return array
  102. */
  103. public function getSources ( ) {
  104. $sources = array();
  105. if ( !empty($_GET['f']) ) {
  106. $files = explode($_GET['f']);
  107. unset($_GET['f']);
  108. foreach ( $files as $file ) {
  109. $sources[] = $this->getSource($file);
  110. }
  111. }
  112. else {
  113. $sources = array($this->getSource());
  114. }
  115. return $sources;
  116. }
  117. /**
  118. * Compiles the CSS using the engine and caches the result
  119. * @access public
  120. * @param string $file
  121. * @return Scaffold_Source
  122. */
  123. public function getSource ( $file = null, $config = array() ) {
  124. if ( $file || ($file = $_GET['f']) ) {
  125. if ( strstr($file, 'http') )
  126. $source = new Scaffold_Source_Url($file);
  127. else
  128. $source = new Scaffold_Source_File($this->helper->load->file($file));
  129. }
  130. elseif(isset($_GET['file']))
  131. {
  132. $source = new Scaffold_Source_File( $this->helper->load->file($_GET['file']) );
  133. }
  134. elseif(isset($_GET['url']) AND $config['enable_url'] === true)
  135. {
  136. $source = new Scaffold_Source_Url($_GET['url']);
  137. }
  138. elseif(isset($_GET['string']) AND $config['enable_string'] === true)
  139. {
  140. $source = new Scaffold_Source_String($_GET['string']);
  141. }
  142. elseif(isset($config['default_source']))
  143. {
  144. $source = new Scaffold_Source_File($config['default_source']);
  145. }
  146. else
  147. {
  148. throw new Exception('Could not detect source.');
  149. }
  150. return $source;
  151. }
  152. /**
  153. * Renders the contents of a file. In production mode, it will tell
  154. * the rendering engine to use the browsers cache if it's available.
  155. *
  156. * In development mode, the content will always be resent so that you
  157. * don't have to clear the cache every time you make a request.
  158. *
  159. * @access public
  160. * @param $source
  161. * @return void
  162. */
  163. public function render(Scaffold_Source $source)
  164. {
  165. $this->response->set(
  166. $source->contents,
  167. $source->last_modified,
  168. $this->_output_type
  169. );
  170. $this->notify('pre_render',array($this->response));
  171. $this->response->render($this->production);
  172. }
  173. /**
  174. * Parses a CSS string through each of the extensions.
  175. *
  176. * - Initialize: Used for loading libraries and preparing for processing
  177. * - Pre-format
  178. * - Pre-process: Any formatting or processing that needs to occur so that the process hook will go smooth
  179. * - Process: Any form of processing the CSS.
  180. * - Post-process: Cleaning up anything left behind from the process hook to make it valid CSS again.
  181. * - Post-Format: Used for formatting the CSS. Should be valid CSS by this point.
  182. *
  183. * As well as these, the extensions themselves can create hooks, so that
  184. * you can only run an extension during another extension.
  185. *
  186. * @param $source Scaffold_Source
  187. * @return Scaffold_Source
  188. */
  189. public function parse(Scaffold_Source $source)
  190. {
  191. $params = array($source,$this);
  192. $this->notify('initialize',$params);
  193. $this->notify('pre_format',$params);
  194. $this->notify('pre_process',$params);
  195. $this->notify('process',$params);
  196. $this->notify('post_process',$params);
  197. $this->notify('post_format',$params);
  198. return $source;
  199. }
  200. /**
  201. * Saves the contents of the source object
  202. * @access public
  203. * @param $source Scaffold_Source
  204. * @return void
  205. */
  206. public function save(Scaffold_Source $source)
  207. {
  208. $this->cache->set(
  209. $source->id,
  210. $source->contents,
  211. $source->last_modified
  212. );
  213. }
  214. /**
  215. * Adds a helper object
  216. * @param Scaffold_Helper
  217. * @access public
  218. * @return void
  219. */
  220. public function attach_helper(Scaffold_Helper $helper)
  221. {
  222. $this->helper = $helper;
  223. }
  224. }