/** * @param string $name * @param array $options * @return \Wizard\Step\StepInterface */ public function create($name, array $options = []) { $step = $this->stepPluginManager->get($name); if (isset($options['form'])) { $form = $this->formPluginManager->get($options['form']); $step->setForm($form); unset($options['form']); } $step->setName($name); $step->getOptions()->setFromArray($options); return $step; }
/** * Standard preferences handling via preferences form subclass * * @param string $name Name of the form service * @return \Zend\View\Model\ViewModel|\Zend\Http\Response View model for "form.php" template or redirect response */ protected function _useForm($name) { $form = $this->_formManager->get($name); if ($this->getRequest()->isPost()) { $form->setData($this->params()->fromPost()); if ($form->isValid()) { // Flatten Preferences array, i.e. incorporate fields from a // fieldset into a single array. $this->_config->setOptions(new \RecursiveIteratorIterator(new \RecursiveArrayIterator($form->getData()['Preferences']))); return $this->redirectToRoute('preferences', $this->getEvent()->getRouteMatch()->getParams()['action']); } } else { $preferences = array(); foreach ($form->get('Preferences') as $element) { $name = $element->getName(); if ($element instanceof \Zend\Form\Fieldset) { foreach ($element as $subElement) { $subElementName = $subElement->getName(); $preferences[$name][$subElementName] = $this->_config->{$subElementName}; } } else { $preferences[$name] = $this->_config->{$name}; } } $form->setData(array('Preferences' => $preferences)); } return $this->printForm($form); }
/** * Gets a specific formular. * * This formular will be created upon the first retrievement. * If created, the formular gets passed the formular parameters set in this container. * * @param string $key * @return null|\Core\Form\ContainerInterface|\Zend\Form\FormInterface */ public function getForm($key) { if (false !== strpos($key, '.')) { list($key, $childKey) = explode('.', $key, 2); $container = $this->getForm($key); return $container->getForm($childKey); } if (!isset($this->forms[$key])) { return null; } $form = $this->forms[$key]; if (isset($form['__instance__']) && is_object($form['__instance__'])) { return $form['__instance__']; } $options = isset($form['options']) ? $form['options'] : array(); if (!isset($options['use_post_array'])) { $options['use_post_array'] = true; } if (!isset($options['use_files_array'])) { $options['use_files_array'] = false; } $formInstance = $this->formElementManager->get($form['type'], $options); $formName = (($name = $this->getName()) ? $name . '.' : '') . $form['name']; $formInstance->setName($formName)->setAttribute('action', '?form=' . $formName); if (isset($form['label'])) { $formInstance->setLabel($form['label']); } if ($entity = $this->getEntity()) { $this->mapEntity($formInstance, isset($form['property']) ? $form['property'] : $key, $entity); } $formInstance->setParams($this->getParams()); $this->forms[$key]['__instance__'] = $formInstance; $this->forms[$key]['options'] = $options; return $formInstance; }
/** * @group 6132 */ public function testSharedFormElementsAreNotInitializedMultipleTimes() { $element = $this->getMock('Zend\\Form\\Element', array('init')); $element->expects($this->once())->method('init'); $this->manager->setFactory('sharedElement', function () use($element) { return $element; }); $this->manager->setShared('sharedElement', true); $this->manager->get('sharedElement'); $this->manager->get('sharedElement'); }
/** * Fetches a text search form. * * If only the service for an element fieldset ist passed, * it will fetch a "Core/TextSearch" form and pass the * elements fieldset along. * * @param string|array $elementsFieldset * @param string|null $buttonsFieldset * * @return \Core\Form\TextSearchForm */ public function get($elementsFieldset, $buttonsFieldset = null) { if (is_array($elementsFieldset)) { $elementsOptions = isset($elementsFieldset[1]) ? $elementsFieldset[1] : []; $elementsFieldset = $elementsFieldset[0]; } else { $elementsOptions = []; } $form = $this->formElementManager->get($elementsFieldset, $elementsOptions); /** @noinspection PhpUndefinedMethodInspection */ $params = $this->getController()->getRequest()->getQuery()->toArray(); if (!$form instanceof TextSearchForm) { $options = ['elements_fieldset' => $form]; if (null !== $buttonsFieldset) { $options['buttons_fieldset'] = $buttonsFieldset; } $form = $this->formElementManager->get('Core/TextSearch', $options); } $form->setSearchParams($params); return $form; }
/** * Gets a specific formular. * * This formular will be created upon the first retrievement. * If created, the formular gets passed the formular parameters set in this container. * * @param string $key * @param bool $asInstance if set to false, the specification array is returned, and no instance created. * * @return null|\Core\Form\Container|\Zend\Form\FormInterface * @since 0,25 added $asInstance parameter */ public function getForm($key, $asInstance = true) { if (false !== strpos($key, '.')) { list($key, $childKey) = explode('.', $key, 2); $container = $this->getForm($key); return $container->getForm($childKey); } if (!isset($this->forms[$key])) { return null; } $form = $this->forms[$key]; if (!$asInstance) { return $form; } if (isset($form['__instance__']) && is_object($form['__instance__'])) { return $form['__instance__']; } $options = isset($form['options']) ? $form['options'] : array(); if (!isset($options['name'])) { $options['name'] = isset($form['name']) ? $form['name'] : $key; } if (!isset($options['use_post_array'])) { $options['use_post_array'] = true; } if (!isset($options['use_files_array'])) { $options['use_files_array'] = false; } $formInstance = $this->formElementManager->get($form['type'], $options); $formInstance->setParent($this); if (isset($form['attributes'])) { $formInstance->setAttributes($form['attributes']); } $formName = $this->formatAction($form['name']); $formInstance->setName($formName); $formAction = $formInstance->getAttribute('action'); if (empty($formAction)) { $formInstance->setAttribute('action', '?form=' . $formName); } if (isset($form['label'])) { $formInstance->setLabel($form['label']); } if (isset($form['disable_elements']) && $formInstance instanceof DisableElementsCapableInterface && $formInstance->isDisableElementsCapable()) { $formInstance->disableElements($form['disable_elements']); } $entity = $this->getEntity($form['entity']); if ($entity) { $this->mapEntity($formInstance, $entity, isset($form['property']) ? $form['property'] : $key); } $formInstance->setParams($this->getParams()); $this->forms[$key]['__instance__'] = $formInstance; $this->forms[$key]['options'] = $options; return $formInstance; }
public function testImportActionPostValidSuccess() { $fileSpec = array('tmp_name' => 'uploaded_file'); $this->getRequest()->getFiles()->set('File', $fileSpec); $postData = array('key' => 'value'); $form = $this->_formManager->get('Console\\Form\\Import'); $form->expects($this->once())->method('isValid')->will($this->returnValue(true)); $form->expects($this->once())->method('setData')->with(array('File' => $fileSpec, 'key' => 'value')); $form->expects($this->once())->method('getData')->will($this->returnValue(array('File' => $fileSpec))); $form->expects($this->never())->method('render'); $response = new \Zend\Http\Response(); $response->setStatusCode(200); $this->_inventoryUploader->expects($this->once())->method('uploadFile')->with('uploaded_file')->will($this->returnValue($response)); $this->dispatch('/console/client/import/', 'POST', $postData); $this->assertRedirectTo('/console/client/index/'); }
/** * Import client via file upload * * @return array|\Zend\Http\Response array(form [, uri, response]) or redirect response */ public function importAction() { $form = $this->_formManager->get('Console\\Form\\Import'); $vars = array('form' => $form); if ($this->getRequest()->isPost()) { $form->setData($this->params()->fromFiles() + $this->params()->fromPost()); if ($form->isValid()) { $data = $form->getData(); $response = $this->_inventoryUploader->uploadFile($data['File']['tmp_name']); if ($response->isSuccess()) { return $this->redirectToRoute('client', 'index'); } else { $vars['response'] = $response; $vars['uri'] = $this->_config->communicationServerUri; } } } return $vars; }
/** * @param $className * * @return object * @throws \Exception */ private function buildGraph($className) { if ($this->enableObjectGraphDebug) { echo 'Building an object from the class: ' . $className . "<br />"; } try { $reflection = new \ReflectionClass($className); } catch (\Exception $e) { if ($this->enableObjectGraphDebug) { echo 'Could not build a reflection class for ' . $className . "<br />"; } return false; } if (!$reflection->getConstructor()) { if ($this->enableObjectGraphDebug) { echo 'We have build an object for class ' . $className . "<br />"; } return new $className(); } $constructorParameters = $reflection->getConstructor()->getParameters(); $dependencies = []; foreach ($constructorParameters as $param) { try { $paramClassName = $param->getClass(); } catch (\Exception $e) { throw new \Exception('Could not create dependency class "' . $param->getName() . '" for the currently building class: ' . $className); } if (!$paramClassName) { switch (true) { case $this->controllerPluginManager->has($param->name, false): if ($this->enableObjectGraphDebug) { echo 'The parameter "' . $param->name . '" exists in Controller Manager' . "<br />"; } $dependencies[] = $this->controllerPluginManager->get($param->name); break; case $this->serviceManager->has($param->name, false): if ($this->enableObjectGraphDebug) { echo 'The parameter "' . $param->name . '" exists in Service Manager' . "<br />"; } $dependencies[] = $this->serviceManager->get($param->name); break; default: if ($param->isOptional()) { $dependencies[] = $param->getDefaultValue(); } else { if ($this->enableObjectGraphDebug) { echo 'The parameter "' . $param->name . '" does not exist in (any) Service Manager' . "<br />"; } echo 'The parameter "' . $param->name . '" does not exist in the Service Manager' . "<br />"; die; } break; } } else { if ($this->enableObjectGraphDebug) { echo 'Does the SM have an object for ' . $param->getClass()->name . "<br />"; } switch (true) { case $this->controllerPluginManager->has($param->getClass()->name, false, false): if ($this->enableObjectGraphDebug) { echo 'It does in the Controller Plugin Manager' . "<br />"; } $dependencies[] = $this->controllerPluginManager->get($param->getClass()->name); break; case $this->viewHelperManager->has($param->getClass()->name, false, false): if ($this->enableObjectGraphDebug) { echo 'It does in the View Helper Manager' . "<br />"; } $dependencies[] = $this->viewHelperManager->get($param->getClass()->name); break; case $this->formElementManager->has($param->getClass()->name, false, false): if ($this->enableObjectGraphDebug) { echo 'It does in the Form Element Manager' . "<br />"; } $dependencies[] = $this->formElementManager->get($param->getClass()->name); break; case $this->serviceManager->has($param->getClass()->name, false, false): if ($this->enableObjectGraphDebug) { echo 'It does in the Service Manager' . "<br />"; } $name = $param->getClass()->name; if ($this->serviceManager->hasAlias($name)) { $config = $this->serviceManager->get('config')['service_manager']; $name = $config['aliases'][$name]; } $dependencies[] = $this->serviceManager->get($name); break; default: if ($this->enableObjectGraphDebug) { echo 'No it It does not have an object' . "<br />"; } if ($param->getClass()->isInterface()) { continue; } $dependencies[] = $this->buildGraph($param->getClass()->name); break; } } } $instance = $reflection->newInstanceArgs($dependencies); if (in_array($className, $this->serviceManager->get('config')['service_manager']['shared'])) { $this->serviceManager->setAllowOverride(true); $this->serviceManager->setService($className, $instance); $this->serviceManager->setAllowOverride(false); } if ($this->enableObjectGraphDebug) { echo 'We have build an object for class ' . $className . "<br />"; } if ($this->enableObjectGraphDebug) { echo ' ==== ' . "<br />"; } return $instance; }
public function testLoadingInvalidElementRaisesException() { $this->manager->setInvokableClass('test', get_class($this)); $this->setExpectedException('Zend\\Form\\Exception\\InvalidElementException'); $this->manager->get('test'); }