chg: [requestProcessor] Refactoring code organisation
parent
1729d4b6ed
commit
414ac9a59f
|
@ -14,7 +14,7 @@ interface GenericProcessorActionI
|
||||||
|
|
||||||
class GenericRequestProcessor
|
class GenericRequestProcessor
|
||||||
{
|
{
|
||||||
public $Inbox;
|
protected $Inbox;
|
||||||
protected $registeredActions = [];
|
protected $registeredActions = [];
|
||||||
protected $validator;
|
protected $validator;
|
||||||
private $processingTemplate = '/genericTemplates/confirm';
|
private $processingTemplate = '/genericTemplates/confirm';
|
||||||
|
@ -56,7 +56,7 @@ class GenericRequestProcessor
|
||||||
$request = $this->Inbox->newEmptyEntity();
|
$request = $this->Inbox->newEmptyEntity();
|
||||||
$request = $this->Inbox->patchEntity($request, $requestData);
|
$request = $this->Inbox->patchEntity($request, $requestData);
|
||||||
if ($request->getErrors()) {
|
if ($request->getErrors()) {
|
||||||
throw new MethodNotAllowed(__('Could not create request.{0}Reason: {1}', PHP_EOL, json_encode($request->getErrors())), 1);
|
throw new Exception(__('Could not create request.{0}Reason: {1}', PHP_EOL, json_encode($request->getErrors())), 1);
|
||||||
}
|
}
|
||||||
return $request;
|
return $request;
|
||||||
}
|
}
|
||||||
|
@ -103,6 +103,18 @@ class GenericRequestProcessor
|
||||||
}
|
}
|
||||||
|
|
||||||
public function create($requestData)
|
public function create($requestData)
|
||||||
|
{
|
||||||
|
$requestData['scope'] = $this->scope;
|
||||||
|
$requestData['action'] = $this->action;
|
||||||
|
$requestData['description'] = $this->description;
|
||||||
|
$request = $this->generateRequest($requestData);
|
||||||
|
$savedRequest = $this->Inbox->save($request);
|
||||||
|
if ($savedRequest !== false) {
|
||||||
|
// log here
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function discard($requestData)
|
||||||
{
|
{
|
||||||
$request = $this->generateRequest($requestData);
|
$request = $this->generateRequest($requestData);
|
||||||
$savedRequest = $this->Inbox->save($request);
|
$savedRequest = $this->Inbox->save($request);
|
||||||
|
|
|
@ -6,8 +6,8 @@ require_once(ROOT . DS . 'libraries' . DS . 'RequestProcessors' . DS . 'GenericR
|
||||||
class UserRequestProcessor extends GenericRequestProcessor
|
class UserRequestProcessor extends GenericRequestProcessor
|
||||||
{
|
{
|
||||||
protected $scope = 'User';
|
protected $scope = 'User';
|
||||||
protected $action = 'overridden-in-processor-action';
|
protected $action = 'not-specified'; //overriden when extending
|
||||||
protected $description = 'overridden-in-processor-action';
|
protected $description = ''; // overriden when extending
|
||||||
protected $registeredActions = [
|
protected $registeredActions = [
|
||||||
'Registration'
|
'Registration'
|
||||||
];
|
];
|
||||||
|
@ -18,15 +18,12 @@ class UserRequestProcessor extends GenericRequestProcessor
|
||||||
|
|
||||||
public function create($requestData)
|
public function create($requestData)
|
||||||
{
|
{
|
||||||
$requestData['scope'] = $this->scope;
|
|
||||||
$requestData['action'] = $this->action;
|
|
||||||
$requestData['description'] = $this->description;
|
|
||||||
parent::create($requestData);
|
parent::create($requestData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class RegistrationProcessor extends UserRequestProcessor implements GenericProcessorActionI {
|
class RegistrationProcessor extends UserRequestProcessor implements GenericProcessorActionI {
|
||||||
protected $action = 'Registration';
|
public $action = 'Registration';
|
||||||
protected $description;
|
protected $description;
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
|
|
|
@ -3,10 +3,11 @@
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Controller\AppController;
|
use App\Controller\AppController;
|
||||||
use Cake\Utility\Hash;
|
|
||||||
use Cake\Utility\Text;
|
|
||||||
use Cake\Database\Expression\QueryExpression;
|
use Cake\Database\Expression\QueryExpression;
|
||||||
use Cake\Event\EventInterface;
|
use Cake\Event\EventInterface;
|
||||||
|
use Cake\ORM\TableRegistry;
|
||||||
|
use Cake\Utility\Hash;
|
||||||
|
use Cake\Utility\Text;
|
||||||
use Cake\Http\Exception\NotFoundException;
|
use Cake\Http\Exception\NotFoundException;
|
||||||
use Cake\Http\Exception\MethodNotAllowedException;
|
use Cake\Http\Exception\MethodNotAllowedException;
|
||||||
use Cake\Http\Exception\ForbiddenException;
|
use Cake\Http\Exception\ForbiddenException;
|
||||||
|
@ -47,15 +48,6 @@ class InboxController extends AppController
|
||||||
$this->CRUD->filtering();
|
$this->CRUD->filtering();
|
||||||
}
|
}
|
||||||
|
|
||||||
// public function add()
|
|
||||||
// {
|
|
||||||
// $this->CRUD->add();
|
|
||||||
// $responsePayload = $this->CRUD->getResponsePayload();
|
|
||||||
// if (!empty($responsePayload)) {
|
|
||||||
// return $responsePayload;
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
public function view($id)
|
public function view($id)
|
||||||
{
|
{
|
||||||
$this->CRUD->view($id);
|
$this->CRUD->view($id);
|
||||||
|
@ -79,7 +71,8 @@ class InboxController extends AppController
|
||||||
$request = $this->Inbox->get($id);
|
$request = $this->Inbox->get($id);
|
||||||
$scope = $request->scope;
|
$scope = $request->scope;
|
||||||
$action = $request->action;
|
$action = $request->action;
|
||||||
$processor = $this->Inbox->getRequestProcessor($scope, $action);
|
$this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor');
|
||||||
|
$processor = $this->requestProcessor->getProcessor($scope, $action);
|
||||||
if ($this->request->is('post')) {
|
if ($this->request->is('post')) {
|
||||||
$processResult = $processor->process($id, $this->request);
|
$processResult = $processor->process($id, $this->request);
|
||||||
if ($processResult['success']) {
|
if ($processResult['success']) {
|
||||||
|
@ -91,11 +84,38 @@ class InboxController extends AppController
|
||||||
}
|
}
|
||||||
return $response;
|
return $response;
|
||||||
} else {
|
} else {
|
||||||
$processor->setViewVariables($this, $request);
|
$this->requestProcessor->render($this, $processor, $request);
|
||||||
$processingTemplate = $processor->getProcessingTemplate();
|
|
||||||
$this->set('request', $request);
|
|
||||||
$this->viewBuilder()->setLayout('ajax');
|
|
||||||
$this->render($processingTemplate);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function listProcessors()
|
||||||
|
{
|
||||||
|
$this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor');
|
||||||
|
$requestProcessors = $this->requestProcessor->listProcessors();
|
||||||
|
if ($this->ParamHandler->isRest()) {
|
||||||
|
return $this->RestResponse->viewData($requestProcessors, 'json');
|
||||||
|
}
|
||||||
|
$data = [];
|
||||||
|
foreach ($requestProcessors as $scope => $processors) {
|
||||||
|
foreach ($processors as $processor) {
|
||||||
|
$data[] = [
|
||||||
|
'scope' => $scope,
|
||||||
|
'action' => $processor->action
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$this->set('title', 'Available request processors');
|
||||||
|
$this->set('fields', [
|
||||||
|
[
|
||||||
|
'name' => 'Processor scope',
|
||||||
|
'data_path' => 'scope',
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'name' => 'Processor action',
|
||||||
|
'data_path' => 'action',
|
||||||
|
]
|
||||||
|
]);
|
||||||
|
$this->set('data', $data);
|
||||||
|
$this->render('/genericTemplates/index_simple');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -138,8 +138,8 @@ class UsersController extends AppController
|
||||||
|
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->Inbox = TableRegistry::getTableLocator()->get('Inbox');
|
$this->requestProcessor = TableRegistry::getTableLocator()->get('RequestProcessor');
|
||||||
$processor = $this->Inbox->getRequestProcessor('User', 'Registration');
|
$processor = $this->requestProcessor->getProcessor('User', 'Registration');
|
||||||
$data = [
|
$data = [
|
||||||
'origin' => '127.0.0.1',
|
'origin' => '127.0.0.1',
|
||||||
'comment' => 'Hi there!, please create an account',
|
'comment' => 'Hi there!, please create an account',
|
||||||
|
|
|
@ -4,7 +4,6 @@ namespace App\Model\Table;
|
||||||
use App\Model\Table\AppTable;
|
use App\Model\Table\AppTable;
|
||||||
use Cake\Database\Schema\TableSchemaInterface;
|
use Cake\Database\Schema\TableSchemaInterface;
|
||||||
use Cake\Database\Type;
|
use Cake\Database\Type;
|
||||||
use Cake\Filesystem\Folder;
|
|
||||||
use Cake\ORM\Table;
|
use Cake\ORM\Table;
|
||||||
use Cake\ORM\RulesChecker;
|
use Cake\ORM\RulesChecker;
|
||||||
use Cake\Validation\Validator;
|
use Cake\Validation\Validator;
|
||||||
|
@ -13,8 +12,6 @@ Type::map('json', 'Cake\Database\Type\JsonType');
|
||||||
|
|
||||||
class InboxTable extends AppTable
|
class InboxTable extends AppTable
|
||||||
{
|
{
|
||||||
private $processorsDirectory = ROOT . '/libraries/RequestProcessors';
|
|
||||||
private $requestProcessors;
|
|
||||||
|
|
||||||
public function initialize(array $config): void
|
public function initialize(array $config): void
|
||||||
{
|
{
|
||||||
|
@ -65,52 +62,4 @@ class InboxTable extends AppTable
|
||||||
|
|
||||||
return $rules;
|
return $rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getRequestProcessor($name, $action=null)
|
|
||||||
{
|
|
||||||
if (!isset($this->requestProcessors)) {
|
|
||||||
$this->loadRequestProcessors();
|
|
||||||
}
|
|
||||||
if (isset($this->requestProcessors[$name])) {
|
|
||||||
if (is_null($action)) {
|
|
||||||
return $this->requestProcessors[$name];
|
|
||||||
} else if (!empty($this->requestProcessors[$name]->{$action})) {
|
|
||||||
return $this->requestProcessors[$name]->{$action};
|
|
||||||
} else {
|
|
||||||
throw new \Exception(__('Processor {0}.{1} not found', $name, $action));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new \Exception(__('Processor not found'), 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private function loadRequestProcessors()
|
|
||||||
{
|
|
||||||
$processorDir = new Folder($this->processorsDirectory);
|
|
||||||
$processorFiles = $processorDir->find('.*RequestProcessor\.php', true);
|
|
||||||
foreach ($processorFiles as $processorFile) {
|
|
||||||
if ($processorFile == 'GenericRequestProcessor.php') {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
$processorMainClassName = str_replace('.php', '', $processorFile);
|
|
||||||
$processorMainClassNameShort = str_replace('RequestProcessor.php', '', $processorFile);
|
|
||||||
$processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName);
|
|
||||||
if ($processorMainClass !== false) {
|
|
||||||
$this->requestProcessors[$processorMainClassNameShort] = $processorMainClass;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function getProcessorClass($filePath, $processorMainClassName)
|
|
||||||
{
|
|
||||||
require_once($filePath);
|
|
||||||
$reflection = new \ReflectionClass($processorMainClassName);
|
|
||||||
$processorMainClass = $reflection->newInstance(true);
|
|
||||||
if ($processorMainClass->checkLoading() === 'Assimilation successful!') {
|
|
||||||
return $processorMainClass;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
} catch (Exception $e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,81 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Model\Table;
|
||||||
|
|
||||||
|
use App\Model\Table\AppTable;
|
||||||
|
use Cake\Filesystem\Folder;
|
||||||
|
|
||||||
|
class RequestProcessorTable extends AppTable
|
||||||
|
{
|
||||||
|
private $processorsDirectory = ROOT . '/libraries/RequestProcessors';
|
||||||
|
private $requestProcessors;
|
||||||
|
|
||||||
|
public function initialize(array $config): void
|
||||||
|
{
|
||||||
|
parent::initialize($config);
|
||||||
|
$this->loadProcessors();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getProcessor($scope, $action=null)
|
||||||
|
{
|
||||||
|
if (isset($this->requestProcessors[$scope])) {
|
||||||
|
if (is_null($action)) {
|
||||||
|
return $this->requestProcessors[$scope];
|
||||||
|
} else if (!empty($this->requestProcessors[$scope]->{$action})) {
|
||||||
|
return $this->requestProcessors[$scope]->{$action};
|
||||||
|
} else {
|
||||||
|
throw new \Exception(__('Processor {0}.{1} not found', $scope, $action));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw new \Exception(__('Processor not found'), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function render($controller, $processor, $request=[])
|
||||||
|
{
|
||||||
|
$processor->setViewVariables($controller, $request);
|
||||||
|
$controller->set('request', $request);
|
||||||
|
$controller->viewBuilder()->setLayout('ajax');
|
||||||
|
$processingTemplate = $processor->getProcessingTemplate();
|
||||||
|
$controller->render($processingTemplate);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function listProcessors($scope=null)
|
||||||
|
{
|
||||||
|
if (is_null($scope)) {
|
||||||
|
return $this->requestProcessors;
|
||||||
|
} else {
|
||||||
|
if (isset($this->requestProcessors[$scope])) {
|
||||||
|
return $this->requestProcessors[$scope];
|
||||||
|
} else {
|
||||||
|
throw new \Exception(__('Processors for {0} not found', $scope));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function loadProcessors()
|
||||||
|
{
|
||||||
|
$processorDir = new Folder($this->processorsDirectory);
|
||||||
|
$processorFiles = $processorDir->find('.*RequestProcessor\.php', true);
|
||||||
|
foreach ($processorFiles as $processorFile) {
|
||||||
|
if ($processorFile == 'GenericRequestProcessor.php') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$processorMainClassName = str_replace('.php', '', $processorFile);
|
||||||
|
$processorMainClassNameShort = str_replace('RequestProcessor.php', '', $processorFile);
|
||||||
|
$processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName);
|
||||||
|
if ($processorMainClass !== false) {
|
||||||
|
$this->requestProcessors[$processorMainClassNameShort] = $processorMainClass;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getProcessorClass($filePath, $processorMainClassName)
|
||||||
|
{
|
||||||
|
require_once($filePath);
|
||||||
|
$reflection = new \ReflectionClass($processorMainClassName);
|
||||||
|
$processorMainClass = $reflection->newInstance(true);
|
||||||
|
if ($processorMainClass->checkLoading() === 'Assimilation successful!') {
|
||||||
|
return $processorMainClass;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
echo $this->element('genericElements/IndexTable/index_table', [
|
||||||
|
'data' => [
|
||||||
|
'skip_pagination' => true,
|
||||||
|
'data' => !empty($data) ? $data : [],
|
||||||
|
'top_bar' => [],
|
||||||
|
'fields' => !empty($fields) ? $fields : [],
|
||||||
|
'title' => !empty($title) ? h($title) : __('Index'),
|
||||||
|
'description' => !empty($description) ? h($description) : '',
|
||||||
|
'actions' => !empty($actions) ? $actions : []
|
||||||
|
],
|
||||||
|
]);
|
Loading…
Reference in New Issue