From 285061c4c6864fcd3434e492d6febc1ec0e9cd67 Mon Sep 17 00:00:00 2001 From: mokaddem Date: Sat, 12 Jun 2021 12:09:54 +0200 Subject: [PATCH] chg: [requestProcessor] Improved processor collection for local tools processor --- src/Model/Table/RequestProcessorTable.php | 79 ++++++++++++++++++++--- 1 file changed, 70 insertions(+), 9 deletions(-) diff --git a/src/Model/Table/RequestProcessorTable.php b/src/Model/Table/RequestProcessorTable.php index e76fdab..d36e922 100644 --- a/src/Model/Table/RequestProcessorTable.php +++ b/src/Model/Table/RequestProcessorTable.php @@ -4,6 +4,13 @@ namespace App\Model\Table; use App\Model\Table\AppTable; use Cake\Filesystem\Folder; +use Cake\Http\Exception\MethodNotAllowedException; +use Cake\Core\Exception\Exception; + +class MissingRequestProcessorException extends Exception +{ + protected $_defaultCode = 404; +} class RequestProcessorTable extends AppTable { @@ -42,7 +49,19 @@ class RequestProcessorTable extends AppTable throw new \Exception(__('Processor {0}.{1} not found', $scope, $action)); } } - throw new \Exception(__('Processor not found'), 1); + throw new MissingRequestProcessorException(__('Processor not found')); + } + + public function getLocalToolProcessor($action, $toolName) + { + $scope = "LocalTool"; + $specificScope = "{$toolName}LocalTool"; + try { // try to get specific processor for module name or fall back to generic local tool processor + $processor = $this->getProcessor($specificScope, $action); + } catch (MissingRequestProcessorException $e) { + $processor = $this->getProcessor($scope, $action); + } + return $processor; } public function listProcessors($scope=null) @@ -53,7 +72,7 @@ class RequestProcessorTable extends AppTable if (isset($this->requestProcessors[$scope])) { return $this->requestProcessors[$scope]; } else { - throw new \Exception(__('Processors for {0} not found', $scope)); + throw new MissingRequestProcessorException(__('Processors for {0} not found', $scope)); } } } @@ -69,7 +88,7 @@ class RequestProcessorTable extends AppTable $processorMainClassName = str_replace('.php', '', $processorFile); $processorMainClassNameShort = str_replace('RequestProcessor.php', '', $processorFile); $processorMainClass = $this->getProcessorClass($processorDir->pwd() . DS . $processorFile, $processorMainClassName); - if ($processorMainClass !== false) { + if (is_object($processorMainClass)) { $this->requestProcessors[$processorMainClassNameShort] = $processorMainClass; foreach ($this->requestProcessors[$processorMainClassNameShort]->getRegisteredActions() as $registeredAction) { $scope = $this->requestProcessors[$processorMainClassNameShort]->getScope(); @@ -79,17 +98,59 @@ class RequestProcessorTable extends AppTable $this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = false; } } + } else { + $this->requestProcessors[$processorMainClassNameShort] = new \stdClass(); + $this->requestProcessors[$processorMainClassNameShort]->{$registeredAction} = new \stdClass(); + $this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->action = "N/A"; + $this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->enabled = false; + $this->requestProcessors[$processorMainClassNameShort]->{$registeredAction}->error = $processorMainClass; } } } - + + /** + * getProcessorClass + * + * @param string $filePath + * @param string $processorMainClassName + * @return object|string Object loading success, string containing the error if failure + */ private function getProcessorClass($filePath, $processorMainClassName) { - require_once($filePath); - $reflection = new \ReflectionClass($processorMainClassName); - $processorMainClass = $reflection->newInstance(true); - if ($processorMainClass->checkLoading() === 'Assimilation successful!') { - return $processorMainClass; + try { + require_once($filePath); + try { + $reflection = new \ReflectionClass($processorMainClassName); + } catch (\ReflectionException $e) { + return $e->getMessage(); + } + $processorMainClass = $reflection->newInstance(true); + if ($processorMainClass->checkLoading() === 'Assimilation successful!') { + return $processorMainClass; + } + } catch (Exception $e) { + return $e->getMessage(); } } + + /** + * createInboxEntry + * + * @param Object|Array $processor can either be the processor object or an array containing data to fetch it + * @param Array $data + * @return Array + */ + public function createInboxEntry($processor, $data) + { + if (!is_object($processor) && !is_array($processor)) { + throw new MethodNotAllowedException(__("Invalid processor passed")); + } + if (is_array($processor)) { + if (empty($processor['scope']) || empty($processor['action'])) { + throw new MethodNotAllowedException(__("Invalid data passed. Missing either `scope` or `action`")); + } + $processor = $this->getProcessor('User', 'Registration'); + } + return $processor->create($data); + } }