Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/routing memory leak #23

Merged
merged 5 commits into from Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -8,6 +8,8 @@ This is a tool for Laravel's Nova administrator panel that allows you to create

[![Screenshot](https://raw.githubusercontent.com/dniccum/nova-documentation/master/screenshots/screenshot-1.png)](https://raw.githubusercontent.com/dniccum/nova-documentation/master/screenshots/screenshot-1.png)



## Compatibility Note

Please note, this plugin now **only supports Laravel Nova v4**. If you are using Laravel Nova <= v3, please use version `^3.0`,
Expand Down Expand Up @@ -61,7 +63,7 @@ public function tools()

## Upgrading from version 2

If you are upgrading from version 2 to version 3 (Laravel 4 support), make sure your `composer.json` has the following version/reference to included the updated version:
If you are upgrading from version 2 to version 3 (Laravel Nova 4 support), make sure your `composer.json` has the following version/reference to included the updated version:

```
"dniccum/nova-documentation": "^3.0"
Expand Down Expand Up @@ -145,6 +147,10 @@ Other types of links that are supported:
* Mailto (`mailto:`) links
* External http and https links

### Routes and adding new pages

When a new document is added to the application architecture, and if your application leverages route caching, **be sure to clear/reset your route cache accordingly** (`php artisan route:clear`).

## Configuration

The configuration items listed below can be found in the `novadocumentation.php` configuration file.
Expand Down
25 changes: 24 additions & 1 deletion routes/inertia.php
@@ -1,5 +1,8 @@
<?php

use Dniccum\NovaDocumentation\Library\Contracts\DocumentationPage;
use Dniccum\NovaDocumentation\Library\MarkdownUtility;
use Dniccum\NovaDocumentation\Library\RouteUtility;
use Illuminate\Support\Facades\Route;
use Laravel\Nova\Http\Requests\NovaRequest;

Expand All @@ -15,4 +18,24 @@
*/

Route::get('/', [ \Dniccum\NovaDocumentation\Http\Controllers\NovaDocumentationController::class, 'home' ])
->name('nova.tools.documentation-home');
->name('nova.tools.documentation-home');

try {
$utility = new MarkdownUtility();
$pageRoutes = $utility->buildPageRoutes();

/**
* @var DocumentationPage[] $filteredRoutes
*/
$filteredRoutes = collect($pageRoutes)
->filter(fn(DocumentationPage $item) => !$item->isHome)
->toArray();

foreach ($filteredRoutes as $filteredRoute) {
\Illuminate\Support\Facades\Route::get("/$filteredRoute->route", function () use ($filteredRoute, $pageRoutes) {
return RouteUtility::buildDocumentRoute($filteredRoute->file, $pageRoutes);
});
}
} catch (\Dniccum\NovaDocumentation\Exceptions\DocumentationParsingException $exception) {
abort($exception->getCode() > 0 ? $exception->getCode() : 500, $exception->getMessage());
}
8 changes: 8 additions & 0 deletions src/Exceptions/DocumentationParsingException.php
@@ -0,0 +1,8 @@
<?php

namespace Dniccum\NovaDocumentation\Exceptions;

class DocumentationParsingException extends \Exception
{

}
3 changes: 2 additions & 1 deletion src/Library/MarkdownUtility.php
Expand Up @@ -4,6 +4,7 @@
use cebe\markdown\Markdown;
use cebe\markdown\GithubMarkdown;
use cebe\markdown\MarkdownExtra;
use Dniccum\NovaDocumentation\Exceptions\DocumentationParsingException;
use Dniccum\NovaDocumentation\Library\Contracts\DocumentationPage;
use Dniccum\NovaDocumentation\Library\Contracts\PageContent;
use Spatie\YamlFrontMatter\YamlFrontMatter;
Expand Down Expand Up @@ -115,7 +116,7 @@ public function buildPageRoutes()
is_int(strpos($files[$i], config('novadocumentation.home')))
);
}
} catch (\Exception $e) {
} catch (DocumentationParsingException $e) {
abort(500, $e);
}

Expand Down
21 changes: 4 additions & 17 deletions src/ToolServiceProvider.php
Expand Up @@ -4,7 +4,7 @@

use Dniccum\NovaDocumentation\Library\Contracts\DocumentationPage;
use Dniccum\NovaDocumentation\Library\MarkdownUtility;
use Dniccum\NovaDocumentation\Library\RouteUtility;
use Illuminate\Support\Facades\Route;
use Laravel\Nova\Nova;
use Laravel\Nova\Events\ServingNova;
use Illuminate\Support\ServiceProvider;
Expand Down Expand Up @@ -85,22 +85,9 @@ protected function routes()
return;
}

Nova::router(['nova', Authorize::class], $this->prefix)
->group(function() {
/**
* @var DocumentationPage[] $filteredRoutes
*/
$filteredRoutes = collect($this->pageRoutes)
->filter(fn(DocumentationPage $item) => !$item->isHome)
->toArray();
foreach ($filteredRoutes as $filteredRoute) {
$this->app['router']->get("/$filteredRoute->route", function() use ($filteredRoute) {
return RouteUtility::buildDocumentRoute($filteredRoute->file, $this->pageRoutes);
});
}

require(__DIR__.'/../routes/inertia.php');
});
Route::middleware(['nova', Authorize::class])
->prefix('nova-vendor/nova-documentation')
->group(__DIR__.'/../routes/inertia.php');
}

/**
Expand Down