Permalink
Browse files

fixes windows paths

  • Loading branch information...
jaapio committed Dec 16, 2017
1 parent 76e5ba1 commit 4b36a14d79b764a26e8455a6791a584267fe3e1f
@@ -44,6 +44,8 @@
/** @var string[] */
private $parameters = [];
const WINDOWS_DSN = '~(^((?<scheme>file):\\/\\/)?(?<path>((?:[a-z]|[A-Z]):(?=\\\\(?![\\0-\\37<>:"/\\\\|?*])|\\/(?![\\0-\\37<>:"/\\\\|?*])|$)|^\\\\(?=[\\\\\\/][^\\0-\\37<>:"/\\\\|?*]+)|^(?=(\\\\|\\/)$)|^\\.(?=(\\\\|\\/)$)|^\\.\\.(?=(\\\\|\\/)$)|^(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+)|^\\.(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+)|^\\.\\.(?=(\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+))((\\\\|\\/)[^\\0-\\37<>:"/\\\\|?*]+|(\\\\|\\/)$)*()))$~';
/**
* Initializes the Dsn
*
@@ -163,12 +165,16 @@ private function parse($dsn)
unset($dsnParts[0]);
$locationParts = parse_url($location);
if (! isset($locationParts['scheme'])) {
if ($locationParts === false || (array_key_exists('scheme', $locationParts) && \strlen($locationParts['scheme']) === 1)) {
preg_match(static::WINDOWS_DSN, $dsn, $locationParts);
}
if (! array_key_exists('scheme', $locationParts) || ($locationParts['scheme'] === '' && array_key_exists('path',$locationParts)) ) {
$locationParts['scheme'] = 'file';
$location = 'file://' . $location;
}
if (! filter_var($location, FILTER_VALIDATE_URL)) {
if (!filter_var($location, FILTER_VALIDATE_URL) && !preg_match(static::WINDOWS_DSN, $location)) {
throw new \InvalidArgumentException(
sprintf('"%s" is not a valid DSN.', $dsn)
);
@@ -213,15 +219,26 @@ private function parseDsn($location, array $dsnParts)
*/
private function parseScheme(array $locationParts)
{
$validSchemes = ['file', 'git+http', 'git+https'];
if (! in_array(strtolower($locationParts['scheme']), $validSchemes)) {
if (! $this->isValidScheme($locationParts['scheme'])) {
throw new \InvalidArgumentException(
sprintf('"%s" is not a valid scheme.', $locationParts['scheme'])
);
}
$this->scheme = strtolower($locationParts['scheme']);
}
/**
* Validated provided scheme.
*
* @param string $scheme
* @return bool
*/
private function isValidScheme(string $scheme): bool
{
$validSchemes = ['file', 'git+http', 'git+https'];
return \in_array(\strtolower($scheme), $validSchemes, true);
}
/**
* Validates and sets the host and path properties
*
@@ -126,6 +126,51 @@ public function testValidDsnWithoutScheme()
$this->assertEquals('src', $fixture->getPath());
}
/**
* @covers ::__construct
* @covers ::__toString
* @covers ::getScheme
* @covers ::getHost
* @covers ::getPort
* @covers ::getPath
* @covers ::<private>
* @uses \phpDocumentor\DomainModel\Path
*/
public function testValidWindowsDsnWithoutScheme()
{
$dsn = "C:\\phpdocumentor\\tests\\unit\\phpDocumentor\\Infrastructure\\Parser";
$fixture = new Dsn($dsn);
$this->assertEquals('file://C:\\phpdocumentor\\tests\\unit\\phpDocumentor\\Infrastructure\\Parser', (string)$fixture);
$this->assertEquals('file', $fixture->getscheme());
$this->assertEquals(null, $fixture->getHost());
$this->assertEquals(0, $fixture->getPort());
$this->assertEquals('C:\\phpdocumentor\\tests\\unit\\phpDocumentor\\Infrastructure\\Parser', $fixture->getPath());
}
/**
* @covers ::__construct
* @covers ::__toString
* @covers ::getScheme
* @covers ::getHost
* @covers ::getPort
* @covers ::getPath
* @covers ::<private>
* @uses \phpDocumentor\DomainModel\Path
*/
public function testValidWindowsDsnWithScheme()
{
$dsn = "file://C:\\phpdocumentor\\tests";
$fixture = new Dsn($dsn);
$this->assertEquals('file://C:\\phpdocumentor\\tests', (string)$fixture);
$this->assertEquals('file', $fixture->getscheme());
$this->assertEquals(null, $fixture->getHost());
$this->assertEquals(0, $fixture->getPort());
$this->assertEquals('C:\\phpdocumentor\\tests', $fixture->getPath());
}
/**
* @covers ::__construct
* @covers ::getScheme
@@ -33,7 +33,7 @@ public function testSingleSourceDir()
new SpecificationFactory(),
new FlySystemFactory(new MountManager())
);
echo 'file://' . __DIR__;
$files = $fileCollector->getFiles(new Dsn('file://' . __DIR__), ['assets'], [], ['php']);
static::assertCount(3, $files);
}

0 comments on commit 4b36a14

Please sign in to comment.