/** * Create (if necessary) and return a \Phruts\Action\AbstractActionForm instance appropriate * for this request. * * If no \Phruts\Action\AbstractActionForm instance is required, return null. * * @param \Symfony\Component\HttpFoundation\Request $request The actionKernel request we are * processing * @param \Phruts\Config\ActionConfig $mapping The action mapping for this request * @param \Phruts\Config\ModuleConfig $moduleConfig The configuration for this * module * @param \Phruts\Action\ActionKernel $actionKernel The action actionKernel * @return \Phruts\Action\AbstractActionForm Form instance associated with this * request */ public static function createActionForm(\Symfony\Component\HttpFoundation\Request $request, \Phruts\Config\ActionConfig $mapping, \Phruts\Config\ModuleConfig $moduleConfig, \Phruts\Action\ActionKernel $kernel) { // Is there a form bean associated with this mapping? $attribute = $mapping->getAttribute(); if (is_null($attribute)) { return null; } // Look up the form bean configuration information to use $name = $mapping->getName(); $config = $moduleConfig->findFormBeanConfig($name); if (is_null($config)) { return null; } $instance = null; $session = null; if ($mapping->getScope() == 'request') { $instance = $request->attributes->get($attribute); } else { \Phruts\Util\ClassLoader::loadClass($config->getType()); $session = $request->getSession(); if (!empty($session)) { $instance = $session->get($attribute); } } // Can we recycle the existing form bean instance (if there is one)? if (!is_null($instance)) { $configClass = $config->getType(); $instanceClass = get_class($instance); if (\Phruts\Util\ClassLoader::classIsAssignableFrom($configClass, $instanceClass)) { return $instance; } } // Create and return a new form bean instance try { /** @var \Phruts\Action\AbstractActionForm $instance */ $instance = \Phruts\Util\ClassLoader::newInstance($config->getType(), '\\Phruts\\Action\\AbstractActionForm'); $instance->setActionKernel($kernel); } catch (\Exception $e) { $msg = $kernel->getInternal()->getMessage(null, 'formBean', $config->getType()); throw new \Phruts\Exception($msg); } return $instance; }
public function testActionConfig() { $moduleConfig = new ModuleConfig('prefix'); $globalForward = new ForwardConfig(); $globalForward->setPath("login.html.twig"); $globalForward->setName('login'); $moduleConfig->addForwardConfig($globalForward); $config = new ActionConfig(); $config->setType('\\MyAction'); $this->assertEquals('\\MyAction', $config->getType()); $config->setScope('session'); $this->assertEquals('session', $config->getScope()); $config->setScope('request'); $config->setName('myForm'); $this->assertEquals('myForm', $config->getName()); $config->setPath('mypath'); $this->assertEquals('/mypath', $config->getPath()); $config->setInput('form.php'); $this->assertEquals('form.php', $config->getInput()); $config->setPrefix('prefix'); $this->assertEquals('prefix', $config->getPrefix()); $config->setRoles("role1,role2,role3"); $this->assertEquals('role1,role2,role3', $config->getRoles()); $this->assertEquals(array('role1', 'role2', 'role3'), $config->getRoleNames()); $config->setAttribute('attribute'); $config->setModuleConfig($moduleConfig); // Test our sets $expected = "\\Phruts\\Config\\ActionConfig[path='/mypath',type='\\\\MyAction',name='myForm',scope='request',attribute='attribute',prefix='prefix',validate=true,input='form.php',roles='role1,role2,role3',unknown=false]"; $this->assertEquals($expected, (string) $config); $config->setParameter('abc'); $this->assertEquals('abc', $config->getParameter()); // Test we can find the forward config $forwardConfig1 = new ForwardConfig(); $forwardConfig1->setName('success'); $forwardConfig1->setPath('mypath.html.twig'); $config->addForwardConfig($forwardConfig1); $this->assertNotEmpty($config->findForwardConfig('success')); $config->removeForwardConfig($forwardConfig1); $this->assertEmpty($config->findForwardConfig('success')); $this->assertNotEmpty($config->findForwardConfigs()); $this->assertNotEmpty($config->findForwardConfig('login')); $config->setParameter('myparam'); $this->assertEquals('myparam', $config->getParameter()); $exceptionConfig = new ExceptionConfig(); $exceptionConfig->setType('\\Exception'); $exceptionConfig->setPath('exception.html.twig'); $config->addExceptionConfig($exceptionConfig); $this->assertNotEmpty($config->findExceptionConfig('\\Exception')); $this->assertEmpty($config->findExceptionConfig('\\MyOtherException')); $config->removeExceptionConfig($exceptionConfig); $this->assertEmpty($config->findExceptionConfig('\\Exception')); $this->assertTrue($config->getValidate()); $config->setValidate('no'); $this->assertNotTrue($config->getValidate()); $config->setValidate('yes'); $this->assertTrue($config->getValidate()); $config->setValidate('false'); $this->assertNotTrue($config->getValidate()); $config->setValidate('true'); $this->assertTrue($config->getValidate()); $this->assertNotTrue($config->getUnknown()); $config->setUnknown('yes'); $this->assertTrue($config->getUnknown()); $config->setUnknown('false'); $this->assertNotTrue($config->getUnknown()); $config->setUnknown('true'); $this->assertTrue($config->getUnknown()); $config->setUnknown('no'); $this->assertNotTrue($config->getUnknown()); $config->setPath('path'); $this->assertEquals('/path', $config->getPath()); // TODO: Test exception $config->freeze(); $this->setExpectedException('\\Phruts\\Exception\\IllegalStateException'); $config->setType('\\MyOtherActionActually'); }