Skip to content

Commit d4e1b63

Browse files
committed
leave paths with no matching file as original
1 parent 4489af9 commit d4e1b63

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

src/Resources/AbstractDispatchableResource.php

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -70,40 +70,6 @@ protected function _minify() { }
7070

7171
protected function _dispatch() { }
7272

73-
/**
74-
* Dispatch a nested URL
75-
*
76-
* @param $uri
77-
*
78-
* @return string
79-
* @throws \Exception
80-
*/
81-
protected function _dispatchNestedUrl($uri)
82-
{
83-
$quote = $uri[1];
84-
$path = $uri[2];
85-
86-
list($path, $append) = Strings::explode('?', $path, [$path, null], 2);
87-
88-
if(!$this->_manager->isExternalUrl($path))
89-
{
90-
$path = Path::system($this->_makeFullPath(dirname($path), dirname($this->_path)), basename($path));
91-
}
92-
93-
$url = $this->_manager->getResourceUri($path);
94-
95-
if(empty($url))
96-
{
97-
return $quote . ($path ?? $uri[0]) . $quote;
98-
}
99-
100-
if(!empty($append))
101-
{
102-
return $quote . "$url?$append" . $quote;
103-
}
104-
return $quote . "$url" . $quote;
105-
}
106-
10773
/**
10874
* Make the relative path
10975
*
@@ -134,6 +100,33 @@ protected function _makeFullPath($relativePath, $workingDirectory)
134100
return $workingDirectory[0] !== '.' ? Path::url($workingDirectory, $relativePath) : $relativePath;
135101
}
136102

103+
/**
104+
* @param $path
105+
*
106+
* @return string
107+
* @throws \Exception
108+
*/
109+
protected function _getDispatchUrl($path): string
110+
{
111+
list($newPath, $append) = Strings::explode('?', $path, [$path, null], 2);
112+
113+
if(!$this->_manager->isExternalUrl($newPath))
114+
{
115+
$newPath = Path::system($this->_makeFullPath(dirname($newPath), dirname($this->_path)), basename($newPath));
116+
}
117+
118+
$url = $this->_manager->getResourceUri($newPath);
119+
if(empty($url))
120+
{
121+
return $path;
122+
}
123+
if(!empty($append))
124+
{
125+
$url .= '?' . $append;
126+
}
127+
return $url;
128+
}
129+
137130
/**
138131
* Get the content for this resource
139132
*

src/Resources/CssResource.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Packaged\Dispatch\Resources;
33

4+
use Packaged\Helpers\Strings;
5+
46
class CssResource extends AbstractDispatchableResource
57
{
68
protected $_options = [
@@ -20,9 +22,25 @@ public function getContentType()
2022

2123
protected function _dispatch()
2224
{
23-
$regex = '~(?<=url\()\s*(["\']?)(.*?)\1\s*(?=\))~';
2425
//Find all URL(.*) and dispatch their values
25-
$this->_content = preg_replace_callback($regex, [$this, "_dispatchNestedUrl"], $this->_content);
26+
$this->_content = preg_replace_callback(
27+
'~(?<=url\()\s*(["\']?)(.*?)\1\s*(?=\))~',
28+
[$this, "_dispatchUrlPaths"],
29+
$this->_content
30+
);
31+
}
32+
33+
/**
34+
* Dispatch a nested URL
35+
*
36+
* @param $uri
37+
*
38+
* @return string
39+
* @throws \Exception
40+
*/
41+
protected function _dispatchUrlPaths($uri)
42+
{
43+
return Strings::wrap($this->_getDispatchUrl($uri[2]), $uri[1], true);
2644
}
2745

2846
protected function _minify()

tests/Resources/AbstractDispatchableResourceTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ public function testProcessesContent()
2626
$this->assertContains('url(\'r/d41d8cd9/css/css.jpg\')', $content);
2727
$this->assertContains('url("r/d41d8cd9/css/sub/subimg.jpg")', $content);
2828
$this->assertContains('url(\'http://www.example.com/background.jpg\')', $content);
29-
$this->assertContains('url(img/missing-file.jpg)', $content);
29+
$this->assertContains('url(../img/missing-file.jpg)', $content);
30+
$this->assertContains('url(../../../img/missing-file.jpg)', $content);
3031

3132
$resource->setProcessingPath('css/do-not-modify.css');
3233
$resource->setContent(file_get_contents(Path::system($root, Dispatch::RESOURCES_DIR, 'css', 'do-not-modify.css')));

0 commit comments

Comments
 (0)