Skip to content

Commit 57bd39a

Browse files
committed
Cleanup: replace mergeResults with StaticData strategy
1 parent 9882564 commit 57bd39a

File tree

10 files changed

+99
-88
lines changed

10 files changed

+99
-88
lines changed

config/scribe.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Knuckles\Scribe\Config;
44
use Knuckles\Scribe\Config\{AuthIn,ExternalTheme};
55
use Knuckles\Scribe\Extracting\Strategies;
6-
use function Knuckles\Scribe\Config\{mergeResults, removeStrategies, withConfiguredStrategy};
6+
use function Knuckles\Scribe\Config\{removeStrategies, withConfiguredStrategy};
77

88
/**
99
* For documentation, use your IDE's autocomplete features, or see https://scribe.knuckles.wtf/laravel/reference/config
@@ -33,14 +33,17 @@
3333
),
3434
strategies: Config\Extracting::strategies(
3535
// Use removeStrategies() to remove an included strategy.
36-
// Use mergeResults() to override the data returned from a strategy. Use withConfiguredStrategy() to configure a strategy which supports it.
36+
// Use withConfiguredStrategy() to configure a strategy which supports it.
3737
metadata: [...Config\Defaults::METADATA_STRATEGIES],
3838
urlParameters: [...Config\Defaults::URL_PARAMETERS_STRATEGIES],
3939
queryParameters: [...Config\Defaults::QUERY_PARAMETERS_STRATEGIES],
40-
headers: mergeResults(Config\Defaults::HEADERS_STRATEGIES, with: [
41-
'Content-Type' => 'application/json',
42-
'Accept' => 'application/json',
43-
]),
40+
headers: [
41+
...Config\Defaults::HEADERS_STRATEGIES,
42+
Strategies\StaticData::withSettings(data: [
43+
'Content-Type' => 'application/json',
44+
'Accept' => 'application/json',
45+
]),
46+
],
4447
bodyParameters: [...Config\Defaults::BODY_PARAMETERS_STRATEGIES],
4548
responses: withConfiguredStrategy(
4649
Config\Defaults::RESPONSES_STRATEGIES,

config/scribe_old.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Knuckles\Scribe\Extracting\Strategies;
44
use Knuckles\Scribe\Config;
5-
use function Knuckles\Scribe\Config\{mergeResults, removeStrategies, withConfiguredStrategy};
5+
use function Knuckles\Scribe\Config\{removeStrategies, withConfiguredStrategy};
66

77
return [
88
// The HTML <title> for the generated documentation. If this is empty, Scribe will infer it from config('app.name').
@@ -203,7 +203,7 @@
203203

204204
// The strategies Scribe will use to extract information about your routes at each stage.
205205
// Use removeStrategies() to remove an included strategy.
206-
// Use mergeResults() to override the data returned from a strategy. Use withConfiguredStrategy() to configure a strategy which supports it.
206+
// Use withConfiguredStrategy() to configure a strategy which supports it.
207207
'strategies' => [
208208
'metadata' => [
209209
...Config\Defaults::METADATA_STRATEGIES,
@@ -214,10 +214,13 @@
214214
'queryParameters' => [
215215
...Config\Defaults::QUERY_PARAMETERS_STRATEGIES,
216216
],
217-
'headers' => mergeResults(Config\Defaults::HEADERS_STRATEGIES, with: [
218-
'Content-Type' => 'application/json',
219-
'Accept' => 'application/json',
220-
]),
217+
'headers' => [
218+
...Config\Defaults::HEADERS_STRATEGIES,
219+
Strategies\StaticData::withSettings(data: [
220+
'Content-Type' => 'application/json',
221+
'Accept' => 'application/json',
222+
]),
223+
],
221224
'bodyParameters' => [
222225
...Config\Defaults::BODY_PARAMETERS_STRATEGIES,
223226
],

src/Config/strategies.php

+1-25
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,12 @@
33
namespace Knuckles\Scribe\Config;
44

55
use Illuminate\Support\Arr;
6-
use Knuckles\Scribe\Extracting\Strategies\Strategy;
76

87
// Strategies can be:
98
// 1. (Original) A class name, e.g. Strategies\Responses\ResponseCalls::class
109
// 2. (New) A tuple containing the class name as item 1, and its config array as item 2
1110
// 3. (New) A tuple containing "merge" as item 1, and the values to override array as item 2
1211

13-
/**
14-
* Merge the results of all previous strategies with some static data.
15-
*
16-
* @param array $strategies
17-
* @param array $with
18-
* @param array $only The routes that should be overridden. Same format as the route matcher.
19-
* @param array $except The routes that should not be overridden. Same format as the route matcher.
20-
* @return array
21-
*/
22-
function mergeResults(array $strategies, array $with, array $only = ['*'], array $except = []): array
23-
{
24-
$mergeStrategy = Strategy::wrapWithSettings('merge', only: $only, except: $except, otherSettings: ['with' => $with]);
25-
return addStrategies($strategies, [$mergeStrategy]);
26-
}
27-
28-
/**
29-
* Add one or more strategies to a list of strategies. Only use this if you know the strategy isn't already in the list.
30-
*/
31-
function addStrategies(array $strategies, array $newStrategies = []): array
32-
{
33-
return array_merge($strategies, $newStrategies);
34-
}
35-
3612
/**
3713
* Remove one or more strategies from a list of strategies.
3814
*/
@@ -70,7 +46,7 @@ function withConfiguredStrategy(array $strategies, array $configurationTuple): a
7046

