Skip to content

Commit

Permalink
Merge pull request #25 from kliker02/at/namespace-feature-bc
Browse files Browse the repository at this point in the history
Process multiple namespaces for class names and configuration
  • Loading branch information
Ocramius authored Jun 27, 2022
2 parents f68c904 + b74a1c5 commit 86bb1c6
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 32 deletions.
116 changes: 95 additions & 21 deletions src/View/Helper/FlashMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ class FlashMessenger extends AbstractHelper
*/
protected $pluginFlashMessenger;

/**
* All namespaces of FlashMessenger
*
* Keys of array are the namespace names
*
* @var array<string, FlashMessengerNamespace> $namespaces
*/
private array $namespaces = [];

/**
* @param array<string, FlashMessengerNamespace> $namespaces
*/
public function __construct(array $namespaces = [])
{
foreach ($namespaces as $namespace) {
$this->namespaces[$namespace->getName()] = $namespace;
}
}

/**
* Returns the flash messenger plugin controller
*
Expand Down Expand Up @@ -169,12 +188,7 @@ protected function renderMessages(

// Prepare classes for opening tag
if (empty($classes)) {
if (isset($this->classMessages[$namespace])) {
$classes = $this->classMessages[$namespace];
} else {
$classes = $this->classMessages['default'];
}
$classes = [$classes];
$classes = [$this->getClasses($namespace)];
}

$autoEscape ??= $this->autoEscape;
Expand Down Expand Up @@ -208,12 +222,12 @@ function ($item) use (&$messagesToPrint, $escapeHtml, $autoEscape, $translator,
}

// Generate markup
$markup = sprintf($this->getMessageOpenFormat(), ' class="' . implode(' ', $classes) . '"');
$markup = sprintf($this->getMessageOpenFormat($namespace), ' class="' . implode(' ', $classes) . '"');
$markup .= implode(
sprintf($this->getMessageSeparatorString(), ' class="' . implode(' ', $classes) . '"'),
sprintf($this->getMessageSeparatorString($namespace), ' class="' . implode(' ', $classes) . '"'),
$messagesToPrint
);
$markup .= $this->getMessageCloseString();
$markup .= $this->getMessageCloseString($namespace);
return $markup;
}

Expand Down Expand Up @@ -245,9 +259,16 @@ public function getAutoEscape()
* @param string $messageCloseString
* @return FlashMessenger
*/
public function setMessageCloseString($messageCloseString)
public function setMessageCloseString($messageCloseString, ?string $namespaceName = null)
{
$this->messageCloseString = (string) $messageCloseString;
$namespaceName = $namespaceName ?? $this->getDefaultNamespaceName();
$namespace = $this->getNamespace($namespaceName);
if ($namespace === null) {
$this->messageCloseString = $messageCloseString;
} else {
$namespace->setMessageCloseString($messageCloseString);
}

return $this;
}

Expand All @@ -256,9 +277,15 @@ public function setMessageCloseString($messageCloseString)
*
* @return string
*/
public function getMessageCloseString()
public function getMessageCloseString(?string $namespaceName = null)
{
return $this->messageCloseString;
$namespaceName = $namespaceName ?? $this->getDefaultNamespaceName();
$namespace = $this->getNamespace($namespaceName);
if ($namespace === null) {
return $this->messageCloseString;
}

return $namespace->getMessageCloseString();
}

/**
Expand All @@ -267,9 +294,15 @@ public function getMessageCloseString()
* @param string $messageOpenFormat
* @return FlashMessenger
*/
public function setMessageOpenFormat($messageOpenFormat)
public function setMessageOpenFormat($messageOpenFormat, ?string $namespaceName = null)
{
$this->messageOpenFormat = (string) $messageOpenFormat;
$namespaceName = $namespaceName ?? $this->getDefaultNamespaceName();
$namespace = $this->getNamespace($namespaceName);
if ($namespace === null) {
$this->messageOpenFormat = $messageOpenFormat;
} else {
$namespace->setMessageOpenFormat($messageOpenFormat);
}
return $this;
}

Expand All @@ -278,9 +311,15 @@ public function setMessageOpenFormat($messageOpenFormat)
*
* @return string
*/
public function getMessageOpenFormat()
public function getMessageOpenFormat(?string $namespaceName = null)
{
return $this->messageOpenFormat;
$namespaceName = $namespaceName ?? $this->getDefaultNamespaceName();
$namespace = $this->getNamespace($namespaceName);
if ($namespace === null) {
return $this->messageOpenFormat;
}

return $namespace->getMessageOpenFormat();
}

/**
Expand All @@ -289,9 +328,15 @@ public function getMessageOpenFormat()
* @param string $messageSeparatorString
* @return FlashMessenger
*/
public function setMessageSeparatorString($messageSeparatorString)
public function setMessageSeparatorString($messageSeparatorString, ?string $namespaceName = null)
{
$this->messageSeparatorString = (string) $messageSeparatorString;
$namespaceName = $namespaceName ?? $this->getDefaultNamespaceName();
$namespace = $this->getNamespace($namespaceName);
if ($namespace === null) {
$this->messageSeparatorString = $messageSeparatorString;
} else {
$namespace->setMessageSeparatorString($messageSeparatorString);
}
return $this;
}

