Skip to content

Windows fixes #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
language: php

php:
- '7.1'
- '7.2'
- '7.3'
- nightly

matrix:
include:
# linux tests
- php: '7.1'
- php: '7.2'
- php: '7.3'
- php: nightly
# windows tests
# https://travis-ci.community/t/where-to-contribute-php-support-for-windows/304
- os: windows
language: sh
before_install:
- export PATH="/c/tools/composer:/c/tools/php73:$PATH"
- echo "$PATH"
- choco install php --version 7.3.5
- php -i
- cat /c/tools/php73/php.ini
#- choco install make
- ls /c/tools/php73
- ls /c/tools/php73/ext
#- ls /c/tools/composer
- wget https://raw.githubusercontent.com/composer/getcomposer.org/76a7060ccb93902cd7576b67264ad91c8a2700e2/web/installer -O - -q | php -dextension=/c/tools/php73/ext/php_openssl.dll --
- ls
install: php -dextension=/c/tools/php73/ext/php_openssl.dll -dextension=/c/tools/php73/ext/php_mbstring.dll composer.phar install --prefer-dist --no-interaction
script: php -dextension=/c/tools/php73/ext/php_openssl.dll -dextension=/c/tools/php73/ext/php_mbstring.dll vendor/phpunit/phpunit/phpunit

# allow php nightly to fail until https://travis-ci.community/t/php-nightly-now-requires-oniguruma/2237 is fixed
allow_failures:
- php: nightly
Expand Down
30 changes: 27 additions & 3 deletions src/ReferenceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private function normalizeUri($uri)
return "file://$uri";
}
if (stripos(PHP_OS, 'WIN') === 0 && strncmp(substr($uri, 1), ':\\', 2) === 0) {
return "file://$uri";
return "file:///" . strtr($uri, [' ' => '%20', '\\' => '/']);
}
throw new UnresolvableReferenceException('Can not resolve references for a specification given as a relative path.');
}
Expand Down Expand Up @@ -88,9 +88,14 @@ public function resolveRelativeUri(string $uri): string
// absolute path
return 'file://' . $parts['path'];
}
// convert absolute path on windows to a file:// URI. This is probably incomplete but should work with the majority of paths.
if (stripos(PHP_OS, 'WIN') === 0 && strncmp(substr($uri, 1), ':\\', 2) === 0) {
return "file:///" . strtr($uri, [' ' => '%20', '\\' => '/']);
}

if (isset($parts['path'])) {
// relative path
return dirname($baseUri) . '/' . $parts['path'];
return $this->dirname($baseUri) . '/' . $parts['path'];
}

throw new UnresolvableReferenceException("Invalid URI: '$uri'");
Expand All @@ -109,10 +114,29 @@ public function resolveRelativeUri(string $uri): string
if (isset($parts['path'][0]) && $parts['path'][0] === '/') {
$absoluteUri .= $parts['path'];
} elseif (isset($parts['path'])) {
$absoluteUri .= rtrim(dirname($baseParts['path'] ?? ''), '/') . '/' . $parts['path'];
$absoluteUri .= rtrim($this->dirname($baseParts['path'] ?? ''), '/') . '/' . $parts['path'];
}
return $absoluteUri
. (isset($parts['query']) ? '?' . $parts['query'] : '')
. (isset($parts['fragment']) ? '#' . $parts['fragment'] : '');
}

/**
* Returns parent directory's path.
* This method is similar to `dirname()` except that it will treat
* both \ and / as directory separators, independent of the operating system.
*
* @param string $path A path string.
* @return string the parent directory's path.
* @see http://www.php.net/manual/en/function.dirname.php
* @see https://github.com/yiisoft/yii2/blob/e1f6761dfd9eba1ff1260cd37b04936aaa4959b5/framework/helpers/BaseStringHelper.php#L75-L92
*/
private function dirname($path)
{
$pos = mb_strrpos(str_replace('\\', '/', $path), '/');
if ($pos !== false) {
return mb_substr($path, 0, $pos);
}
return '';
}
}
7 changes: 6 additions & 1 deletion tests/spec/ReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,13 @@ public function testResolveCyclicReferenceInDocument()
public function testResolveFile()
{
$file = __DIR__ . '/data/reference/base.yaml';
if (stripos(PHP_OS, 'WIN') === 0) {
$yaml = str_replace('##ABSOLUTEPATH##', 'file:///' . strtr(dirname($file), [' ' => '%20', '\\' => '/']), file_get_contents($file));
} else {
$yaml = str_replace('##ABSOLUTEPATH##', 'file://' . dirname($file), file_get_contents($file));
}
/** @var $openapi OpenApi */
$openapi = Reader::readFromYaml(str_replace('##ABSOLUTEPATH##', 'file://' . dirname($file), file_get_contents($file)));
$openapi = Reader::readFromYaml($yaml);

$result = $openapi->validate();
$this->assertEquals([], $openapi->getErrors());
Expand Down