Skip to content

Commit

Permalink
fixes windows paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jaapio committed Dec 16, 2017
1 parent 76e5ba1 commit 4b36a14
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
25 changes: 21 additions & 4 deletions src/phpDocumentor/DomainModel/Dsn.php
Expand Up @@ -44,6 +44,8 @@ final class Dsn
/** @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
*
Expand Down Expand Up @@ -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)
);
Expand Down Expand Up @@ -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
*
Expand Down
45 changes: 45 additions & 0 deletions tests/unit/phpDocumentor/DomainModel/DsnTest.php
Expand Up @@ -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
Expand Down
Expand Up @@ -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);
}
Expand Down

0 comments on commit 4b36a14

Please sign in to comment.