Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Oct 1, 2020
2 parents 7912041 + ecdb5a9 commit 8a1e845
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 58 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project aims to adhere to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.9.0 (Thursday, 1 October, 2020)
### Changes
- Start database transaction much earlier and close it much later for ApiResource and Transformer strategies. Also set current route properly when resolving (https://github.com/knuckleswtf/scribe/pull/104)

## 1.8.3 (Thursday, 17 September, 2020)
### Fixes
- Reverts 1.8.2 as it broke a few things (https://github.com/knuckleswtf/scribe/commit/5a2217513945bcb92ca26e463f7717c0efb99ac1)
Expand Down
8 changes: 4 additions & 4 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ In the template, you have the `$baseUrl` and `$route` variables available to you
.. Important:: Parameters which have been excluded from the example requests (see `Specifying Example Values <documenting-endpoint-query-parameters.html#specifying-example-values>`_) will not be present in :code:`cleanQueryParameters`, :code:`cleanBodyParameters`, or :code:`fileParameters`.
```

Finally, add the language to the `example_languages` array in your config and generate your documentation as usual.

```eval_rst
.. Tip:: You can make use of some utilities in the class :code:`\Knuckles\Scribe\Tools\WritingUtils` to help you easily output data in various forms (such as in key=value format or as a PHP array). Please take a look at that class and its usages in the included example request templates for details.
.. Note:: Scribe makes use of CSS from [ighlight.js](https://highlightjs.org) for its syntax highlighting. The bundle we use only includes support for a bunch of the most popular languages. If your language isn't supported (all code appears white), you can [download a new CSS bundle](https://highlightjs.org/download/) yourself, and include your desired language. Then locate the highlight.js CSS file that Scribe outputs for you after generation, and replace that with the one you downloaded.
```

Finally, add the language to the `example_languages` array in your config and generate your documentation as usual.

## Customizing the code used in examples
Customising existing language templates follows the same process as described above: publish assets, then modify the Blade templates in `resources/views/vendor/scribe/partials/example-requests` as necessary.

Expand Down Expand Up @@ -124,4 +124,4 @@ There are also a number of included components that you can utilize in your Blad
- `badges/base.blade.php`: The base badge component, used by `auth` and `http-method`. Takes in `colour` and `text` attributes, and uses Pastel's badge classes to create a badge.

## Changing the CSS styles
The CSS styling is provided by Pastel, which currently supports only one template. Consider making a pull request to add your alternative styles.
The CSS styling is provided by Pastel, which currently supports only one template. Consider making a pull request to add your alternative styles. In the meantime, you can manualy add CSS files to the generated output directory.
23 changes: 16 additions & 7 deletions src/Extracting/Strategies/Responses/UseApiResourceTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,15 @@ public function __invoke(Route $route, ReflectionClass $controller, ReflectionFu
$methodDocBlock = $docBlocks['method'];

try {
return $this->getApiResourceResponse($methodDocBlock->getTags());
$this->startDbTransaction();
return $this->getApiResourceResponse($methodDocBlock->getTags(), $route);
} catch (Exception $e) {
c::warn('Exception thrown when fetching Eloquent API resource response for [' . implode(',', $route->methods) . "] {$route->uri}.");
e::dumpExceptionIfVerbose($e);

return null;
} finally {
$this->endDbTransaction();
}
}

Expand All @@ -62,9 +66,11 @@ public function __invoke(Route $route, ReflectionClass $controller, ReflectionFu
*
* @param Tag[] $tags
*
* @param \Illuminate\Routing\Route $route
* @return array|null
* @throws \Exception
*/
public function getApiResourceResponse(array $tags)
public function getApiResourceResponse(array $tags, Route $route)
{
if (empty($apiResourceTag = $this->getApiResourceTag($tags))) {
return null;
Expand Down Expand Up @@ -111,8 +117,11 @@ public function getApiResourceResponse(array $tags)
: $apiResourceClass::collection($list);
}


/** @var Response $response */
$response = $resource->toResponse(app(Request::class));
$response = $resource->toResponse(app(Request::class)->setRouteResolver(function () use ($route) {
return $route;
}));

return [
[
Expand Down Expand Up @@ -171,7 +180,6 @@ private function getClassToBeTransformedAndAttributes(array $tags): array
*/
protected function instantiateApiResourceModel(string $type, array $factoryStates = [], array $relations = [])
{
$this->startDbTransaction();
try {
// Try Eloquent model factory

Expand All @@ -181,7 +189,10 @@ protected function instantiateApiResourceModel(string $type, array $factoryState

$factory = Utils::getModelFactory($type, $factoryStates);
try {
return $factory->create();
$model = $factory->create();
$model->load($relations);

return $model;
} catch (Exception $e) {
// If there was no working database, it would fail.
return $factory->make();
Expand All @@ -204,8 +215,6 @@ protected function instantiateApiResourceModel(string $type, array $factoryState
e::dumpExceptionIfVerbose($e);
}
}
} finally {
$this->endDbTransaction();
}

return $instance;
Expand Down
6 changes: 3 additions & 3 deletions src/Extracting/Strategies/Responses/UseTransformerTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,15 @@ public function __invoke(Route $route, ReflectionClass $controller, ReflectionFu
$methodDocBlock = $docBlocks['method'];

try {
$this->startDbTransaction();
return $this->getTransformerResponse($methodDocBlock->getTags());
} catch (Exception $e) {
c::warn('Exception thrown when fetching transformer response for [' . implode(',', $route->methods) . "] {$route->uri}.");
e::dumpExceptionIfVerbose($e);

return null;
} finally {
$this->endDbTransaction();
}
}

Expand Down Expand Up @@ -160,7 +163,6 @@ private function getClassToBeTransformed(array $tags, ReflectionFunctionAbstract

protected function instantiateTransformerModel(string $type, array $factoryStates = [], array $relations = [])
{
$this->startDbTransaction();
try {
// try Eloquent model factory

Expand Down Expand Up @@ -193,8 +195,6 @@ protected function instantiateTransformerModel(string $type, array $factoryState
e::dumpExceptionIfVerbose($e);
}
}
} finally {
$this->endDbTransaction();
}

return $instance;
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/TestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class TestUser extends Model
{

public function children()
{
return $this->hasMany(TestUser::class, 'parent_id');
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/TestUserApiResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function toArray($request)
}),
];

if($request->route()->named('test')) {
$result['test'] = true;
}

if ($this['state1'] && $this['random-state']) {
$result['state1'] = $this['state1'];
$result['random-state'] = $this['random-state'];
Expand Down
8 changes: 7 additions & 1 deletion tests/Fixtures/TestUserApiResourceCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ class TestUserApiResourceCollection extends ResourceCollection
*/
public function toArray($request)
{
return [
$data = [
'data' => $this->collection,
'links' => [
'self' => 'link-value',
],
];

if($request->route()->named('test')) {
$data['test'] = true;
}

return $data;
}
}
Loading

0 comments on commit 8a1e845

Please sign in to comment.