/**
  * If this request was not cancelled, and the request's \Phruts\Config\ActionConfig
  * has not disabled validation, call the validate method of the specified
  * ActionForm, and forward back to the input form if there were any
  * errors.
  *
  * Return true if we should continue processing, or false if we have already
  * forwarded control back to the input form.
  *
  * @param \Symfony\Component\HttpFoundation\Request $request The kernel request we are
  * processing
  * @param \Symfony\Component\HttpFoundation\Response $response The kernel response we are
  * creating
  * @param \Phruts\Action\AbstractActionForm $form The ActionForm instance we are
  * populating
  * @param \Phruts\Action\ActionMapping $mapping The \Phruts\Config\ActionConfig we are using
  * @return boolean
  */
 protected function processValidate(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response, \Phruts\Action\AbstractActionForm $form = null, \Phruts\Action\ActionMapping $mapping)
 {
     if (is_null($form)) {
         return true;
     }
     // Was this request cancelled?
     if (!is_null($request->attributes->get(\Phruts\Util\Globals::CANCEL_KEY))) {
         if (!empty($this->log)) {
             $this->log->debug('  Cancelled transaction, skipping validation');
         }
         return true;
     }
     // Has validation been turned off for this mapping?
     if (!$mapping->getValidate()) {
         return true;
     }
     // Call the form bean's validation method
     if (!empty($this->log)) {
         $this->log->debug('  Validating input form properties');
     }
     $errors = $form->validate($mapping, $request);
     if (is_null($errors) || $errors->isEmpty()) {
         if (!empty($this->log)) {
             $this->log->debug('  No errors detected, accepting input');
         }
         return true;
     }
     // Has an input form been specified for this mapping?
     $input = $mapping->getInput();
     if (is_null($input)) {
         if (!empty($this->log)) {
             $this->log->debug('  Validation failed but no input form available');
         }
         $msg = $this->getInternal()->getMessage(null, 'noInput', $mapping->getPath());
         throw new \Phruts\Exception($msg);
     }
     // Save our error messages and return to the input form if possible
     if (!empty($this->log)) {
         $this->log->debug('  Validation failed, returning to "' . $input . '"');
     }
     $request->attributes->set(\Phruts\Util\Globals::ERROR_KEY, $errors);
     if ($this->moduleConfig->getControllerConfig()->getInputForward()) {
         $forward = $mapping->findForward($input);
         $this->processForwardConfig($request, $response, $forward);
     } else {
         // Delegate the processing of this request
         if (!empty($this->log)) {
             $this->log->debug('  Delegating via forward to "' . $input . '"');
         }
         $this->doForward($input, $request, $response);
     }
 }
