Respect / Respect/Rest

Routines as events

Open
#82 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discussion
Dominant language
PHP
Stars
605
Forks
100
PR merge metrics
No merged PRs in 30d

Description

What if routines were events? Since they

At first purely cosmetic refactor imploring the use of a Dispatcher pattern instead of Abstract Routine doing the work.

<?php

namespace Respect\Rest\Routines;

class RoutineDispatcher
{
    private static $handlers = array();

    public static function addHandler(string $type, Routinable $routine)
    {
        if (!is_array(static::$handlers[$type]))
            static::$handlers[$type] = array();
        static::$handlers[$type][] = $routine;
    }

    public static function dispatch(string $type, string $method, array $args) {
        foreach (static::$handler[$type] as $routine)
            call_user_func_array(array($routine, $method), $args);
    }

}

class Request
{
    /** ...  */
    public function routineCall($type, $method, AbstractRoutinable $routine, &$routeParamsValues)
    {
        /** ... */
        $callbackParameters = $routeParamsValues;

        return RoutineDispatcher::dispatch($type, $method, array($this, $callbackParameters));
    }
}

class AbstractRoute
{
    public function appendRoutine(Routinable $routine)
    {
        RoutineDispatcher::addHandler(get_class($routine), $routine);
        return $this;
    }

}

Making things a tad more structured, understandable logical and already adding the benefit of multiple handlers per type which will allow for this:

$r3->get('/about', function() {
    return array('v' => 2.0);
})->accept(array(
    'text/html' => function($data) {
        list($k,$v)=each($data);
        return "<strong>$k</strong>: $v";
    }
))->accept(array(
    'application/json' => 'json_encode'
));

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The proposal centers on Request::routineCall, AbstractRoute::appendRoutine, and a new RoutineDispatcher, but it does not name repository files or tests. First inspect the existing routine and route implementations and discuss the incomplete design before coding. Done would require an agreed dispatcher design and verified support for multiple handlers per type.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
api, backend
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.