Skip to content

Commit

Permalink
Always using Unix style paths for Windows compat.
Browse files Browse the repository at this point in the history
Closes aws#610.

This commit updates the JsonCompiler class to normalize paths to always
use "/" instead of using DIRECTORY_SEPARATOR. This works for all SDK
usage because the SDK uses relative paths for everything, and any
absolute paths that would have been mangled through this transformation
(e.g., 'C:/path/to/sdk/foo/bar.json.php') are modified to remove the base
directory of the SDK from the path (e.g., 'foo/bar.json.php' ->
'foo_bar.json.php).

This commit also removes a duplicate call to normalizePath().

Phar testing has been updated to include loading a paginator.

Phars are now built and tests on every Travis build.

Removing use of time command

Only building phar when using Guzzle 6
  • Loading branch information
mtdowling committed Jun 4, 2015
1 parent 0a6302b commit 25ddee9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 16 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ install:
- export AWS_SECRET_ACCESS_KEY=bar
- travis_retry composer update $COMPOSER_OPTS --no-interaction --prefer-source

script: make test
script:
- make test
- [ -z "$COMPOSER_OPTS" ] && make package
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ integ:

# Packages the phar and zip
package:
time php build/packager.php $(SERVICE)
php build/packager.php $(SERVICE)

guide:
cd docs && make html
Expand Down
6 changes: 5 additions & 1 deletion build/test-phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
'version' => 'latest'
];

new Aws\S3\S3Client($conf);
// Ensure that a client can be created.
$s3 = new Aws\S3\S3Client($conf);
// Ensure that waiters can be found.
$s3->getPaginator('ListObjects');

// Legacy factory instantiation.
Aws\DynamoDb\DynamoDbClient::factory($conf);

Expand Down
24 changes: 11 additions & 13 deletions src/JsonCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ class JsonCompiler
*/
public function __construct($useCache = true)
{
$this->stripPath = __DIR__ . DIRECTORY_SEPARATOR;
// Because the cache path is normalized to convert windows backslashes
// to unix style /, we must do that here as well to ensure that the
// relative SDK directory is trimmed from the generated cache file
// names.
$this->stripPath = str_replace('\\', '/', __DIR__ . DIRECTORY_SEPARATOR);
$this->useCache = $useCache && extension_loaded('Zend OPcache');
$this->hasOpcacheCheck = $this->useCache
&& function_exists('opcache_is_script_cached');
Expand Down Expand Up @@ -75,10 +79,9 @@ public function load($path)
return $this->loadJsonFromFile($path, $real);
}

$real = $this->normalize($path);
$cache = str_replace($this->stripPath, '', $real);
$cache = str_replace(['\\', '/'], '_', $cache);
$cache = $this->cacheDir . DIRECTORY_SEPARATOR . $cache . '.php';
$cache = "{$this->cacheDir}/{$cache}.php";

if (($this->hasOpcacheCheck && opcache_is_script_cached($cache))
|| file_exists($cache)
Expand All @@ -94,28 +97,23 @@ public function load($path)

/**
* Resolve relative paths without using realpath (which causes an
* unnecessary fstat).
* unnecessary fstat). And realpath does not work with phar files!
*
* @param $path
*
* @return string
*/
private function normalize($path)
{
static $replace = ['/', '\\'];
static $skip = ['' => true, '.' => true];

$isPhar = substr($path, 0, 7) === 'phar://';

if ($isPhar) {
$path = substr($path, 7);
}

$parts = explode(
DIRECTORY_SEPARATOR,
// Normalize path separators
str_replace($replace, DIRECTORY_SEPARATOR, $path)
);
// Normalize path separators
$parts = explode('/', str_replace('\\', '/', $path));

$segments = [];
foreach ($parts as $part) {
Expand All @@ -128,11 +126,11 @@ private function normalize($path)
}
}

$resolved = implode(DIRECTORY_SEPARATOR, $segments);
$resolved = implode('/', $segments);

// Add a leading slash if necessary.
if (isset($parts[0]) && $parts[0] === '') {
$resolved = DIRECTORY_SEPARATOR . $resolved;
$resolved = '/' . $resolved;
}

return $isPhar ? 'phar://' . $resolved : $resolved;
Expand Down
5 changes: 5 additions & 0 deletions tests/JsonCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testCanPurgeCachedFiles()
$c->load($this->models . '/endpoints.json');
$entries = array_diff(scandir($c->getCacheDir()), ['.', '..']);
$this->assertNotEmpty($entries);
$this->assertEquals(['data_endpoints.json.php'], array_values($entries));
$c->purge();
$entries = array_diff(scandir($c->getCacheDir()), ['.', '..']);
$this->assertEmpty($entries);
Expand All @@ -87,6 +88,10 @@ public function pathProvider()
// Relative with no leading slash
['foo/baz/../bar.qux', 'foo/bar.qux'],
['\\foo\\baz\\..\\.\\bar.qux', '/foo/bar.qux'],
// Phar path
['phar://foo.phar/foo/bar/../baz.qux', 'phar://foo.phar/foo/baz.qux'],
// Phar path with windows mixed in
['phar://foo.phar\\foo\\bar\\..\\baz.qux', 'phar://foo.phar/foo/baz.qux'],
];
}

Expand Down

0 comments on commit 25ddee9

Please sign in to comment.