Started implementing ControllerHandler.

ControllerHandler is a standard utility handler for MVCR Controllers. This allows the user to create a task which is handled by a standardized controller.
Not finished yet! Needs some love.
This commit is contained in:
Abel Hoogeveen 2020-02-19 00:42:48 +01:00
parent b60ac20786
commit 820624e180
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
3 changed files with 178 additions and 2 deletions

View File

@ -1,7 +1,7 @@
FROM php:7.3-cli-buster
RUN apt-get update &&\
apt-get install --no-install-recommends --assume-yes --quiet procps ca-certificates curl git &&\
apt-get install --no-install-recommends --assume-yes --quiet procps ca-certificates curl git unzip &&\
rm -rf /var/lib/apt/lists/*
# Install Redis

View File

@ -19,7 +19,8 @@
"ext-redis": "*"
},
"require-dev": {
"fuzeworks/tracycomponent": "~1.2.0"
"fuzeworks/tracycomponent": "~1.2.0",
"fuzeworks/mvcr": "~1.2.0"
},
"autoload": {
"psr-4": {

View File

@ -0,0 +1,175 @@
<?php
/**
* FuzeWorks Async Library
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2020 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2020, TechFuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.0.0
*
* @version Version 1.0.0
*/
namespace FuzeWorks\Async\Handler;
use FuzeWorks\Async\Handler;
use FuzeWorks\Async\Task;
use FuzeWorks\Async\TasksException;
use FuzeWorks\Controller;
use FuzeWorks\Controllers;
use FuzeWorks\Exception\ControllerException;
use FuzeWorks\Exception\FactoryException;
use FuzeWorks\Exception\NotFoundException;
use FuzeWorks\Factory;
class ControllerHandler implements Handler
{
/**
* @var string Name of the controller used to handle the task
*/
protected $controllerName;
/**
* @var string The specific method to handle the task
*/
protected $controllerMethod;
/**
* @var string|null The method used to handle the post phase; if requested
*/
protected $postMethod = null;
protected $output;
protected $postOutput;
/**
* @inheritDoc
* @throws TasksException
*/
public function primaryHandler(Task $task): bool
{
// Set the arguments
$args = $this->setArguments($task);
// First we fetch the controller
$controller = $this->getController($this->controllerName);
// Check if method exists
if (!method_exists($controller, $this->controllerMethod))
throw new TasksException("Could not handle task. Method '$this->controllerMethod' not found on controller.");
// Call method and collect output
$this->output = $controller->{$this->controllerMethod}(...$args);
return true;
}
/**
* @inheritDoc
*/
public function getOutput()
{
return $this->output;
}
/**
* @inheritDoc
* @throws TasksException
*/
public function postHandler(Task $task)
{
// Set the arguments
$args = $this->setArguments($task);
// Abort if no postMethod exists
if (is_null($this->postMethod))
throw new TasksException("Could not handle task. No post method provided.");
// First we fetch the controller
$controller = $this->getController($this->controllerName);
// Check if method exists
if (!method_exists($controller, $this->postMethod))
throw new TasksException("Could not handle task. Post method '$this->postMethod' not found on controller.");
// Call method and collect output
$this->postOutput = $controller->{$this->postMethod}($task);
return true;
}
/**
* @inheritDoc
*/
public function getPostOutput()
{
return $this->postOutput;
}
/**
* Set the arguments of this handler using the provided task
*
* @param Task $task
* @return array
* @throws TasksException
*/
private function setArguments(Task $task): array
{
// Direct arguments
$args = $task->getArguments();
if (count($args) < 3)
throw new TasksException("Could not handle task. Not enough arguments provided.");
// First argument: controllerName
$this->controllerName = $args[0];
$this->controllerMethod = $args[1];
$this->postMethod = isset($args[2]) ? $args[2] : null;
return !isset($args[2]) ? [] : array_slice(func_get_args(), 3);
}
/**
* @param string $controllerName
* @return Controller
* @throws TasksException
*/
private function getController(string $controllerName): Controller
{
// First load the controllers component
try {
/** @var Controllers $controllers */
$controllers = Factory::getInstance('controllers');
// Load the requested controller
return $controllers->get($controllerName);
} catch (FactoryException $e) {
throw new TasksException("Could not get controller. FuzeWorks\MVCR is not installed!");
} catch (ControllerException $e) {
throw new TasksException("Could not get controller. Controller threw exception: '" . $e->getMessage() . "'");
} catch (NotFoundException $e) {
throw new TasksException("Could not get controller. Controller was not found.");
}
}
}