Giter Club home page Giter Club logo

Comments (2)

driesvints avatar driesvints commented on May 14, 2024

Right now this does not work with vanilla PHP. But I'll bring this up internally. Can't provide guarantees however that we'll pursue this sorry. Thanks for your request!

from folio.

inmanturbo avatar inmanturbo commented on May 14, 2024

Thanks for taking a look @driesvints!

This commit in a demo shows a naive bare minimum of what someone would have to do currently, to get this working on their own:

inmanturbo/folio-demo@a6e6f9b

It would be nice if the Laravel\Folio\Router simply called something like Folio::pipeline() (made up method) which by default would just return the array currently being used by the package, and if Folio exposed a public method, (for the sake of this example called withPipeline()) where one could (optionally) set a custom array as a property to be traversed in Laravel\Folio\Router::matchAtPath():

// Laravel\Folio\Router

    /**
     * Resolve the given URI via page based routing at the given mount path.
     */
    protected function matchAtPath(string $mountPath, Request $request, string $uri): ?MatchedView
    {
        $state = new State(
            uri: $uri,
            mountPath: $mountPath,
            segments: explode('/', $uri)
        );

        for ($i = 0; $i < $state->uriSegmentCount(); $i++) {
            $value = (new Pipeline)
                ->send($state->forIteration($i))
-                ->through([
-                   new EnsureNoDirectoryTraversal,
-                    new TransformModelBindings($request),
-                    new SetMountPathOnMatchedView,
-                    // ...
-                    new MatchRootIndex,
-                    new MatchDirectoryIndexViews,
-                    new MatchWildcardViewsThatCaptureMultipleSegments,
-                    new MatchLiteralDirectories,
-                    new MatchWildcardDirectories,
-                    new MatchLiteralViews,
-                    new MatchWildcardViews,
-                ])->then(fn () => new StopIterating);
+                ->through(Folio::pipeline())
+                ->then(fn () => new StopIterating);

            if ($value instanceof MatchedView) {
                return $value;
            } elseif ($value instanceof ContinueIterating) {
                $state = $value->state;

                continue;
            } elseif ($value instanceof StopIterating) {
                break;
            }
        }

        return null;
    }
//Laravel\Folio\FolioManager

public array $pipeline = [];

public function withPipeline(array $pipeline)
{
    $this->pipeline = $pipeline;
   
    return $this;
}

public function pipeline() : array
{
     return !empty($this->pipeline) ?
                  $this->pipeline :
                  [ 
                   // default pipeline array
                    new EnsureNoDirectoryTraversal,
                    new TransformModelBindings($request),
                    new SetMountPathOnMatchedView,
                    // ...
                    new MatchRootIndex,
                    new MatchDirectoryIndexViews,
                    new MatchWildcardViewsThatCaptureMultipleSegments,
                    new MatchLiteralDirectories,
                    new MatchWildcardDirectories,
                    new MatchLiteralViews,
                    new MatchWildcardViews,
                ];
}

which would give the option of passing custom array to the router in the service provider:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Laravel\Folio\Folio;

class FolioServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        Folio::withPipeline([
                    new EnsureNoDirectoryTraversal,
                    new TransformModelBindings($request),
                    new SetMountPathOnMatchedView,
                    // ...
                    new \App\MatchPhpOrMarkdownRootIndex, // < --- custom class
                    new MatchDirectoryIndexViews,
                    new MatchWildcardViewsThatCaptureMultipleSegments,
                    new MatchLiteralDirectories,
                    new MatchWildcardDirectories,
                    new MatchLiteralViews,
                    new MatchWildcardViews,
        ])->route(resource_path('views/pages'), middleware: [
            '*' => [
                //
            ],
        ]);
    }
}

from folio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.