middleware php - a simple micro-framework
TRANSCRIPT
About Middlewarefunction ($request, $response)
Something that does something with a Request and a Response
Why middleware is interesting?Service Oriented ArchitectureMicro Services
Something that we can change in couple of weeksetc. etc.
General principleWire components that interacts with the
request and the response$app = new MiddlewareRunner();$app>add(new Versioning());$app>add(new Router());$app>add(new Authentication());$app>add(new Options());$app>add(new Authorization());$app>add(new Accepts());$app>add(new ContentType());$app>add(new Parser());$app>add(new Params());$app>add(new Query());$app>add(new Body());$app>add(new Dispatcher());$app>add(new ProblemHandler());$app>run($request, $response);
FrankieA Frankenstein framework
https://github.com/wdalmut/frankie
Frankie is my third frameworkSimple-MVC
Push & Pull MVC
UpCloo Web FrameworkEvent-Driven framework with ZF components
FrankieMiddleware with different framework components
https://github.com/wdalmut/simple-mvc
https://github.com/wdalmut/upcloo-web-framework
https://github.com/wdalmut/frankie
Componentsacclimate/containerdoctrine/annotationsdoctrine/cachedoctrine/commonmnapoli/php-disymfony/routingsymfony/http-foundationsymfony/config
IdeasMiddeware approachComposition (DiC)Readability (Annotations)Not event-driven (state-machine approach)TDD & Code Design
Middlewareclass MyController public function index(Request $request, Response $response, ...$params)
CompositionVia acclimate (mixing containers)
$sfContainer = new DicBuilder();$loader = new XmlFileLoader( $sfContainer, new FileLocator(realpath(__DIR__ . '/../')));$loader>load(realpath(__DIR__ . '/../configs/services.xml'));$container>addContainer($acclimate>acclimate($sfContainer));//$conf = include __DIR__ . '/../configs/services.php';$serviceManager = new ServiceManager();
$serviceManagerConfigurator = new ServiceManagerConfig($conf["services"]);$serviceManagerConfigurator>configureServiceManager($serviceManager);
$serviceManager>setService("Config", $conf);
$container>addContainer($acclimate>acclimate($serviceManager));
Symfony2 DiC and Zend Framework ServiceManager together
Readability via annotations/** * @Route(name="/v1") */class User /** * @Route(name="/user/id", methods="GET") */ public function get(Request $request, Response $response, $id) //...
http://domain.tld/v1/user/12
State-MachineMiddle-out - Read the action in order to compose other steps
/** * @Route(name="/v1") * @Before(targetClass="One", targetMethod="one") * @After(targetClass="Two", targetMethod="two") * @After(targetClass="Five", targetMethod="five") */class User /** * @Route(name="/user/id", methods="GET") * @Before(targetClass="Three", targetMethod="three") * @After(targetClass="Four", targetMethod="four") */ public function get(Request $request, Response $response, $id)
State-Machine: (other objects can chain more steps)
One::one => Three::threeUser::getFour::four => Two::two => Five::five
Shortcuttingclass Auth public function basic(Request $request, Response $response) $auth = $request>headers>get("Authorization");
//...
if (!$isValid) $response>setStatusCode(401); return $response;
Or more readable
return new ApiProblem(401, "Unauthorized"); //ApiProblem extends Response
Discover more on:https://github.com/wdalmut/frankiehttp://frankie.readthedocs.org/en/develop/https://github.com/wdalmut/frankie-apihttps://github.com/wdalmut/frankie-app
About PHP and Middlewarehttps://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-
7.html