/** * Identify and return the path component (from the request URI) that * we will use to select a \Phruts\Config\ActionConfig to dispatch with. * * If no such path can be identified, create an error response * and return null. * * @param \Symfony\Component\HttpFoundation\Request $request The kernel request we are * processing * @param \Symfony\Component\HttpFoundation\Response $response The kernel response we are * creating * @return string */ protected function processPath(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\HttpFoundation\Response $response) { // For prefix matching, match on the path info (if any) $path = (string) $request->attributes->get(self::INCLUDE_PATH_INFO); if ($path == null) { $path = $request->getPathInfo(); } // Always have a forward slash in the path if (substr($path, 0, 1) !== '/') { $path = '/' . $path; } // For extension matching, strip the module prefix and extension $prefix = $this->moduleConfig->getPrefix(); if (!empty($prefix)) { if (!preg_match('#^/?' . $prefix . '/.*#', $path)) { $msg = $this->getInternal()->getMessage(null, "processPath", $request->getRequestURI()); if (!empty($this->log)) { $this->log->error($msg); } throw new BadRequestHttpException($msg); } // Strip module $path = preg_replace('#^/?' . $prefix . '#', '', $path); } return $path; }
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'); }
protected function initModulePlugIns(\Phruts\Config\ModuleConfig $config) { if (!empty($this->log)) { $this->log->debug('Initializing module "' . $config->getPrefix() . '" plug ins'); } $plugInConfigs = $config->findPlugInConfigs(); $plugIns = array(); foreach ($plugInConfigs as $plugInConfig) { /* @var $plugInConfig \Phruts\Config\PlugInConfig */ try { /* @var $plugIn \Phruts\Action\PlugInInterface */ $plugIn = \Phruts\Util\ClassLoader::newInstance($plugInConfig->getClassName(), '\\Phruts\\Action\\PlugInInterface'); \Phruts\Util\BeanUtils::populate($plugIn, $plugInConfig->getProperties()); $plugIn->init($this, $config); $plugIns[] = $plugIn; } catch (\Exception $e) { $msg = $this->getInternal()->getMessage(null, 'plugIn.init', $plugInConfig->getClassName()); if (!empty($this->log)) { $this->log->error($msg . ' - ' . $e->getMessage()); } throw new \Phruts\Exception($msg); } } $this->application[\Phruts\Util\Globals::PLUG_INS_KEY . $config->getPrefix()] = $plugIns; }