Expand All @@ -300,9 +345,15 @@ public function setMessageSeparatorString($messageSeparatorString)
*
* @return string
*/
public function getMessageSeparatorString()
public function getMessageSeparatorString(?string $namespaceName = null)
{
return $this->messageSeparatorString;
$namespaceName = $namespaceName ?? $this->getDefaultNamespaceName();
$namespace = $this->getNamespace($namespaceName);
if ($namespace === null) {
return $this->messageSeparatorString;
}

return $namespace->getMessageSeparatorString();
}

/**
Expand Down Expand Up @@ -369,4 +420,27 @@ protected function getEscapeHtmlHelper()

return $this->escapeHtmlHelper;
}

private function getNamespace(string $namespace): ?FlashMessengerNamespace
{
return $this->namespaces[$namespace] ?? null;
}

private function getClasses(string $namespaceName): string
{
$namespace = $this->getNamespace($namespaceName);
if ($namespace === null) {
return $this->classMessages[$namespaceName] ?? '';
}

return $namespace->getClasses();
}

/**
* Returns default namespace name
*/
private function getDefaultNamespaceName(): string
{
return PluginFlashMessenger::NAMESPACE_DEFAULT;
}
}
102 changes: 91 additions & 11 deletions src/View/Helper/FlashMessengerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Laminas\ServiceManager\Factory\FactoryInterface;
use Laminas\ServiceManager\ServiceLocatorInterface;

use function is_array;
use function method_exists;

class FlashMessengerFactory implements FactoryInterface
Expand All @@ -25,29 +26,108 @@ public function __invoke(ContainerInterface $container, $name, ?array $options =
if (! method_exists($container, 'configure')) {
$container = $container->getServiceLocator();
}
$helper = new FlashMessenger();
$controllerPluginManager = $container->get('ControllerPluginManager');
$flashMessenger = $controllerPluginManager->get('flashmessenger');

$helper->setPluginFlashMessenger($flashMessenger);
$controllerPluginManager = $container->get('ControllerPluginManager');
/** @var \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger */
$flashMessenger = $controllerPluginManager->get('flashmessenger');

$config = $container->get('config');
if (isset($config['view_helper_config']['flashmessenger'])) {
$configHelper = $config['view_helper_config']['flashmessenger'];
if (isset($configHelper['message_open_format'])) {
$helper->setMessageOpenFormat($configHelper['message_open_format']);
$configHelper = (array) $config['view_helper_config']['flashmessenger'];

$isArrayOneDimensional = $this->isArrayOneDimensional($configHelper);

if ($isArrayOneDimensional === true) {
return $this->createHelperWithOldConfig($flashMessenger, $configHelper);
} else {
return $this->createHelperWithActualConfig($flashMessenger, $configHelper);
}
}

return new FlashMessenger();
}

/**
* @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger
* @param array $configHelper {
* message_open_format?:string, message_close_string?:string, message_separator_string?:string
* }
* @return FlashMessenger
*/
private function createHelperWithOldConfig($flashMessenger, $configHelper)
{
$helper = new FlashMessenger();
$helper->setPluginFlashMessenger($flashMessenger);

if (isset($configHelper['message_open_format'])) {
$helper
->setMessageOpenFormat((string) $configHelper['message_open_format']);
}
if (isset($configHelper['message_separator_string'])) {
$helper
->setMessageSeparatorString((string) $configHelper['message_separator_string']);
}
if (isset($configHelper['message_close_string'])) {
$helper
->setMessageCloseString((string) $configHelper['message_close_string']);
}

return $helper;
}

/**
* @param \Laminas\Mvc\Plugin\FlashMessenger\FlashMessenger $flashMessenger
* @param array $configHelper
* @return FlashMessenger
*/
private function createHelperWithActualConfig($flashMessenger, $configHelper)
{
$namespaces = [];
/**
* @var array<string, mixed> $arrProperties {
* classes?:string, message_open_format?:string, message_close_string?:string, message_separator_string?:string
* }
*/
foreach ($configHelper as $configNamespace => $arrProperties) {
$namespace = new FlashMessengerNamespace(
(string) $configNamespace,
isset($arrProperties['classes']) ? (string) $arrProperties['classes'] : ''
);
if (isset($arrProperties['message_open_format'])) {
$namespace
->setMessageOpenFormat((string) $arrProperties['message_open_format']);
}
if (isset($configHelper['message_separator_string'])) {
$helper->setMessageSeparatorString($configHelper['message_separator_string']);
if (isset($arrProperties['message_separator_string'])) {
$namespace
->setMessageSeparatorString((string) $arrProperties['message_separator_string']);
}
if (isset($configHelper['message_close_string'])) {
$helper->setMessageCloseString($configHelper['message_close_string']);
if (isset($arrProperties['message_close_string'])) {
$namespace
->setMessageCloseString((string) $arrProperties['message_close_string']);
}
$namespaces[$namespace->getName()] = $namespace;
}

$helper = new FlashMessenger($namespaces);
$helper->setPluginFlashMessenger($flashMessenger);
return $helper;
}

/**
* @param array $array
*/
private function isArrayOneDimensional(array $array): bool
{
/** @var mixed $property */
foreach ($array as $property) {
if (is_array($property)) {
return false;
}
}

return true;
}

/**
* Create service (v2)
*
Expand Down
Loading

0 comments on commit 86bb1c6

Please sign in to comment.