7147
// If strategy wasn't in there, add it.
7248
if (!$strategyFound) {
73-
$strategies = addStrategies($strategies, [$configurationTuple]);
49+
$strategies = array_merge($strategies, [$configurationTuple]);
7450
}
7551
return $strategies;
7652
}

src/Extracting/Extractor.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
use Knuckles\Camel\Extraction\ResponseCollection;
1515
use Knuckles\Camel\Extraction\ResponseField;
1616
use Knuckles\Camel\Output\OutputEndpointData;
17-
use Knuckles\Scribe\Extracting\Strategies\Override;
17+
use Knuckles\Scribe\Extracting\Strategies\StaticData;
18+
use Knuckles\Scribe\Tools\ConsoleOutputUtils as c;
1819
use Knuckles\Scribe\Tools\DocumentationConfig;
1920
use Knuckles\Scribe\Tools\RoutePatternMatcher;
2021

@@ -210,13 +211,17 @@ protected function iterateThroughStrategies(
210211
if (is_array($strategyClassOrTuple)) {
211212
[$strategyClass, &$settings] = $strategyClassOrTuple;
212213
if ($strategyClass == 'override') {
213-
$strategyClass = Override::class;
214-
// Overrides can be short: ['override', ['key' => 'value']],
215-
// or extended ['override', ['with' => ['key' => 'value'], 'only' => ['GET *'], 'except' => []]],
216-
$settingsFormat = array_key_exists('with', $settings) ? 'extended' : 'short';
214+
c::warn("The 'override' strategy was renamed to 'static_data', and will stop working in the future. Please replace 'override' in your config file with 'static_data'.");
215+
$strategyClass = 'static_data';
216+
}
217+
if ($strategyClass == 'static_data') {
218+
$strategyClass = StaticData::class;
219+
// Static data can be short: ['static_data', ['key' => 'value']],
220+
// or extended ['static_data', ['data' => ['key' => 'value'], 'only' => ['GET *'], 'except' => []]],
221+
$settingsFormat = array_key_exists('data', $settings) ? 'extended' : 'short';
217222
$settings = match($settingsFormat) {
218223
'extended' => $settings,
219-
'short' => ['with' => $settings],
224+
'short' => ['data' => $settings],
220225
};
221226
}
222227
} else {

src/Extracting/Strategies/Override.php

-13
This file was deleted.

src/Extracting/Strategies/Responses/ResponseCalls.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ protected function getResponseHeaders($response): array
301301
* @return array
302302
*/
303303
public static function withSettings(
304-
array $only = ['GET *'],
304+
array $only = [],
305305
array $except = [],
306306
array $config = [],
307307
array $queryParams = [],
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Knuckles\Scribe\Extracting\Strategies;
4+
5+
use Knuckles\Camel\Extraction\ExtractedEndpointData;
6+
7+
/**
8+
* A simple strategy that returns a set of static data.
9+
*/
10+
class StaticData extends Strategy
11+
{
12+
public function __invoke(ExtractedEndpointData $endpointData, array $settings = []): ?array
13+
{
14+
return $settings['data'];
15+
}
16+
17+
public static function withSettings(
18+
array $only = [],
19+
array $except = [],
20+
array $data = [],
21+
): array
22+
{
23+
return static::wrapWithSettings(
24+
static::class, only: $only, except: $except, otherSettings: compact(
25+
'data',
26+
));
27+
}
28+
}

src/Extracting/Strategies/Strategy.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ abstract public function __invoke(ExtractedEndpointData $endpointData, array $se
4141
* Specify route names ("users.index", "users.*"), or method and path ("GET *", "POST /safe/*").
4242
* @return array{string,array} Tuple of strategy class FQN and specified settings.
4343
*/
44-
public static function wrapWithSettings(
44+
protected static function wrapWithSettings(
4545
string $strategyName = self::class,
46-
array $only = ['*'],
46+
array $only = [],
4747
array $except = [],
4848
array $otherSettings = []
4949
): array

tests/GenerateDocumentation/OutputTest.php

+8-26
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Support\Facades\Storage;
77
use Illuminate\Support\Facades\View;
88
use Illuminate\Support\Str;
9-
use Knuckles\Scribe\Config\Defaults;
109
use Knuckles\Scribe\Tests\BaseLaravelTest;
1110
use Knuckles\Scribe\Tests\Fixtures\TestController;
1211
use Knuckles\Scribe\Tests\Fixtures\TestGroupController;
@@ -22,7 +21,6 @@
2221
use Symfony\Component\DomCrawler\Crawler;
2322
use Symfony\Component\Yaml\Yaml;
2423
use Knuckles\Scribe\Extracting\Strategies;
25-
use function Knuckles\Scribe\Config\mergeResults;
2624
use function Knuckles\Scribe\Config\withConfiguredStrategy;
2725

2826
class OutputTest extends BaseLaravelTest
@@ -140,9 +138,10 @@ public function generated_postman_collection_file_is_correct()
140138
'postman.overrides' => [
141139
'info.version' => '3.9.9',
142140
],
143-
'strategies.headers' => mergeResults(config('scribe.strategies.headers'), [
144-
'Custom-Header' => 'NotSoCustom',
145-
]),
141+
'strategies.headers' => [
142+
...config('scribe.strategies.headers'),
143+
Strategies\StaticData::withSettings(data: ['Custom-Header' => 'NotSoCustom']),
144+
],
146145
]);
147146
$this->enableResponseCalls();
148147

@@ -201,9 +200,10 @@ public function generated_openapi_spec_file_is_correct()
201200
'openapi.overrides' => [
202201
'info.version' => '3.9.9',
203202
],
204-
'strategies.headers' => mergeResults(Defaults::HEADERS_STRATEGIES, [
205-
'Custom-Header' => 'NotSoCustom',
206-
]),
203+
'strategies.headers' => [
204+
...config('scribe.strategies.headers'),
205+
Strategies\StaticData::withSettings(data: ['Custom-Header' => 'NotSoCustom']),
206+
],
207207
]);
208208
$this->enableResponseCalls();
209209

@@ -218,24 +218,6 @@ public function generated_openapi_spec_file_is_correct()
218218
$this->assertEquals($fixtureSpec, $generatedSpec);
219219
}
220220

