245 lines
7.6 KiB
PHP
245 lines
7.6 KiB
PHP
<?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;
|
|
|
|
/**
|
|
* The namespace to use to load the controller
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $controllerNamespace;
|
|
|
|
/**
|
|
* @var string|null The method used to handle the post phase; if requested
|
|
*/
|
|
protected $postMethod = null;
|
|
|
|
/**
|
|
* @var string The output to be returned to ShellWorker
|
|
*/
|
|
protected $output;
|
|
|
|
/**
|
|
* @var string The postOutput to be returned to ShellWorker
|
|
*/
|
|
protected $postOutput;
|
|
|
|
/**
|
|
* Input imported from the parent handler
|
|
*
|
|
* @var string|null
|
|
*/
|
|
protected $parentInput;
|
|
|
|
/**
|
|
* ControllerHandler constructor.
|
|
*
|
|
* Provides all information of which controller to use. Requests get redirected to that controller.
|
|
*
|
|
* @param string $controllerName The name of the controller to use
|
|
* @param string $controllerMethod The method to use for the task execution
|
|
* @param string|null $postMethod The method to use for the post execution
|
|
* @param string $controllerNamespace A potential custom namespace for the controller
|
|
*/
|
|
public function __construct(string $controllerName, string $controllerMethod, string $postMethod = null, string $controllerNamespace = '\Application\Controller\\')
|
|
{
|
|
$this->controllerName = $controllerName;
|
|
$this->controllerMethod = $controllerMethod;
|
|
$this->postMethod = $postMethod;
|
|
$this->controllerNamespace = $controllerNamespace;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function init(Task $task)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
* @throws TasksException
|
|
*/
|
|
public function primaryHandler(Task $task): bool
|
|
{
|
|
// Set the arguments
|
|
$args = $task->getArguments();
|
|
array_unshift($args, $task);
|
|
|
|
// First we fetch the controller
|
|
$controller = $this->getController();
|
|
|
|
// Check if method exists
|
|
if (!method_exists($controller, $this->controllerMethod))
|
|
throw new TasksException("Could not handle task. Method '$this->controllerMethod' not found on controller.");
|
|
|
|
if (!method_exists($controller, 'getTaskStatus'))
|
|
throw new TasksException("Could not handle task. Method 'getTaskStatus()' not found on controller, which is required.");
|
|
|
|
if ($this->parentInput !== null && method_exists($controller, 'setInput'))
|
|
$controller->setInput($this->parentInput);
|
|
|
|
// Call method and collect output
|
|
$this->output = call_user_func_array([$controller, $this->controllerMethod], $args);
|
|
$success = $controller->getTaskStatus();
|
|
if (!is_bool($success))
|
|
throw new TasksException("Could not determine whether task has succeeded. getTaskStatus() returned non-bool.");
|
|
|
|
return $success;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getOutput(): string
|
|
{
|
|
return $this->output;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
* @throws TasksException
|
|
*/
|
|
public function postHandler(Task $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();
|
|
|
|
// 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.");
|
|
|
|
if (!method_exists($controller, 'getTaskStatus'))
|
|
throw new TasksException("Could not handle task. Method 'getTaskStatus()' not found on controller, which is required.");
|
|
|
|
if ($this->parentInput !== null && method_exists($controller, 'setInput'))
|
|
$controller->setInput($this->parentInput);
|
|
|
|
// Call method and collect output
|
|
$this->postOutput = call_user_func_array([$controller, $this->postMethod], [$task]);
|
|
$success = $controller->getTaskStatus();
|
|
if (!is_bool($success))
|
|
throw new TasksException("Could not determine whether task has succeeded. getTaskStatus() returned non-bool.");
|
|
|
|
return $success;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getPostOutput(): string
|
|
{
|
|
return $this->postOutput;
|
|
}
|
|
|
|
/**
|
|
* @return Controller
|
|
* @throws TasksException
|
|
*/
|
|
private function getController(): Controller
|
|
{
|
|
// First load the controllers component
|
|
try {
|
|
/** @var Controllers $controllers */
|
|
$controllers = Factory::getInstance('controllers');
|
|
|
|
// Load the requested controller
|
|
return $controllers->get($this->controllerName, [], $this->controllerNamespace);
|
|
} 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.");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @var Handler
|
|
*/
|
|
private $parentHandler;
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function getParentHandler(): ?Handler
|
|
{
|
|
return $this->parentHandler;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function setParentHandler(Handler $parentHandler): void
|
|
{
|
|
$this->parentHandler = $parentHandler;
|
|
}
|
|
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function setParentInput(string $input): void
|
|
{
|
|
$this->parentInput = $input;
|
|
}
|
|
} |