Skip to content

Commit

Permalink
[REFACTOR] Move the debug functions to their own class
Browse files Browse the repository at this point in the history
  • Loading branch information
beromir committed Nov 24, 2021
1 parent 4d0448c commit e651e05
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 87 deletions.
95 changes: 8 additions & 87 deletions Classes/FusionObjects/Ray.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
114 changes: 114 additions & 0 deletions Classes/Service/RayService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Beromir\Ray\Service;

use Neos\Flow\Exception;

class RayService
{
private static $allowedDebugActions = ['nodetypename', 'context', 'contextpath', 'properties'];

private static $attachedRayActions = [];

/**
* @param $debugValue
* @param string $debugAction
* @param array $attachedRayActions
* @return void
*/
public static function rayDebug($debugValue, string $debugAction = '', array $attachedRayActions = []): void
{
$debugAction = strtolower($debugAction);

self::$attachedRayActions = $attachedRayActions;

if (is_string($debugAction) && in_array($debugAction, self::$allowedDebugActions)) {
self::debugNodes($debugValue, $debugAction);
} elseif (is_string($debugAction) && !empty($debugAction)) {
ray()->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));
}
}
}

0 comments on commit e651e05

Please sign in to comment.