Skip to content
This repository has been archived by the owner on Jan 4, 2022. It is now read-only.

Commit

Permalink
Adjusted processor to use key as default handler
Browse files Browse the repository at this point in the history
  • Loading branch information
erik-perri committed Jul 17, 2019
1 parent 3f6ff94 commit 06a96dc
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 55 deletions.
1 change: 0 additions & 1 deletion Observer/Action/DesignConfigSaveObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function handle(Http $request)
'message' => 'Design configuration scope {design-config} modified.',
'context' => [
'design-config' => [
'handler' => 'design-config',
'text' => $scope,
'id' => $scopeId,
],
Expand Down
5 changes: 1 addition & 4 deletions Observer/Action/SystemConfigSaveObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ public function handle(Http $request)
'group' => 'admin',
'message' => 'Configuration section {config-section} modified.',
'context' => [
'config-section' => [
'handler' => 'config-section',
'text' => $section,
],
'config-section' => $section,
'store-view' => $this->storeViewFinder->getActiveStoreView(),
],
]);
Expand Down
1 change: 0 additions & 1 deletion Observer/Model/CategoryModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function handle(AbstractModel $entity, string $action)
'message' => 'Category {category} {action}.',
'context' => [
'category' => [
'handler' => 'category',
'text' => $entity->getData('name'),
'id' => $entity->getId(),
],
Expand Down
1 change: 0 additions & 1 deletion Observer/Model/CmsBlockModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function handle(AbstractModel $entity, string $action)
'message' => 'Content block {cms-block} {action}.',
'context' => [
'cms-block' => [
'handler' => 'cms-block',
'text' => $entity->getData('title'),
'id' => $entity->getId(),
],
Expand Down
1 change: 0 additions & 1 deletion Observer/Model/CmsPageModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function handle(AbstractModel $entity, string $action)
'message' => 'Page {cms-page} {action}.',
'context' => [
'cms-page' => [
'handler' => 'cms-page',
'text' => $entity->getData('title'),
'id' => $entity->getId(),
],
Expand Down
1 change: 0 additions & 1 deletion Observer/Model/CustomerGroupModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function handle(AbstractModel $entity, string $action)
'message' => 'Customer group {group} {action}.',
'context' => [
'group' => [
'handler' => 'group',
'text' => trim($entity->getData('code')),
'id' => (string)$entity->getId(),
],
Expand Down
1 change: 0 additions & 1 deletion Observer/Model/CustomerModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function handle(AbstractModel $entity, string $action)
'message' => 'Customer {customer} {action}.',
'context' => [
'customer' => [
'handler' => 'customer',
'text' => trim(sprintf('%s %s', $entity->getData('firstname'), $entity->getData('lastname'))),
'id' => (string)$entity->getId(),
],
Expand Down
1 change: 0 additions & 1 deletion Observer/Model/ProductModelObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected function handle(AbstractModel $entity, string $action)
'message' => 'Product {product} {action}.',
'context' => [
'product' => [
'handler' => 'product',
'text' => $entity->getData('sku'),
],
'action' => $action,
Expand Down
126 changes: 90 additions & 36 deletions Placeholder/PlaceholderProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Magento\Framework\DataObjectFactory;
use Ryvon\EventLog\Placeholder\Handler\HandlerInterface;

/**
* Replaces placeholders in the message string.
*/
class PlaceholderProcessor
{
/**
Expand All @@ -21,76 +24,127 @@ class PlaceholderProcessor
/**
* @var Handler\HandlerInterface[]
*/
private $handlers = [];
private $handlers;

/**
* @param DataObjectFactory $dataObjectFactory
* @param array $handlers
*/
public function __construct(DataObjectFactory $dataObjectFactory, $handlers = [])
{
public function __construct(
DataObjectFactory $dataObjectFactory,
$handlers = []
) {
$this->dataObjectFactory = $dataObjectFactory;
$this->handlers = $handlers;
}

/**
* Returns the string to use when a placeholder is not able to be replaced.
*
* @return string
*/
public function getUnknownText(): string
{
return $this->unknownText;
return htmlentities($this->unknownText);
}

/**
* Replaces any placeholders in the message.
*
* @param string $message
* @param DataObject $context
* @param bool $plainText
* @param DataObject|array $context
* @param bool $withoutHandlers
* @return string
*/
public function process(string $message, DataObject $context, bool $plainText = false): string
public function process(string $message, $context, bool $withoutHandlers = false): string
{
if (!($context instanceof DataObject)) {
$context = $this->dataObjectFactory->create(['data' => $context]);
}

$message = htmlentities($message, ENT_QUOTES);

return preg_replace_callback('#\{([^}]+)\}#', function ($matches) use ($context, $plainText) {
return preg_replace_callback('#\{([^}]+)\}#', function ($matches) use ($context, $withoutHandlers) {
$placeholderKey = $matches[1];

$placeholderContext = $context->getData($placeholderKey);
if (!is_array($placeholderContext)) {
if (!$this->canBeString($placeholderContext)) {
return $this->unknownText;
}
$placeholderValue = $context->getData($placeholderKey);
if ($placeholderValue === null) {
return $this->getUnknownText();
}

$placeholderContext = ['text' => (string)$placeholderContext];
$placeholderDataObject = $this->createPlaceholderContext($placeholderKey, $placeholderValue);

switch($placeholderKey) {
case 'user-ip':
$placeholderContext['handler'] = 'ip';
break;
}
}
return $this->handle($placeholderDataObject, $withoutHandlers);
}, $message);
}

$plainTextVersion = $placeholderContext['text'] ?? $this->unknownText;
if (!$this->canBeString($plainTextVersion)) {
return $this->unknownText;
}
/**
* Returns the replacement string for the specified placeholder key using the specified context.
*
* @param DataObject|array $context
* @param bool $withoutHandlers
* @return string
*/
private function handle($context, bool $withoutHandlers = false): string
{
if (!($context instanceof DataObject)) {
$context = $this->dataObjectFactory->create(['data' => $context]);
}

$placeholderHandlerId = $placeholderContext['handler'] ?? null;
$placeholderHandler = $placeholderHandlerId ? ($this->handlers[$placeholderHandlerId] ?? null) : null;
if ($plainText || !$placeholderHandler || !($placeholderHandler instanceof HandlerInterface)) {
return htmlentities($plainTextVersion);
}
$plainTextVersion = $context->getData('text');
if ($plainTextVersion === null || !$this->canBeString($plainTextVersion)) {
return $this->getUnknownText();
}

$value = $placeholderHandler->handle($this->dataObjectFactory->create(['data' => $placeholderContext]));
if ($value === null) {
return htmlentities($plainTextVersion);
}
$plainTextVersion = htmlentities((string)$plainTextVersion);

return $value;
}, $message);
$placeholderHandlerId = $context->getData('handler');
$placeholderHandler = $placeholderHandlerId ? ($this->handlers[$placeholderHandlerId] ?? null) : null;
if ($withoutHandlers || !$placeholderHandler || !($placeholderHandler instanceof HandlerInterface)) {
return $plainTextVersion;
}

$value = $placeholderHandler->handle($context);
if ($value === null) {
return $plainTextVersion;
}

return $value;
}

/**
* Creates the placeholder context.
*
* If the value is not an array it is set as the context's 'text' value. If no handler is specified the placeholder
* key is used.
*
* @param string $placeholderKey
* @param string|array $placeholderValue
* @return DataObject
*/
private function createPlaceholderContext(string $placeholderKey, $placeholderValue): DataObject
{
if (!is_array($placeholderValue)) {
$placeholderContext = [
'text' => $placeholderValue,
];
} else {
$placeholderContext = $placeholderValue;
}

if (!array_key_exists('handler', $placeholderContext)) {
$placeholderContext['handler'] = $placeholderKey;
}

return $this->dataObjectFactory->create([
'data' => $placeholderContext,
]);
}

/**
* @param $value
* Checks if the value can be properly converted to a plain string.
*
* @param mixed $value
* @return bool
*/
private function canBeString($value): bool
Expand Down
2 changes: 0 additions & 2 deletions Plugin/AdminRolePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function aroundSave(
'message' => 'Admin role {role} {action}.',
'context' => [
'role' => [
'handler' => 'role',
'text' => $subject->getData('name'),
'id' => (string)$subject->getId(),
],
Expand All @@ -67,7 +66,6 @@ public function beforeDelete(
'message' => 'Admin role {role} {action}.',
'context' => [
'role' => [
'handler' => 'role',
'text' => $subject->getData('role_name'),
'id' => (string)$subject->getId(),
],
Expand Down
2 changes: 0 additions & 2 deletions Plugin/AdminUserPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function aroundSave(
'message' => 'Admin user {user} {action}.',
'context' => [
'user' => [
'handler' => 'user',
'text' => $subject->getData('username'),
'id' => (string)$subject->getId(),
],
Expand Down Expand Up @@ -102,7 +101,6 @@ public function beforeDelete(
'message' => 'Admin user {user} {action}.',
'context' => [
'user' => [
'handler' => 'user',
'text' => $subject->getData('username'),
'id' => (string)$subject->getId(),
],
Expand Down
2 changes: 0 additions & 2 deletions Plugin/AttributePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function aroundSave(
'message' => 'Attribute {attribute} {action}.',
'context' => [
'attribute' => [
'handler' => 'attribute',
'text' => $object->getData('attribute_code'),
'id' => $object->getData('attribute_id'),
],
Expand Down Expand Up @@ -86,7 +85,6 @@ public function beforeDelete(
'message' => 'Attribute {attribute} {action}.',
'context' => [
'attribute' => [
'handler' => 'attribute',
'text' => $object->getData('attribute_code'),
'id' => $object->getData('attribute_id'),
],
Expand Down
2 changes: 0 additions & 2 deletions Plugin/AttributeSetPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ public function aroundSave(
'message' => 'Attribute Set {attribute-set} {action}.',
'context' => [
'attribute-set' => [
'handler' => 'attribute-set',
'text' => $object->getData('attribute_set_name'),
'id' => $id,
],
Expand Down Expand Up @@ -85,7 +84,6 @@ public function beforeDelete(
'message' => 'Attribute Set {attribute-set} {action}.',
'context' => [
'attribute-set' => [
'handler' => 'attribute-set',
'text' => $object->getData('attribute_set_name'),
'id' => $object->getData('attribute_set_id'),
],
Expand Down

0 comments on commit 06a96dc

Please sign in to comment.