Skip to content

Commit

Permalink
Adds new API for middleware and withTrashed
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Jul 10, 2023
1 parent 5b9e4e4 commit 8e11dc1
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 46 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,9 @@ By default, models that have been soft deleted are not retrieved when resolving
```php
<?php

use function Laravel\Folio\{page};
use function Laravel\Folio\{withTrashed};

page(withTrashed: true);
withTrashed();

?>

Expand All @@ -183,9 +183,9 @@ You can apply middleware to a specific page by invoking the `page` function with
```php
<?php

use function Laravel\Folio\{page};
use function Laravel\Folio\{middleware};

page(middleware: ['auth']);
middleware(['auth']);

?>

Expand Down
33 changes: 22 additions & 11 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
namespace Laravel\Folio;

use Closure;
use Illuminate\Container\Container;
use Illuminate\Support\Arr;
use Laravel\Folio\Exceptions\MetadataIntercepted;
use Laravel\Folio\Options\PageOptions;

function page(Closure|string|array $middleware = [], bool $withTrashed = false)
/**
* Adds one or more middleware to the current page.
*/
function middleware(Closure|string|array $middleware = []): PageOptions
{
if (Container::getInstance()->make(InlineMetadataInterceptor::class)->listening()) {
throw new MetadataIntercepted(
new Metadata(
middleware: collect(Arr::wrap($middleware)),
withTrashed: $withTrashed,
)
);
}
app(InlineMetadataInterceptor::class)->whenListening(
fn () => Metadata::instance()->middleware = Metadata::instance()->middleware->merge(Arr::wrap($middleware)),
);

return new PageOptions;
}

/**
* Indicates that the current page should include trashed models.
*/
function withTrashed(bool $withTrashed = true): PageOptions
{
app(InlineMetadataInterceptor::class)->whenListening(
fn () => Metadata::instance()->withTrashed = $withTrashed,
);

return new PageOptions;
}
16 changes: 0 additions & 16 deletions src/Exceptions/MetadataIntercepted.php

This file was deleted.

16 changes: 9 additions & 7 deletions src/InlineMetadataInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Laravel\Folio;

use Laravel\Folio\Exceptions\MetadataIntercepted;
use Laravel\Folio\Pipeline\MatchedView;

class InlineMetadataInterceptor
Expand Down Expand Up @@ -41,13 +40,13 @@ public function intercept(MatchedView $matchedView): Metadata
require $__path;
})();
});
} catch (MetadataIntercepted $e) {
return $this->cache[$matchedView->path] = $e->metadata;
} finally {
ob_get_clean();

$metadata = tap(Metadata::instance(), fn() => Metadata::flush());
}

return $this->cache[$matchedView->path] = new Metadata;
return $this->cache[$matchedView->path] = $metadata;
}

/**
Expand All @@ -57,6 +56,7 @@ public function listen(callable $callback): void
{
$this->listening = true;


try {
$callback();
} finally {
Expand All @@ -65,10 +65,12 @@ public function listen(callable $callback): void
}

/**
* Determine if the interceptor is listening for metadata.
* Execute the callback if the interceptor is listening for metadata.
*/
public function listening(): bool
public function whenListening(callable $callback): void
{
return $this->listening;
if ($this->listening) {
$callback();
}
}
}
23 changes: 22 additions & 1 deletion src/Metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,34 @@

class Metadata
{
/**
* The current global instance of the metadata, if any.
*/
protected static ?self $instance = null;

/**
* Create a new metadata instance.
*/
public function __construct(
protected function __construct(
public Collection $middleware = new Collection,
public bool $withTrashed = false
) {
//
}

/**
* Get the current compile context instance or create a new one.
*/
public static function instance(): static
{
return static::$instance ??= new static;
}

/**
* Flush the current global instance of the compile context.
*/
public static function flush(): void
{
static::$instance = null;
}
}
25 changes: 25 additions & 0 deletions src/Options/PageOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Laravel\Folio\Options;

use Closure;
use function Laravel\Folio\{middleware, withTrashed};

class PageOptions
{
/**
* Adds one or more middleware to the current page.
*/
public function middleware(Closure|string|array $middleware = []): static
{
return middleware($middleware);
}

/**
* Indicates that the current page should include trashed models.
*/
public function withTrashed(bool $withTrashed = true): static
{
return withTrashed($withTrashed);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
use function Laravel\Folio\page;
use function Laravel\Folio\middleware;
page(middleware: 'auth');
middleware('auth');
?>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
use function Laravel\Folio\{page};
page(withTrashed: true);
use function Laravel\Folio\{withTrashed};
withTrashed();
?>

<div>
{{ $podcast->name }}
</div>
8 changes: 5 additions & 3 deletions tests/Feature/resources/views/pages/flights/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php
use function Laravel\Folio\{page};
page(middleware: [function ($request, $next) {
use function Laravel\Folio\{middleware};
middleware(function ($request, $next) {
$_SERVER['__folio_flights_inline_middleware'] = true;
return $next($request);
}]);
});
?>

<div>
Expand Down

0 comments on commit 8e11dc1

Please sign in to comment.