All Projects → aolarchive → atc

aolarchive / atc

Licence: MIT license
An Action-Based PHP Dispatching Library

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to atc

Loafer
Asynchronous message dispatcher - Currently using asyncio and amazon SQS
Stars: ✭ 104 (+593.33%)
Mutual labels:  dispatcher
openvpn-shapeshifter
This script will automatically guide you to install and configure your OpenVPN server with Shapeshifter Dispatcher (obfuscation) which will allow you to bypass the DPI blockage on OpenVPN. This setup will offer the users the freedom to choose between regular OpenVPN connection or obfuscated one, they actually can use both! OpenVPN is the VPN pro…
Stars: ✭ 59 (+293.33%)
Mutual labels:  dispatcher
lamp-luwak
Service-oriented state management for React
Stars: ✭ 12 (-20%)
Mutual labels:  dispatcher
Vim Clap
👏 Modern performant fuzzy picker for Vim and NeoVim
Stars: ✭ 1,802 (+11913.33%)
Mutual labels:  dispatcher
Easyrouter
A component routing framework simple, stable and high-performance, which supports UI, Method Calls, Interceptors, Callbacks and More than these.
Stars: ✭ 172 (+1046.67%)
Mutual labels:  dispatcher
atomic-state
Atomic State is a state management library for React
Stars: ✭ 15 (+0%)
Mutual labels:  dispatcher
Fluxxan
Fluxxan is an Android implementation of the Flux Architecture that combines concepts from both Fluxxor and Redux.
Stars: ✭ 80 (+433.33%)
Mutual labels:  dispatcher
http-multiserver.cr
Mount multiple web applications 🚦
Stars: ✭ 23 (+53.33%)
Mutual labels:  dispatcher
wasmcloud-otp
wasmCloud host runtime that leverages Elixir/OTP and Rust to provide simple, secure, distributed application development using the actor model
Stars: ✭ 197 (+1213.33%)
Mutual labels:  dispatcher
cqrs
A foundational package for Command Query Responsibility Segregation (CQRS) compatible with Laravel.
Stars: ✭ 37 (+146.67%)
Mutual labels:  dispatcher
Noel
A universal, human-centric, replayable javascript event emitter.
Stars: ✭ 158 (+953.33%)
Mutual labels:  dispatcher
General Store
Simple, flexible store implementation for Flux. #hubspot-open-source
Stars: ✭ 171 (+1040%)
Mutual labels:  dispatcher
flux-redux
An application implementing Flux and Redux with few other dependencies
Stars: ✭ 24 (+60%)
Mutual labels:  dispatcher
Ml Model Ci
MLModelCI is a complete MLOps platform for managing, converting, profiling, and deploying MLaaS (Machine Learning-as-a-Service), bridging the gap between current ML training and serving systems.
Stars: ✭ 122 (+713.33%)
Mutual labels:  dispatcher
broker
Dead simple PSR-15 middleware dispatcher
Stars: ✭ 26 (+73.33%)
Mutual labels:  dispatcher
Middleman
Dead simple PSR-15 / PSR-7 middleware dispatcher
Stars: ✭ 87 (+480%)
Mutual labels:  dispatcher
burp-aem-scanner
Burp Scanner extension to fingerprint and actively scan instances of the Adobe Experience Manager CMS. It checks the website for common misconfigurations and security holes.
Stars: ✭ 60 (+300%)
Mutual labels:  dispatcher
middleware
🐳 A PSR15 middleware dispatcher
Stars: ✭ 15 (+0%)
Mutual labels:  dispatcher
jobor
支持秒级分布式定时任务系统, A high performance distributed task scheduling system, Support multi protocol scheduling tasks
Stars: ✭ 52 (+246.67%)
Mutual labels:  dispatcher
aem-dispatcher-experiments
Experiments to demonstrate the impact of the Dispatcher and it's configuration parameters.
Stars: ✭ 41 (+173.33%)
Mutual labels:  dispatcher

ATC, An Action-Based PHP Dispatching Library

Build Status Latest Stable Version Latest Unstable Version Total Downloads Code Climate

ATC is a small dispatching library for PHP built on Aura.Router package and Symfony's HttpFoundation and EventDispatcher. There are two things you should know about this library:

  1. Every single route matches to a single Action class.
  2. Exceptions thrown from the Action can implement the ActionInterface and become the new Action.

"What's an action?" You can think of an Action as a Controller with a single method. Rather than being responsible for many different routes/pages it is only responsible for a single route.

Usage