221-
/** @test */
222-
public function can_append_custom_http_headers()
223-
{
224-
RouteFacade::get('/api/headers', [TestController::class, 'checkCustomHeaders']);
225-
$this->setConfig([
226-
'strategies.headers' => mergeResults(Defaults::HEADERS_STRATEGIES, [
227-
'Authorization' => 'customAuthToken',
228-
'Custom-Header' => 'NotSoCustom',
229-
]
230-
),
231-
]);
232-
$this->generate();
233-
234-
$endpointDetails = Yaml::parseFile('.scribe/endpoints/00.yaml')['endpoints'][0];
235-
$this->assertEquals("customAuthToken", $endpointDetails['headers']["Authorization"]);
236-
$this->assertEquals("NotSoCustom", $endpointDetails['headers']["Custom-Header"]);
237-
}
238-
239221
/** @test */
240222
public function can_parse_utf8_response()
241223
{

tests/Unit/ExtractorTest.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public function overrides_headers_based_on_short_strategy_config()
207207
$this->config['strategies']['headers'] = [
208208
Strategies\Headers\GetFromHeaderAttribute::class,
209209
[
210-
'override', $headers
210+
'static_data', $headers
211211
],
212212
];
213213
$parsed = $this->process($route);
@@ -228,14 +228,41 @@ public function overrides_headers_based_on_extended_strategy_config()
228228
];
229229
$this->config['strategies']['headers'] = [
230230
Strategies\Headers\GetFromHeaderAttribute::class,
231-
['override', ['with' => $headers, 'only' => ['GET *']]],
231+
['static_data', ['data' => $headers, 'only' => ['GET *']]],
232232
];
233233
$parsed = $this->process($route);
234234
$this->assertArraySubset($parsed->headers, $headers);
235235

236236
$this->config['strategies']['headers'] = [
237237
Strategies\Headers\GetFromHeaderAttribute::class,
238-
['override', ['with' => $headers, 'only' => [], 'except' => ['GET *']]],
238+
['static_data', ['data' => $headers, 'only' => [], 'except' => ['GET *']]],
239+
];
240+
$parsed = $this->process($route);
241+
$this->assertEmpty($parsed->headers);
242+
}
243+
244+
/** @test */
245+
public function overrides_headers_based_on_full_strategy_config()
246+
{
247+
$route = $this->createRoute('GET', '/get', 'dummy');
248+
$this->config['strategies']['headers'] = [Strategies\Headers\GetFromHeaderAttribute::class];
249+
$parsed = $this->process($route);
250+
$this->assertEmpty($parsed->headers);
251+
252+
$headers = [
253+
'accept' => 'application/json',
254+
'Content-Type' => 'application/json+vnd',
255+
];
256+
$this->config['strategies']['headers'] = [
257+
Strategies\Headers\GetFromHeaderAttribute::class,
258+
Strategies\StaticData::withSettings(only: ['GET *'], data: $headers),
259+
];
260+
$parsed = $this->process($route);
261+
$this->assertArraySubset($parsed->headers, $headers);
262+
263+
$this->config['strategies']['headers'] = [
264+
Strategies\Headers\GetFromHeaderAttribute::class,
265+
Strategies\StaticData::withSettings(except: ['GET *'], data: $headers),
239266
];
240267
$parsed = $this->process($route);
241268
$this->assertEmpty($parsed->headers);

0 commit comments

Comments
 (0)