Code Monkey home page Code Monkey logo

Comments (2)

gmazzap avatar gmazzap commented on August 11, 2024

Hi @chrisvanpatten

Well, it is not a Cortex feature and I think it's out of the scope of a router to make middlewares available.

With a little bit of code you can use Cortex with external libraries that provide middlewares.

To give you some directions, I think you can create you custom route class that accepts middlewares.

For example:

namespace Chris;

class MiddlewareQueryRoute extends \Brain\Cortex\Route\QueryRoute {

   private $middlewares = [];

   public function __construct($path, $queryBuilder, array $options = []) {

        // store a "before" callback if it was already there
        $before = empty($options['before']) ? null : $options['before'];

        // then set before option to a function that 1st runs middlewares and
        // after that runs the "before" callback if there was one
        $options['before'] = function($vars, $wp, $template, $matches) use($before) {
             $this->runMiddlewares($vars, $wp, $template, $matches);
             is_callable($before) and $before($vars, $wp, $template, $matches);
         };

        parent::__construct($path, $queryBuilder, $options);
  }

   public function addMiddleware(callable $middleware) {
       $this->middlewares[] = $middlewares;
       return $this;
   }

  private function runMiddlewares($vars, $wp, $template, $matches) {
       array_walk(
           $this->middlewares,
           function(callable $middleware) use($vars, $wp, $template, $matches) {
               $middleware($vars, $wp, $template, $matches);
           }
       );
  }
}

Having this class in place, you can create an instance of your route instead of Cortex QueryRoute and use it to add middlewares, something like:

use Brain\Cortex\Route\RouteCollectionInterface as Routes;
use Chris\MiddlewareQueryRoute;

add_action('cortex.routes', function(Routes $routes) {

    $path = '{type:[a-z]+}/latest';

    $querybuilder = function(array $matches) {
           return [ 'post_type' => $matches['type'] ];
    };

    $before = function($vars, $wp, $template, $matches) {
          /* do something */
    };

    $route = new MiddlewareQueryRoute($path, $querybuilder, ['before' => $before])
           ->addMiddleware(function($vars, $wp, $template, $matches) {
                /* do something */ 
           })->addMiddleware(function($vars, $wp, $template, $matches) {
               /* do something */
           });

    $routes->addRoute($route);
});

The "before" callback, if given, will be called after all the middleware callables have been ran.

Note that move from a code like this to a more sophisticated middleware implementation that makes use of, for example, "Relay" or any other middleware library out there, will not be that hard.

from cortex.

chrisvanpatten avatar chrisvanpatten commented on August 11, 2024

Brilliant, thank you!

from cortex.

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.