From e651e058642a37ffb0506cbff6d9025ab7d3bfe5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rei=C3=9Fig?= Date: Wed, 24 Nov 2021 10:46:26 +0100 Subject: [PATCH] [REFACTOR] Move the debug functions to their own class --- Classes/FusionObjects/Ray.php | 95 +++------------------------ Classes/Service/RayService.php | 114 +++++++++++++++++++++++++++++++++ 2 files changed, 122 insertions(+), 87 deletions(-) create mode 100644 Classes/Service/RayService.php diff --git a/Classes/FusionObjects/Ray.php b/Classes/FusionObjects/Ray.php index 1d118a4..4b9b808 100644 --- a/Classes/FusionObjects/Ray.php +++ b/Classes/FusionObjects/Ray.php @@ -2,6 +2,7 @@ namespace Beromir\Ray\FusionObjects; +use Beromir\Ray\Service\RayService; use Neos\Flow\Annotations as Flow; use Neos\Flow\Exception; use Neos\Fusion\FusionObjects\AbstractArrayFusionObject; @@ -16,104 +17,24 @@ class Ray extends AbstractArrayFusionObject */ protected $ignoreProperties = ['__meta', 'debugAction', 'value']; - protected $allowedDebugActions = ['nodetypename', 'context', 'contextpath', 'properties']; - /** * @return void */ public function evaluate(): void { - $debugAction = strtolower($this->fusionValue('debugAction')); + $debugAction = $this->fusionValue('debugAction'); $debugValue = $this->fusionValue('value'); - if (is_string($debugAction) && in_array($debugAction, $this->allowedDebugActions)) { - $this->debugNodes($debugValue, $debugAction); - } elseif (is_string($debugAction) && !empty($debugAction)) { - ray()->once('The debug action is not known.'); - } else { - $this->debug($debugValue); - } - } - - /** - * @param $debugValue - * - * @return void - */ - private function debug($debugValue): void - { - ray(function () use ($debugValue) { - - $debug = ray($debugValue); - - foreach (array_keys($this->properties) as $key) { - if (in_array($key, $this->ignoreProperties)) { - continue; - } + $attachedRayActions = []; - $value = $this->fusionValue($key); - - if (empty($value)) { - $debug = $debug->$key(); - } else { - $debug = $debug->$key($value); - } + foreach (array_keys($this->properties) as $key) { + if (in_array($key, $this->ignoreProperties)) { + continue; } - return $debug; - }); - } - /** - * @param $debugValue - * @param string $debugAction - * - */ - private function getNodeData($debugValue, string $debugAction) - { - switch ($debugAction) { - case strtolower('nodeTypeName'): - try { - return $debugValue->getNodeTypeName(); - } catch (Exception $e) { - ray()->exception($e); - return null; - } - case strtolower('context'): - return $debugValue->getContext(); - case strtolower('contextPath'): - return $debugValue->getContextPath(); - case strtolower('properties'): - try { - return $debugValue->getProperties(); - } catch (Exception $e) { - ray()->exception($e); - return null; - } - default: - return null; + $attachedRayActions[$key] = $this->fusionValue($key); } - } - /** - * @param $debugValues - * @param string $debugAction - * - * @return void - */ - private function debugNodes($debugValues, string $debugAction): void - { - if (is_array($debugValues)) { - $debugArray = []; - - foreach ($debugValues as $debugValue) { - if (is_object($debugValue) && (get_class($debugValue) === 'Neos\ContentRepository\Domain\Model\Node')) { - array_push($debugArray, $this->getNodeData($debugValue, $debugAction)); - } - } - - if (!empty($debugArray)) $this->debug($debugArray); - } elseif (is_object($debugValues) && (get_class($debugValues) === 'Neos\ContentRepository\Domain\Model\Node')) { - $this->debug($this->getNodeData($debugValues, $debugAction)); - } + RayService::rayDebug($debugValue, $debugAction, $attachedRayActions); } } diff --git a/Classes/Service/RayService.php b/Classes/Service/RayService.php new file mode 100644 index 0000000..fb23612 --- /dev/null +++ b/Classes/Service/RayService.php @@ -0,0 +1,114 @@ +once('The debug action is not known.'); + } else { + self::debug($debugValue); + } + } + + /** + * @param $debugValue + * + * @return void + */ + private static function debug($debugValue): void + { + if (empty(self::$attachedRayActions)) { + ray($debugValue); + } else { + ray(function () use ($debugValue) { + + $debug = ray($debugValue); + + foreach (self::$attachedRayActions as $key => $value) { + + if (empty($value)) { + $debug = $debug->$key(); + } else { + $debug = $debug->$key($value); + } + } + return $debug; + }); + } + } + + /** + * @param $debugValue + * @param string $debugAction + * + */ + private static function getNodeData($debugValue, string $debugAction) + { + switch ($debugAction) { + case strtolower('nodeTypeName'): + try { + return $debugValue->getNodeTypeName(); + } catch (Exception $e) { + ray()->exception($e); + return null; + } + case strtolower('context'): + return $debugValue->getContext(); + case strtolower('contextPath'): + return $debugValue->getContextPath(); + case strtolower('properties'): + try { + return $debugValue->getProperties(); + } catch (Exception $e) { + ray()->exception($e); + return null; + } + default: + return null; + } + } + + /** + * @param $debugValues + * @param string $debugAction + * + * @return void + */ + private static function debugNodes($debugValues, string $debugAction): void + { + if (is_array($debugValues)) { + $debugArray = []; + + foreach ($debugValues as $debugValue) { + if (is_object($debugValue) && (get_class($debugValue) === 'Neos\ContentRepository\Domain\Model\Node')) { + array_push($debugArray, self::getNodeData($debugValue, $debugAction)); + } + } + + if (!empty($debugArray)) self::debug($debugArray); + } elseif (is_object($debugValues) && (get_class($debugValues) === 'Neos\ContentRepository\Domain\Model\Node')) { + self::debug(self::getNodeData($debugValues, $debugAction)); + } + } +}