Exemple #2
0
 public function testModuleConfig()
 {
     $config = new ModuleConfig('prefix2');
     $this->assertEquals('prefix2', $config->getPrefix());
     $config->setPrefix('prefix');
     $this->assertEquals('prefix', $config->getPrefix());
     $controllerConfig = new ControllerConfig();
     $controllerConfig->setProcessorClass('\\Mock\\Proccessor');
     $controllerConfig->setContentType('application/x-javascript');
     $controllerConfig->setNocache('true');
     $controllerConfig->setInputForward('true');
     $controllerConfig->setLocale('true');
     $expected = "\\Phruts\\Config\\ControllerConfig[processorClass='\\\\Mock\\\\Proccessor',contentType='application/x-javascript',nocache=true,inputForward=true,locale=true]";
     $this->assertEquals($expected, (string) $controllerConfig);
     $config->setControllerConfig($controllerConfig);
     $this->assertNotEmpty($config->getControllerConfig());
     $actionConfig1 = new ActionConfig();
     $actionConfig1->setPath('action1');
     $actionConfig1->setType('\\ForwardConfig');
     $config->addActionConfig($actionConfig1);
     $actionConfig2 = new ActionConfig();
     $actionConfig2->setPath('action2');
     $actionConfig2->setType('\\ForwardConfig');
     $config->addActionConfig($actionConfig2);
     $this->assertNotEmpty($config->findActionConfig('/action1'));
     $this->assertNotEmpty($config->findActionConfig('/action2'));
     $config->removeActionConfig($actionConfig2);
     $this->assertEmpty($config->findActionConfig('/action2'));
     $actionClass = '\\MyActionConfigClass';
     $config->setActionClass($actionClass);
     $this->assertEquals('\\MyActionConfigClass', $config->getActionClass());
     $formBeanConfig1 = new FormBeanConfig();
     $formBeanConfig1->setName('myForm1');
     $formBeanConfig1->setType('\\MyForm1');
     $formBeanConfig2 = new FormBeanConfig();
     $formBeanConfig2->setName('myForm2');
     $formBeanConfig2->setType('\\MyForm2');
     $config->addFormBeanConfig($formBeanConfig1);
     $config->addFormBeanConfig($formBeanConfig2);
     $this->assertEquals(2, count($config->findFormBeanConfigs()));
     $this->assertEquals($formBeanConfig1, $config->findFormBeanConfig('myForm1'));
     $this->assertEquals($formBeanConfig2, $config->findFormBeanConfig('myForm2'));
     $config->removeFormBeanConfig($formBeanConfig2);
     $this->assertEmpty($config->findFormBeanConfig('myForm2'));
     $forwardConfig1 = new ForwardConfig();
     $forwardConfig1->setName('welcome');
     $forwardConfig1->setPath('welcome.html.twig');
     $forwardConfig2 = new ForwardConfig();
     $forwardConfig2->setName('login');
     $forwardConfig1->setPath('login.html.twig');
     $config->addForwardConfig($forwardConfig1);
     $config->addForwardConfig($forwardConfig2);
     $this->assertEquals($forwardConfig1, $config->findForwardConfig('welcome'));
     $this->assertEquals($forwardConfig2, $config->findForwardConfig('login'));
     $config->removeForwardConfig($forwardConfig1);
     $this->assertEmpty($config->findForwardConfig('welcome'));
     $dataSourceConfig = new DataSourceConfig();
     $dataSourceConfig->setKey('key1');
     $config->addDataSourceConfig($dataSourceConfig);
     $dataSourceConfig2 = new DataSourceConfig();
     $dataSourceConfig2->setKey('key2');
     $config->addDataSourceConfig($dataSourceConfig2);
     $this->assertEquals(2, count($config->findDataSourceConfigs()));
     $this->assertEquals($dataSourceConfig, $config->findDataSourceConfig('key1'));
     $this->assertEquals($dataSourceConfig2, $config->findDataSourceConfig('key2'));
     $config->removeDataSourceConfig($dataSourceConfig);
     $this->assertEmpty($config->findDataSourceConfig('key1'));
     $messageConfig = new MessageResourcesConfig();
     $messageConfig->setKey('key1');
     $config->addMessageResourcesConfig($messageConfig);
     $this->assertEquals($messageConfig, $config->findMessageResourcesConfig('key1'));
     $this->assertEquals(1, count($config->findMessageResourcesConfigs()));
     $config->removeMessageResourcesConfig($messageConfig);
     $this->assertEmpty($config->findMessageResourcesConfig('key1'));
     $plugInConfig = new PlugInConfig();
     $plugInConfig->setKey('test');
     $plugInConfig->setClassName('\\stdClass');
     $config->addPlugInConfig($plugInConfig);
     $this->assertEquals(1, count($config->findPlugInConfigs()));
     $exceptionConfig = new ExceptionConfig();
     $exceptionConfig->setType('\\Exception');
     $config->addExceptionConfig($exceptionConfig);
     $this->assertEquals(1, count($config->findExceptionConfigs()));
     $this->assertEquals($exceptionConfig, $config->findExceptionConfig('\\Exception'));
     $config->removeExceptionConfig($exceptionConfig);
     $this->assertEmpty($config->findExceptionConfig('\\Exception'));
     // Test exception
     $config->freeze();
     $this->assertTrue($config->getConfigured());
     $this->setExpectedException('\\Phruts\\Exception\\IllegalStateException');
     $config->setPrefix('prefix2');
 }
 public function testProcessContent()
 {
     $method = self::getMethod('processContent');
     $method->invokeArgs($this->requestProcessor, array($this->request, $this->response));
     $this->assertEquals($this->moduleConfig->getControllerConfig()->getContentType(), $this->response->headers->get('content-type'));
 }
Exemple #4
0
 /**
  * Instantiate the request processor if defined in the config
  * @param  \Phruts\Config\ModuleConfig $config
  * @return mixed
  * @throws \Exception
  */
 protected function getRequestProcessor(\Phruts\Config\ModuleConfig $config)
 {
     // Access from the dependency injector
     $key = \Phruts\Util\Globals::REQUEST_PROCESSOR_KEY . $config->getPrefix();
     // If not found
     if (empty($this->application[$key])) {
         try {
             $processorClass = $config->getControllerConfig()->getProcessorClass();
             $processor = \Phruts\Util\ClassLoader::newInstance($processorClass, '\\Phruts\\Action\\RequestProcessor');
             $processor->init($this, $config);
             $this->application[$key] = $processor;
             return $this->application[$key];
         } catch (\Exception $e) {
             throw new \Exception('Cannot initialize RequestProcessor of class ' . $processorClass . ': ' . $e->getMessage());
         }
     }
     return $this->application[$key];
 }