/vendor/jasonlewis/basset/src/Basset/Server.php

https://bitbucket.org/larryg/powerhut · PHP · 237 lines · 106 code · 31 blank · 100 comment · 6 complexity · 6d8dbfaff19a248025b8fa04c200e1af MD5 · raw file

  1. <?php namespace Basset;
  2. use Basset\Collection;
  3. use Basset\Manifest\Entry;
  4. use Basset\Manifest\Manifest;
  5. class Server {
  6. /**
  7. * Laravel application instance.
  8. *
  9. * @var \Illuminate\Foundation\Application
  10. */
  11. protected $collections;
  12. /**
  13. * Create a new output server instance.
  14. *
  15. * @param \Illuminate\Foundation\Application
  16. * @return void
  17. */
  18. public function __construct($app)
  19. {
  20. $this->app = $app;
  21. }
  22. /**
  23. * Serve a collection where the group is determined by the the extension.
  24. *
  25. * @param string $collection
  26. * @param string $format
  27. * @return string
  28. */
  29. public function collection($collection, $format = null)
  30. {
  31. list($collection, $extension) = preg_split('/\.(css|js)/', $collection, 2, PREG_SPLIT_DELIM_CAPTURE);
  32. return $this->serve($collection, $extension == 'css' ? 'stylesheets' : 'javascripts', $format);
  33. }
  34. /**
  35. * Serve the stylesheets for a given collection.
  36. *
  37. * @param string $collection
  38. * @param string $format
  39. * @return string
  40. */
  41. public function stylesheets($collection, $format = null)
  42. {
  43. return $this->serve($collection, 'stylesheets', $format);
  44. }
  45. /**
  46. * Serve the javascripts for a given collection.
  47. *
  48. * @param string $collection
  49. * @param string $format
  50. * @return string
  51. */
  52. public function javascripts($collection, $format = null)
  53. {
  54. return $this->serve($collection, 'javascripts', $format);
  55. }
  56. /**
  57. * Serve a given group for a collection.
  58. *
  59. * @param string $collection
  60. * @param string $group
  61. * @param string $format
  62. * @return string
  63. */
  64. public function serve($collection, $group, $format = null)
  65. {
  66. if ( ! isset($this->app['basset'][$collection]))
  67. {
  68. return '<!-- Basset could not find collection: '.$collection.' -->';
  69. }
  70. // Get the collection instance from the array of collections. This instance will be used
  71. // throughout the building process to fetch assets and compare against the stored
  72. // manfiest of fingerprints.
  73. $collection = $this->app['basset'][$collection];
  74. if ($entry = $this->app['basset.manifest']->get($collection))
  75. {
  76. if ($this->runningInProduction() and $entry->hasProductionFingerprint($group))
  77. {
  78. $response = $this->serveProductionCollection($collection, $entry, $group, $format);
  79. }
  80. else
  81. {
  82. $response = $this->serveDevelopmentCollection($collection, $entry, $group, $format);
  83. }
  84. return array_to_newlines($response);
  85. }
  86. else
  87. {
  88. return '<!-- Basset could not find manifest entry for collection: '.$collection->getIdentifer().' -->';
  89. }
  90. }
  91. /**
  92. * Serve a production collection.
  93. *
  94. * @param \Basset\Collection $collection
  95. * @param \Basset\Manifest\Entry $entry
  96. * @param string $group
  97. * @param string $format
  98. * @return array
  99. */
  100. protected function serveProductionCollection(Collection $collection, Entry $entry, $group, $format)
  101. {
  102. $fingerprint = $entry->getProductionFingerprint($group);
  103. $response = $this->serveExcludedAssets($collection, $group, $format);
  104. return array_merge($response, array($this->{'create'.studly_case($group).'Element'}($this->prefixBuildPath($fingerprint), $format)));
  105. }
  106. /**
  107. * Serve a development collection.
  108. *
  109. * @param \Basset\Collection $collection
  110. * @param \Basset\Manifest\Entry $entry
  111. * @param string $group
  112. * @param string $format
  113. * @return array
  114. */
  115. protected function serveDevelopmentCollection(Collection $collection, Entry $entry, $group, $format)
  116. {
  117. $responses = array();
  118. $identifier = $collection->getIdentifier();
  119. foreach ($collection->getAssetsWithExcluded($group) as $asset)
  120. {
  121. if ($asset->isIncluded() and $path = $entry->getDevelopmentAsset($asset))
  122. {
  123. $path = $this->prefixBuildPath($identifier.'/'.$path);
  124. }
  125. else
  126. {
  127. $path = $asset->getRelativePath();
  128. }
  129. $responses[] = $this->{'create'.studly_case($group).'Element'}($path, $format);
  130. }
  131. return $responses;
  132. }
  133. /**
  134. * Serve a collections excluded assets.
  135. *
  136. * @param \Basset\Collection $collection
  137. * @param string $group
  138. * @param string $format
  139. * @return array
  140. */
  141. protected function serveExcludedAssets(Collection $collection, $group, $format)
  142. {
  143. $responses = array();
  144. foreach ($collection->getAssetsOnlyExcluded($group) as $asset)
  145. {
  146. $path = $asset->getRelativePath();
  147. $responses[] = $this->{'create'.studly_case($group).'Element'}($path, $format);
  148. }
  149. return $responses;
  150. }
  151. /**
  152. * Prefix the build path to a given path.
  153. *
  154. * @param string $path
  155. * @return string
  156. */
  157. protected function prefixBuildPath($path)
  158. {
  159. if ($buildPath = $this->app['config']->get('basset::build_path'))
  160. {
  161. $path = "{$buildPath}/{$path}";
  162. }
  163. return $path;
  164. }
  165. /**
  166. * Determine if the application is running in production mode.
  167. *
  168. * @return bool
  169. */
  170. protected function runningInProduction()
  171. {
  172. return in_array($this->app['env'], (array) $this->app['config']->get('basset::production'));
  173. }
  174. /**
  175. * Create a stylesheets element for the specified path.
  176. *
  177. * @param string $path
  178. * @param string $format
  179. * @return string
  180. */
  181. protected function createStylesheetsElement($path, $format)
  182. {
  183. return sprintf($format ?: '<link rel="stylesheet" type="text/css" href="%s" />', $this->buildAssetUrl($path));
  184. }
  185. /**
  186. * Create a javascripts element for the specified path.
  187. *
  188. * @param string $path
  189. * @param string $format
  190. * @return string
  191. */
  192. protected function createJavascriptsElement($path, $format)
  193. {
  194. return sprintf($format ?: '<script src="%s"></script>', $this->buildAssetUrl($path));
  195. }
  196. /**
  197. * Build the URL to an asset.
  198. *
  199. * @param string $path
  200. * @return string
  201. */
  202. public function buildAssetUrl($path)
  203. {
  204. return starts_with($path, '//') ? $path : $this->app['url']->asset($path);
  205. }
  206. }