From 401a68b0d3bb1e9332f61181635e507ab4eebcdc Mon Sep 17 00:00:00 2001 From: Emilien Escalle Date: Thu, 21 Jul 2022 11:04:19 +0200 Subject: [PATCH] refactor(Form): use dedicated getter for retrieving helpers in formRows helper --- Dockerfile | 4 +- phpstan.neon | 1 - src/TwbsHelper/Form/View/Helper/FormRows.php | 174 +++++++++++++++++- .../View/OriginalHtmlAttributesSet.php | 21 ++- 4 files changed, 183 insertions(+), 17 deletions(-) diff --git a/Dockerfile b/Dockerfile index 4dcce7afd..84bfbe8ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,6 +8,6 @@ RUN \ apt-get install -yq unzip libicu-dev libonig-dev libxml2-dev; \ pecl install pcov libsodium; \ docker-php-ext-enable pcov sodium; \ - docker-php-ext-install dom tokenizer json mbstring intl xml xmlwriter simplexml iconv + docker-php-ext-install intl; -RUN echo 'memory_limit = 512M' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini; +RUN echo 'memory_limit=512M' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini; diff --git a/phpstan.neon b/phpstan.neon index 81c351fd9..54c75aaae 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,6 +1,5 @@ parameters: ignoreErrors: - - '/Call to an undefined method Laminas\\View\\Helper\\AbstractHelper::/' - '/Access to an offset on an unknown class Laminas\\ServiceManager\\InitializersConfigurationType/' laminasframework: serviceManagerLoader: tests/phpstan.php diff --git a/src/TwbsHelper/Form/View/Helper/FormRows.php b/src/TwbsHelper/Form/View/Helper/FormRows.php index 9167c5bd8..8e559a8d5 100644 --- a/src/TwbsHelper/Form/View/Helper/FormRows.php +++ b/src/TwbsHelper/Form/View/Helper/FormRows.php @@ -6,6 +6,36 @@ class FormRows extends \Laminas\Form\View\Helper\AbstractHelper { use \TwbsHelper\Form\View\ElementHelperTrait; + /** + * @var null|\TwbsHelper\Form\View\Helper\FormCollection + */ + protected $formCollectionHelper; + + /** + * @var null|\TwbsHelper\Form\View\Helper\FormRow + */ + protected $formRowHelper; + + /** + * @var null|\TwbsHelper\View\Helper\HtmlElement + */ + protected $htmlElementHelper; + + /** + * @var null|\TwbsHelper\View\Helper\HtmlAttributes + */ + protected $htmlAttributesHelper; + + /** + * @var null|\TwbsHelper\View\Helper\HtmlAttributes\HtmlClass + */ + protected $htmlClassHelper; + + /** + * @var null|\TwbsHelper\View\Helper\ButtonGroup + */ + protected $buttonGroupHelper; + /** * @param \Laminas\Form\FormInterface $form * @return \TwbsHelper\Form\View\Helper\FormRows|string @@ -91,12 +121,10 @@ protected function renderElement( return $this->renderButtonGroup($element, $rowsRendering, $rowOptions); } - $helperPluginManager = $this->getView()->getHelperPluginManager(); - if ($element instanceof \Laminas\Form\FieldsetInterface) { - $elementMarkup = $helperPluginManager->get('formCollection')->__invoke($element); + $elementMarkup = $this->getFormCollectionHelper()->__invoke($element); } else { - $elementMarkup = $helperPluginManager->get('formRow')->__invoke($element); + $elementMarkup = $this->getFormRowHelper()->__invoke($element); } if (!$elementMarkup) { @@ -130,11 +158,11 @@ protected function getElementRowRendering( } $rowRendering['helper'] = [ - $this->getView()->plugin('htmlElement'), + $this->getHtmlElementHelper(), '__invoke' ]; - $rowAttributes = $this->getView()->plugin('htmlattributes')->__invoke([ + $rowAttributes = $this->getHtmlattributesHelper()->__invoke([ 'class' => $rowOptions['row_class'], ]); @@ -142,7 +170,7 @@ protected function getElementRowRendering( $gutter = $rowOptions['gutter']; if ($gutter) { $rowAttributes->merge([ - 'class' => $this->getView()->plugin('htmlClass')->plugin('gutter')->getClassesFromOption( + 'class' => $this->getHtmlClassHelper()->plugin('gutter')->getClassesFromOption( $gutter ), ]); @@ -184,7 +212,7 @@ protected function renderButtonGroup( $rowsRendering[$rowRenderingKey] = [ 'content' => [$button], 'helper' => [ - $this->getView()->getHelperPluginManager()->get('buttonGroup'), + $this->getButtonGroupHelper(), '__invoke' ], 'helper_params' => [ @@ -236,4 +264,134 @@ private function incrementRowRenderingKeyPrefix(string $key): string { return (int) explode('_', $key)[0] + 1 . '_'; } + + /** + * Retrieve the formCollection helper + */ + protected function getFormCollectionHelper(): \TwbsHelper\Form\View\Helper\FormCollection + { + if ($this->formCollectionHelper) { + return $this->formCollectionHelper; + } + + if ($this->view !== null && method_exists($this->view, 'plugin')) { + $this->formCollectionHelper = $this->view->plugin('formCollection'); + } + + if (!$this->formCollectionHelper instanceof \TwbsHelper\Form\View\Helper\FormCollection) { + throw new \LogicException(sprintf( + 'FormCollection helper expects an instanceof \TwbsHelper\Form\View\Helper\FormCollection, "%s" defined', + is_object($this->formCollectionHelper) + ? get_class($this->formCollectionHelper) + : gettype($this->formCollectionHelper) + )); + } + + return $this->formCollectionHelper; + } + + /** + * Retrieve the formRow helper + */ + protected function getFormRowHelper(): \TwbsHelper\Form\View\Helper\FormRow + { + if ($this->formRowHelper) { + return $this->formRowHelper; + } + + if ($this->view !== null && method_exists($this->view, 'plugin')) { + $this->formRowHelper = $this->view->plugin('formRow'); + } + + if (!$this->formRowHelper instanceof \TwbsHelper\Form\View\Helper\FormRow) { + throw new \LogicException(sprintf( + 'FormCollection helper expects an instanceof \TwbsHelper\Form\View\Helper\FormRow, "%s" defined', + is_object($this->formRowHelper) + ? get_class($this->formRowHelper) + : gettype($this->formRowHelper) + )); + } + + return $this->formRowHelper; + } + + /** + * Retrieve the htmlElement helper + */ + protected function getHtmlElementHelper() + { + if ($this->htmlElementHelper) { + return $this->htmlElementHelper; + } + + if ($this->view !== null && method_exists($this->view, 'plugin')) { + $this->htmlElementHelper = $this->view->plugin('htmlElement'); + } + + if (!$this->htmlElementHelper instanceof \TwbsHelper\View\Helper\HtmlElement) { + $this->htmlElementHelper = new \TwbsHelper\View\Helper\HtmlElement(); + } + + return $this->htmlElementHelper; + } + + /** + * Retrieve the htmlattributes helper + */ + protected function getHtmlattributesHelper() + { + if ($this->htmlAttributesHelper) { + return $this->htmlAttributesHelper; + } + + if ($this->view !== null && method_exists($this->view, 'plugin')) { + $this->htmlAttributesHelper = $this->view->plugin('htmlAttributes'); + } + + if (!$this->htmlAttributesHelper instanceof \TwbsHelper\View\Helper\HtmlAttributes) { + $this->htmlAttributesHelper = new \TwbsHelper\View\Helper\HtmlAttributes(); + } + + return $this->htmlAttributesHelper; + } + + /** + * Retrieve the htmlclass helper + */ + protected function getHtmlClassHelper() + { + if ($this->htmlClassHelper) { + return $this->htmlClassHelper; + } + + if ($this->view !== null && method_exists($this->view, 'plugin')) { + $this->htmlClassHelper = $this->view->plugin('htmlClass'); + } + + if (!$this->htmlClassHelper instanceof \TwbsHelper\View\Helper\HtmlAttributes\HtmlClass) { + $this->htmlClassHelper = new \TwbsHelper\View\Helper\HtmlAttributes\HtmlClass(); + } + + return $this->htmlClassHelper; + } + + /** + * Retrieve the buttongroup helper + */ + protected function getButtonGroupHelper() + { + if ($this->buttonGroupHelper) { + return $this->buttonGroupHelper; + } + + if ($this->view !== null && method_exists($this->view, 'plugin')) { + $this->buttonGroupHelper = $this->view->plugin('buttonGroup'); + } + + if (!$this->buttonGroupHelper instanceof \TwbsHelper\View\Helper\ButtonGroup) { + $this->buttonGroupHelper = new \TwbsHelper\View\Helper\ButtonGroup(); + } + + return $this->buttonGroupHelper; + } } diff --git a/src/TwbsHelper/View/OriginalHtmlAttributesSet.php b/src/TwbsHelper/View/OriginalHtmlAttributesSet.php index 71feceb6c..4882e9ebb 100644 --- a/src/TwbsHelper/View/OriginalHtmlAttributesSet.php +++ b/src/TwbsHelper/View/OriginalHtmlAttributesSet.php @@ -1,15 +1,22 @@ + * */ class OriginalHtmlAttributesSet extends ArrayObject { /** * HTML escaper - * - * @var Escaper */ - private $escaper; + private Escaper $escaper; public function __construct(Escaper $escaper, iterable $attributes = []) { - $attributes = $attributes instanceof Traversable ? iterator_to_array($attributes, true) : $attributes; + $attributes = $attributes instanceof Traversable ? iterator_to_array($attributes, true) : $attributes; $this->escaper = $escaper; parent::__construct($attributes); } @@ -39,7 +48,7 @@ public function __construct(Escaper $escaper, iterable $attributes = []) /** * Set several attributes at once. * - * @param iterable $attributes + * @param AttributeSet $attributes */ public function set(iterable $attributes): self { @@ -72,7 +81,7 @@ public function add(string $name, $value): self /** * Merge attributes with existing attributes. * - * @param iterable $attributes + * @param AttributeSet $attributes */ public function merge(iterable $attributes): self {