Skip to content

Commit

Permalink
Merge pull request #1 from spurjobs/support-remote-sources
Browse files Browse the repository at this point in the history
Support remote sources
  • Loading branch information
Adam Campbell authored Jun 9, 2020
2 parents c275263 + ebb20af commit 1a49ac8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
7 changes: 4 additions & 3 deletions config/spectator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@
'sources' => [
'local' => [
'source' => 'local',
'base_folder' => env('SPEC_FOLDER'),
'base_path' => env('SPEC_PATH'),
],

'remote' => [
'source' => 'remote',
'base_url' => env('SPEC_URL'),
'base_path' => env('SPEC_PATH'),
'params' => env('SPUR_URL_PARAMS', ''),
],

'github' => [
'source' => 'github',
'base_path' => env('SPEC_PATH'),
'repo' => env('SPEC_GITHUB_REPO'),
'token' => env('SPEC_GITHUB_TOKEN'),
'base_folder' => env('SPEC_FOLDER'),
],
],
];
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</whitelist>
</filter>
<php>
<env name="SPEC_FOLDER" value="./tests/Fixtures"/>
<env name="SPEC_PATH" value="./tests/Fixtures"/>
<env name="SPEC_URL_PARAMS" value=""/>
</php>
</phpunit>
45 changes: 33 additions & 12 deletions src/RequestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,45 @@ protected function getFile()
throw new MissingSpecException('Cannot resolve schema with missing or invalid spec.');
}

if ($source['source'] === 'local') {
$path = $source['base_folder'];
$method = Str::camel("get_{$source['source']}_path");

if (!Str::endsWith($path, '/')) {
$path = $path.'/';
}
if (method_exists($this, $method)) {
return $this->{$method}($source, str_replace('/', '', $this->specName));
}

$file = str_replace('/', '', $this->specName);
throw new MissingSpecException('Cannot resolve schema with missing or invalid spec.');
}

$path = realpath("{$path}/{$file}");
protected function getLocalPath(array $source, $file)
{
$path = $this->standardizePath($source['base_path']);

if (!file_exists($path)) {
throw new MissingSpecException('Cannot resolve schema with missing or invalid spec.');
}
$path = realpath("{$path}{$file}");

return $path;
if (!file_exists($path)) {
throw new MissingSpecException('Cannot resolve schema with missing or invalid spec.');
}

throw new MissingSpecException('Cannot resolve schema with missing or invalid spec.');
return $path;
}

protected function getRemotePath(array $source, $file)
{
$path = $this->standardizePath($source['base_path']);

$params = Arr::get($source, 'params', '');

$path = "{$path}{$file}{$params}";

return $path;
}

protected function standardizePath($path)
{
if (!Str::endsWith($path, '/')) {
$path = $path.'/';
}

return $path;
}
}
8 changes: 4 additions & 4 deletions tests/RequestFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function test_throws_exception_on_invalid_source()

$factory->using($name);

$spec = $factory->resolve();
$factory->resolve();
}

public function test_throws_exception_on_missing_spec_name()
Expand All @@ -84,7 +84,7 @@ public function test_throws_exception_on_missing_spec_name()

$factory = new RequestFactory();

$spec = $factory->resolve();
$factory->resolve();
}

public function test_throws_exception_on_invalid_spec_name()
Expand All @@ -98,7 +98,7 @@ public function test_throws_exception_on_invalid_spec_name()

$factory->using($name);

$spec = $factory->resolve();
$factory->resolve();
}

public function test_throws_exception_on_invalid_spec_extension()
Expand All @@ -112,6 +112,6 @@ public function test_throws_exception_on_invalid_spec_extension()

$factory->using($name);

$spec = $factory->resolve();
$factory->resolve();
}
}

0 comments on commit 1a49ac8

Please sign in to comment.