diff --git a/composer.json b/composer.json index 12b4213..f88f125 100644 --- a/composer.json +++ b/composer.json @@ -19,15 +19,13 @@ ], "require": { "php": "~7.4.0 || ~8.0.0 || ~8.1.0", - "laminas/laminas-servicemanager": "^3.10", - "laminas/laminas-form": "^3.1.1", - "laminas/laminas-dependency-plugin": "^2.2", - "laminas/laminas-log": "2.15.2" + "laminas/laminas-servicemanager": "^3.19.0", + "laminas/laminas-form": "^3.5.0" }, "require-dev": { - "phpunit/phpunit": "^9.1", - "squizlabs/php_codesniffer": "^3.5", - "doctrine/annotations": "^1.10" + "phpunit/phpunit": "^9.5.25", + "squizlabs/php_codesniffer": "^3.7.1", + "doctrine/annotations": "^1.13.3" }, "autoload": { "psr-4": { diff --git a/src/Factory/FormAbstractServiceFactory.php b/src/Factory/FormAbstractServiceFactory.php index 80cb9db..65f89c6 100644 --- a/src/Factory/FormAbstractServiceFactory.php +++ b/src/Factory/FormAbstractServiceFactory.php @@ -10,26 +10,34 @@ namespace Dot\Form\Factory; use Interop\Container\ContainerInterface; +use Laminas\Form\FormElementManager; use Laminas\InputFilter\Factory; use Laminas\InputFilter\InputFilterInterface; use Laminas\ServiceManager\Factory\AbstractFactoryInterface; use Laminas\Stdlib\ArrayUtils; -use Laminas\Log\LoggerAbstractServiceFactory; +use Psr\Container\NotFoundExceptionInterface; +use Psr\Container\ContainerExceptionInterface; /** * Class FormAbstractServiceFactory * @package Dot\Form\Factory */ -class FormAbstractServiceFactory extends LoggerAbstractServiceFactory +class FormAbstractServiceFactory implements AbstractFactoryInterface { const PREFIX = 'dot-form'; + /** @var null|array */ + private $config; + /** @var string */ protected $configKey = 'dot_form'; /** @var string */ protected $subConfigKey = 'forms'; + /** @var null|\Laminas\Form\Factory Form factory used to create forms */ + private ?Factory $factory = null; + /** * @param ContainerInterface $container * @param string $requestedName @@ -38,20 +46,27 @@ class FormAbstractServiceFactory extends LoggerAbstractServiceFactory public function canCreate(ContainerInterface $container, $requestedName) { $parts = explode('.', $requestedName); + if (count($parts) !== 2) { return false; } if ($parts[0] !== static::PREFIX) { return false; } - return parent::canCreate($container, $parts[1]); + + $config = $this->getConfig($container); + if (empty($config)) { + return false; + } + + return isset($config[$parts[1]]); } /** * @param ContainerInterface $container - * @param string $requestedName + * @param $requestedName * @param array|null $options - * @return \Laminas\Form\ElementInterface + * @return void */ public function __invoke(ContainerInterface $container, $requestedName, array $options = null) { @@ -77,8 +92,6 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o } while ($extendsConfigKey != null); $this->config[$parts[1]] = $specificConfig; - - return parent::__invoke($container, $parts[1], $options); } /** @@ -87,7 +100,25 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o */ protected function getConfig(ContainerInterface $container): array { - parent::getConfig($container); + if ($this->config !== null) { + return $this->config; + } + + if (! $container->has('config')) { + $this->config = []; + + return $this->config; + } + + $config = $container->get('config'); + if (! isset($config[$this->configKey])) { + $this->config = []; + + return $this->config; + } + + $this->config = $config[$this->configKey]; + if (!empty($this->config)) { if (isset($this->config[$this->subConfigKey]) && is_array($this->config[$this->subConfigKey])) { $this->config = $this->config[$this->subConfigKey]; @@ -97,14 +128,25 @@ protected function getConfig(ContainerInterface $container): array return $this->config; } + /** + * @param ContainerInterface $container + * @return \Laminas\Form\Factory + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ protected function getFormFactory(ContainerInterface $container): \Laminas\Form\Factory { - $formFactory = parent::getFormFactory($container); - if ($container->has('InputFilterManager')) { - $formFactory->setInputFilterFactory(new Factory($container->get('InputFilterManager'))); + if ($this->factory instanceof Factory) { + return $this->factory; + } + + $elements = null; + if ($container->has(FormElementManager::class)) { + $elements = $container->get(FormElementManager::class); } - return $formFactory; + $this->factory = new Factory($elements); + return $this->factory; } /**