diff --git a/src/Adapter/AbstractAdapter.php b/src/Adapter/AbstractAdapter.php index 29fade2..4fc91bd 100644 --- a/src/Adapter/AbstractAdapter.php +++ b/src/Adapter/AbstractAdapter.php @@ -16,6 +16,7 @@ use Alchemy\Zippy\Archive\ArchiveInterface; use Alchemy\Zippy\Exception\InvalidArgumentException; use Alchemy\Zippy\Resource\ResourceManager; +use Alchemy\Zippy\Resource\PathUtil; use Alchemy\Zippy\Adapter\VersionProbe\VersionProbeInterface; use Alchemy\Zippy\Exception\RuntimeException; use Alchemy\Zippy\Adapter\Resource\ResourceInterface; @@ -246,6 +247,6 @@ private function makeTargetAbsolute($path) throw new InvalidArgumentException(sprintf('Target path %s is not writeable.', $directory)); } - return realpath($directory).'/'.basename ($path); + return realpath($directory).'/'.PathUtil::basename($path); } } diff --git a/src/Resource/PathUtil.php b/src/Resource/PathUtil.php new file mode 100644 index 0000000..ec7a46a --- /dev/null +++ b/src/Resource/PathUtil.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Alchemy\Zippy\Resource; + +abstract class PathUtil +{ + public static function basename($path) + { + return (false === $pos = strrpos(strtr($path, '\\', '/'), '/')) ? $path : substr($path, $pos + 1); + } +} \ No newline at end of file diff --git a/src/Resource/Resource.php b/src/Resource/Resource.php index 11b0ae4..be0257d 100644 --- a/src/Resource/Resource.php +++ b/src/Resource/Resource.php @@ -87,7 +87,7 @@ public function getContextForProcessInSinglePlace() return null; } - if (basename($this->original) === $this->target) { + if (PathUtil::basename($this->original) === $this->target) { return dirname($this->original); } } diff --git a/src/Resource/TargetLocator.php b/src/Resource/TargetLocator.php index ed8e2df..ba86f88 100644 --- a/src/Resource/TargetLocator.php +++ b/src/Resource/TargetLocator.php @@ -60,7 +60,7 @@ private function locateResource($resource) throw new TargetLocatorException($resource, 'Unable to retrieve path from resource'); } - return basename($data['path']); + return PathUtil::basename($data['path']); } /** @@ -86,7 +86,7 @@ private function locateString($context, $resource) return $this->getRelativePathFromContext($url['path'], $context); } - return basename($resource); + return PathUtil::basename($resource); } // resource is a local path @@ -95,7 +95,7 @@ private function locateString($context, $resource) return $this->getRelativePathFromContext($resource, $context); } else { - return basename($resource); + return PathUtil::basename($resource); } } diff --git a/tests/Tests/Resource/PathUtilTest.php b/tests/Tests/Resource/PathUtilTest.php new file mode 100644 index 0000000..0f79a97 --- /dev/null +++ b/tests/Tests/Resource/PathUtilTest.php @@ -0,0 +1,38 @@ +assertEquals($expected, PathUtil::basename($context)); + } + + public function providePathData() + { + return array( + array('file.ext', 'input/path/to/local/file.ext'), + array('file.ext', 'input\path\to\local\file.ext'), + array('file.ext', '\file.ext'), + array('file.ext', 'file.ext'), + array('Ängelholm.jpg', '/tmp/Ängelholm.jpg'), + array('Ängelholm.jpg', '\tmp\Ängelholm.jpg'), + array('Ängelholm.jpg', '\Ängelholm.jpg'), + array('Ängelholm.jpg', 'Ängelholm.jpg'), + array('я-utf8-name.jpg', '/tmp/я-utf8-name.jpg'), + array('я-utf8-name.jpg', '\tmp\я-utf8-name.jpg'), + array('я-utf8-name.jpg', 'я-utf8-name.jpg'), + array('я-utf8-name.jpg', '/я-utf8-name.jpg'), + array('logo.png', 'http://google.com/tmp/logo.png'), + array('Ängelholm.png', 'http://google.com/city/Ängelholm.png'), + array('Ängelholm.png', 'http://google.com/я/Ängelholm.png') + ); + } +} diff --git a/tests/Tests/TestCase.php b/tests/Tests/TestCase.php index d1f012f..3c39637 100644 --- a/tests/Tests/TestCase.php +++ b/tests/Tests/TestCase.php @@ -3,6 +3,7 @@ namespace Alchemy\Zippy\Tests; use Alchemy\Zippy\Adapter\AdapterInterface; +use Alchemy\Zippy\Resource\PathUtil; use Alchemy\Zippy\Resource\ResourceCollection; use Alchemy\Zippy\Resource\Resource; use Alchemy\Zippy\Adapter\VersionProbe\VersionProbeInterface; @@ -120,6 +121,6 @@ protected function getExpectedAbsolutePathForTarget($target) throw new \InvalidArgumentException(sprintf('Unable to get the absolute path for %s', $target)); } - return realpath($directory).'/'.basename($target); + return realpath($directory).'/'.PathUtil::basename($target); } }