Skip to content

Commit

Permalink
fixed a bug introduced by the relative urls fix
Browse files Browse the repository at this point in the history
- Fixed a bug with relative urls when index.php is included in the url
- Fixed a bug with asset caching causing recompilation on every request

Signed-off-by: Suhayb El Wardany <me@suw.me>
  • Loading branch information
suwardany committed Sep 24, 2014
1 parent 8032835 commit f5e2663
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
46 changes: 38 additions & 8 deletions src/Cartalyst/AsseticFilters/UriRewriteFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ class UriRewriteFilter implements FilterInterface {
protected $documentRoot;

/**
* Applications base url.
* Symfony request instance.
*
* @var string
*/
protected $baseUrl;
protected $request;

/**
* Root directory of the asset.
Expand All @@ -78,11 +78,8 @@ class UriRewriteFilter implements FilterInterface {
* @param array $symlinks
* @return void
*/
public function __construct($documentRoot = null, $symlinks = array(), $request = null)
public function __construct($documentRoot = null, $symlinks = array())
{
$request = $request ?: Request::createFromGlobals();

$this->baseUrl = $request->getBaseUrl();
$this->documentRoot = $this->realPath($documentRoot) ?: $_SERVER['DOCUMENT_ROOT'];
$this->symlinks = $symlinks;
}
Expand Down Expand Up @@ -172,6 +169,8 @@ protected function trimUrls($content)
*/
protected function processUriCallback($matches)
{
$scriptName = basename($this->getRequest()->getScriptName());

$isImport = $matches[0][0] === '@';

// Determine what the quote character and the URI is, if there is one.
Expand Down Expand Up @@ -200,6 +199,9 @@ protected function processUriCallback($matches)
}
}

// Strip off the scriptname
$uri = str_replace($scriptName.'/', '', $uri);

// Analyze the URI
if ($uri[0] !== '/' and strpos($uri, '//') === false and strpos($uri, 'data') !== 0)
{
Expand All @@ -222,6 +224,8 @@ protected function processUriCallback($matches)
*/
protected function rewriteRelative($uri)
{
$request = $this->getRequest();

$path = strtr($this->assetDirectory, '/', DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR.strtr($uri, '/', DIRECTORY_SEPARATOR);

foreach ($this->symlinks as $link => $target)
Expand All @@ -234,8 +238,13 @@ protected function rewriteRelative($uri)
}
}

// Strip the document root from the path.
$path = $this->baseUrl . substr($path, strlen($this->documentRoot));
// Prepend the base url to compile the correct paths
// for subdirectories and symlinked directories
$path = $request->getBaseUrl() . substr($path, strlen($this->documentRoot));
$scriptName = basename($request->getScriptName());

// Strip off the scriptname (index.php) if present
$path = str_replace($scriptName.'/', '', $path);

$uri = strtr($path, '/\\', '//');
$uri = $this->removeDots($uri);
Expand All @@ -262,4 +271,25 @@ protected function removeDots($uri)
return $uri;
}

/**
* Returns or creates a new symfony request.
*
* @return \Symfony\Component\HttpFoundation\Request
*/
public function getRequest()
{
return $this->request ?: $this->request = Request::createFromGlobals();
}

/**
* Sets the request instance.
*
* @param \Symfony\Component\HttpFoundation\Request
* @return void
*/
public function setRequest($request)
{
$this->request = $request;
}

}
16 changes: 6 additions & 10 deletions tests/UriRewriteFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ public function tearDown()

public function testUriRewrite()
{
$request = m::mock('Symfony\Component\HttpFoundation\Request');
$request->shouldReceive('getBaseUrl')->once();

$filter = new UriRewriteFilter('path/to/public', array(), $request);
$filter = new UriRewriteFilter('path/to/public', array());

$input = "body { background-image: url('../foo/bar.png'); }";

Expand All @@ -54,10 +51,7 @@ public function testUriRewrite()

public function testUriRewriteWithSymlinks()
{
$request = m::mock('Symfony\Component\HttpFoundation\Request');
$request->shouldReceive('getBaseUrl')->once();

$filter = new UriRewriteFilter('path/to/public', array('//assets' => strtr('path/to/outside/public/assets', '/', DIRECTORY_SEPARATOR)), $request);
$filter = new UriRewriteFilter('path/to/public', array('//assets' => strtr('path/to/outside/public/assets', '/', DIRECTORY_SEPARATOR)));

$input = "body { background-image: url('../foo/bar.png'); }";

Expand All @@ -73,8 +67,11 @@ public function testUriRewriteWithSymlinksAndSubDir()
{
$request = m::mock('Symfony\Component\HttpFoundation\Request');
$request->shouldReceive('getBaseUrl')->once()->andReturn('/test');
$request->shouldReceive('getScriptName')->twice()->andReturn('/index.php');

$filter = new UriRewriteFilter('path/to/public', array('//assets' => strtr('path/to/outside/public/assets', '/', DIRECTORY_SEPARATOR)));

$filter = new UriRewriteFilter('path/to/public', array('//assets' => strtr('path/to/outside/public/assets', '/', DIRECTORY_SEPARATOR)), $request);
$filter->setRequest($request);

$input = "body { background-image: url('../foo/bar.png'); }";

Expand All @@ -86,5 +83,4 @@ public function testUriRewriteWithSymlinksAndSubDir()
$this->assertEquals("body { background-image: url('/test/assets/foo/bar.png'); }", $asset->getContent());
}


}

0 comments on commit f5e2663

Please sign in to comment.