The simplest possible way to use ATC is by just setting up a route and a corresponding action. Lets do a simple hello world for our home page with an Action called Index.

$router->addGet('Index', '/');
namespace Your\Namespace\Prefix;

class Index extends \Aol\Atc\Action
{
    public function __invoke(Request $request)
    {
        return Response::create('Hello world');
    }
}

Now any requests for the home page / will match the Index Action and send a "Hello world" back to the browser. Remember, this is just using Aura.Router so its pretty easy to build complex paths with named parameters.

$router->addGet('Index', '/{name}/');
class Index extends \Aol\Atc\Action
{
    public function __invoke(Request $request)
    {
        return Response::create('Hello ' . $this->params['name']);
    }
}

Leveraging the presenter

While the method above would be great for simple API responses often you need to be able to do more complex things with templating libraries. ATC will always evaluate the return value of your Action and if it is a Symfony response object it will just send it straight to the browser. If it is not a response object it will take that data and hand it off to the Presenter to handle formatting.

There is an interface for the Presenter class - which makes it very easy to drop in your templating package of choice - but out of the box ATC will handle JSON responses and basic PHP templates. By default it will render the HTML template, but if the request header for the content type is set to application/json it will send the json encoded version of your action response. You can always lock an Action down to a single response type by setting the $allowed_formats property.

class Index extends \Aol\Atc\Action
{
    protected $allowed_formats = ['text/html'];
    protected $view = 'index';

    public function __invoke(Request $request)
    {
        return ['name' => $this->params['name']];
    }
}
<!-- file: your/view/dir/index.php -->
Hello <?=$data['name']?>

Exceptions can be Actions too

Any exception thrown from an ATC Action can immediately replace the current Action as long as implements the ActionInterface. Yep, you read that correctly, exceptions can be actions too. For example, if you throw the NotAuthorizedException from your Action the dispatcher will verify the exception implements the ActionInterface and then redispatch the request using the exception action. In this case it will respond with a 401 HTTP response code and look for a errors/401 template to use in the presenter.

class Index extends \Aol\Atc\Action
{
    public function __invoke(Request $request)
    {
        throw new \Aol\Atc\Exceptions\NotAuthorizedException;
    }
}

You could also create custom exceptions within your own application. For example you could have a NotSignedInException that returns a RedirectResponse to the signin page:

class NotSignedInException extends \Aol\Atc\Exception
{
    public function __invoke(Request $request)
    {
        return new RedirectResponse('/signin/');
    }
}

The flexibility is unparalleled and the possibilities are endless.

Setup

The dispatch class itself can be instantiated with just a few dependencies.

$router = (new \Aura\Router\RouterFactory())->newInstance();
$request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
$action_factory = new \Aol\Atc\ActionFactory('Your\\Namespace\\Prefix\\');
$presenter = new \Aol\Atc\Presenter(__DIR__ . '/your/view/dir/');
$event_dispatcher = new \Aol\Atc\EventDispatcher;
$exception_handler = new \Aol\Atc\EventHandlers\DispatchErrorHandler;

$dispatch = new \Aol\Atc\Dispatch(
    $router,
    $request,
    $action_factory,
    $presenter,
    $event_dispatcher,
    $exception_handler
);

$response = $dispatch->run(); // Returns a symfony response object
$response->send();

Installing via Composer

ATC supports PHP 5.4 or above. The recommended way to install ATC is through Composer.

# Install Composer
curl -sS https://getcomposer.org/installer | php

Next, run the Composer command to install the latest stable version of ATC:

composer require aol/atc

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

FAQ

WTF is "ATC"? It originally stood for "Air Traffic Control" but that's a lot of typing and doesn't roll off the tongue very well.

How do I inject dependencies into my Action classes? The included ActionFactory is just the bare bones, but you can inject your own factory into the Dispatcher as long as it implements the ActionFactoryInterface.

What about Twig/Smarty/Blade/etc? Like the ActionFactory you can inject your own presenter class into the dispatcher as well as long as it implements the PresenterInterface. There are a lot of possibilities here so if you do something cool let us know!

I think I found a bug, now what? Please, open up an issue! Make sure you tell us what you expected, what happened instead, and include just enough code so that we can reproduce the issue.

Will you add feature X? Maybe, but you'll never know until you ask. Open up an issue and we'll discuss it and if you're interested in submitting a pull request check out the Contributing section below.

Contributing

ATC is an open source project and pull requests are welcome if you'd like to contribute. Please include full unit test coverage and any relevant documentation updates with your PR.

License

ATC is licensed under the MIT License - see the LICENSE file for details

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].