PageRenderTime 24ms CodeModel.GetById 32ms RepoModel.GetById 8ms app.codeStats 0ms

/htdocs/symfony/2.0.0rc4/vendor/assetic/src/Assetic/Filter/CssRewriteFilter.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 94 lines | 61 code | 14 blank | 19 comment | 19 complexity | 81ba3c55585ffcc3d35649648e2bc7df MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. * This file is part of the Assetic package, an OpenSky project.
  4. *
  5. * (c) 2010-2011 OpenSky Project Inc
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Assetic\Filter;
  11. use Assetic\Asset\AssetInterface;
  12. /**
  13. * Fixes relative CSS urls.
  14. *
  15. * @author Kris Wallsmith <kris.wallsmith@gmail.com>
  16. */
  17. class CssRewriteFilter extends BaseCssFilter
  18. {
  19. public function filterLoad(AssetInterface $asset)
  20. {
  21. }
  22. public function filterDump(AssetInterface $asset)
  23. {
  24. $sourceBase = $asset->getSourceRoot();
  25. $sourcePath = $asset->getSourcePath();
  26. $targetPath = $asset->getTargetPath();
  27. if (null === $sourcePath || null === $targetPath || $sourcePath == $targetPath) {
  28. return;
  29. }
  30. // learn how to get from the target back to the source
  31. if (false !== strpos($sourceBase, '://')) {
  32. list($scheme, $url) = explode('://', $sourceBase.'/'.$sourcePath, 2);
  33. list($host, $path) = explode('/', $url, 2);
  34. $host = $scheme.'://'.$host;
  35. $path = false === strpos($path, '/') ? '' : dirname($path);
  36. $path .= '/';
  37. } else {
  38. // assume source and target are on the same host
  39. $host = '';
  40. // pop entries off the target until it fits in the source
  41. if ('.' == dirname($sourcePath)) {
  42. $path = str_repeat('../', substr_count($targetPath, '/'));
  43. } elseif ('.' == $targetDir = dirname($targetPath)) {
  44. $path = dirname($sourcePath).'/';
  45. } else {
  46. $path = '';
  47. while (0 !== strpos($sourcePath, $targetDir)) {
  48. if (false !== $pos = strrpos($targetDir, '/')) {
  49. $targetDir = substr($targetDir, 0, $pos);
  50. $path .= '../';
  51. } else {
  52. $targetDir = '';
  53. $path .= '../';
  54. break;
  55. }
  56. }
  57. $path .= ltrim(substr(dirname($sourcePath).'/', strlen($targetDir)), '/');
  58. }
  59. }
  60. $content = $this->filterReferences($asset->getContent(), function($matches) use($host, $path)
  61. {
  62. if (false !== strpos($matches['url'], '://') || 0 === strpos($matches['url'], '//')) {
  63. // absolute or protocol-relative
  64. return $matches[0];
  65. }
  66. if ('/' == $matches['url'][0]) {
  67. // root relative
  68. return str_replace($matches['url'], $host.$matches['url'], $matches[0]);
  69. }
  70. // document relative
  71. $url = $matches['url'];
  72. while (0 === strpos($url, '../') && 2 <= substr_count($path, '/')) {
  73. $path = substr($path, 0, strrpos(rtrim($path, '/'), '/') + 1);
  74. $url = substr($url, 3);
  75. }
  76. return str_replace($matches['url'], $host.$path.$url, $matches[0]);
  77. });
  78. $asset->setContent($content);
  79. }
  80. }