Esempio n. 1
0
File: Chain.php Progetto: stunti/zf2
 /**
  * Matches a user submitted path with a previously defined route.
  * Assigns and returns an array of defaults on a successful match.
  *
  * @param  \Zend\Controller\Request\HTTP $request Request to get the path info from
  * @return array|false An array of assigned values or a false on a mismatch
  */
 public function match($request, $partial = null)
 {
     $path = trim($request->getPathInfo(), '/');
     $subPath = $path;
     $values = array();
     foreach ($this->_routes as $key => $route) {
         if ($key > 0 && $matchedPath !== null) {
             $separator = substr($subPath, 0, strlen($this->_separators[$key]));
             if ($separator !== $this->_separators[$key]) {
                 return false;
             }
             $subPath = substr($subPath, strlen($separator));
         }
         // TODO: Should be an interface method. Hack for 1.0 BC
         if (!method_exists($route, 'getVersion') || $route->getVersion() == 1) {
             $match = $subPath;
         } else {
             $request->setPathInfo($subPath);
             $match = $request;
         }
         $res = $route->match($match, true);
         if ($res === false) {
             return false;
         }
         $matchedPath = $route->getMatchedPath();
         if ($matchedPath !== null) {
             $subPath = substr($subPath, strlen($matchedPath));
             $separator = substr($subPath, 0, strlen($this->_separators[$key]));
         }
         $values = $res + $values;
     }
     $request->setPathInfo($path);
     if ($subPath !== '' && $subPath !== false) {
         return false;
     }
     return $values;
 }
Esempio n. 2
0
 public function __construct($uri = null)
 {
     if (null === $uri) {
         $uri = 'http://localhost/foo/bar/baz/2';
     }
     //$uri = \Zend\URI\URL::fromString($uri);
     $url = new \Zend\URI\URL($uri);
     $this->_host = $url->getHost();
     $this->_port = $url->getPort();
     parent::__construct($url);
 }
Esempio n. 3
0
 public function testHelperPullsResponseFromFrontControllerWithNoRegisteredActionController()
 {
     $helper = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $this->assertNull($helper->getActionController());
     $aRequest = new Request\HTTP();
     $aRequest->setModuleName('default')->setControllerName('zend_controller_action_helper-broker')->setActionName('index');
     $aResponse = new Response\Cli();
     $fRequest = new Request\HTTP();
     $fRequest->setModuleName('foo')->setControllerName('foo-bar')->setActionName('baz');
     $fResponse = new Response\Cli();
     $this->front->setRequest($fRequest)->setResponse($fResponse);
     $hRequest = $helper->getRequest();
     $this->assertNotSame($hRequest, $aRequest);
     $this->assertSame($hRequest, $fRequest);
     $hResponse = $helper->getResponse();
     $this->assertNotSame($hResponse, $aResponse);
     $this->assertSame($hResponse, $fResponse);
 }
Esempio n. 4
0
 public function testShouldAllowRetrievingCurrentModuleDirectory()
 {
     $this->testAddModuleDirectory();
     $request = new Request\HTTP();
     $request->setModuleName('bar');
     $this->_controller->setRequest($request);
     $dir = $this->_controller->getModuleDirectory();
     $this->assertContains('modules' . DIRECTORY_SEPARATOR . 'bar', $dir);
     $this->assertNotContains('controllers', $dir);
 }
Esempio n. 5
0
 /**
  * @group ZF-9091
  */
 public function testSetPathInfoShouldNotStripBaseUrlIfBaseUrlNotInRequestUri()
 {
     $request = new Request\HTTP();
     $request->setBaseUrl('/app');
     $_SERVER['REQUEST_URI'] = '/index/index';
     $pathInfo = $request->getPathInfo();
     $this->assertEquals('/index/index', $pathInfo);
 }
Esempio n. 6
0
 /**
  * Matches a user submitted path with parts defined by a map. Assigns and
  * returns an array of variables on a successful match.
  *
  * @param \Zend\Controller\Request\HTTP $request Request to get the host from
  * @return array|false An array of assigned values or a false on a mismatch
  */
 public function match($request)
 {
     // Check the scheme if required
     if ($this->_scheme !== null) {
         $scheme = $request->getScheme();
         if ($scheme !== $this->_scheme) {
             return false;
         }
     }
     // Get the host and remove unnecessary port information
     $host = $request->getHTTPHost();
     if (preg_match('#:\\d+$#', $host, $result) === 1) {
         $host = substr($host, 0, -strlen($result[0]));
     }
     $hostStaticCount = 0;
     $values = array();
     $host = trim($host, '.');
     if ($host != '') {
         $host = explode('.', $host);
         foreach ($host as $pos => $hostPart) {
             // Host is longer than a route, it's not a match
             if (!array_key_exists($pos, $this->_parts)) {
                 return false;
             }
             $name = isset($this->_variables[$pos]) ? $this->_variables[$pos] : null;
             $hostPart = urldecode($hostPart);
             // If it's a static part, match directly
             if ($name === null && $this->_parts[$pos] != $hostPart) {
                 return false;
             }
             // If it's a variable with requirement, match a regex. If not - everything matches
             if ($this->_parts[$pos] !== null && !preg_match($this->_regexDelimiter . '^' . $this->_parts[$pos] . '$' . $this->_regexDelimiter . 'iu', $hostPart)) {
                 return false;
             }
             // If it's a variable store it's value for later
             if ($name !== null) {
                 $values[$name] = $hostPart;
             } else {
                 $hostStaticCount++;
             }
         }
     }
     // Check if all static mappings have been matched
     if ($this->_staticCount != $hostStaticCount) {
         return false;
     }
     $return = $values + $this->_defaults;
     // Check if all map variables have been initialized
     foreach ($this->_variables as $var) {
         if (!array_key_exists($var, $return)) {
             return false;
         }
     }
     $this->_values = $values;
     return $return;
 }
Esempio n. 7
0
 public function testRenderUsingViewRenderer()
 {
     HelperBroker\HelperBroker::addHelper(new Helper\ViewRenderer());
     $request = new Request\HTTP();
     $request->setControllerName('view')->setActionName('script');
     $response = new Response\Cli();
     Controller\Front::getInstance()->setControllerDirectory(dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files');
     $controller = new \ViewController($request, $response);
     $controller->scriptAction();
     $this->assertContains('Inside custom/renderScript.php', $response->getBody());
 }
Esempio n. 8
0
 /**
  * @see ZF-2693
  */
 public function testForcingCamelCasedActionsNotRequestedWithWordSeparatorsShouldRaiseNotices()
 {
     $this->_dispatcher->setParam('useCaseSensitiveActions', true);
     $request = new Request\HTTP();
     $request->setModuleName('admin');
     $request->setControllerName('foo-bar');
     $request->setActionName('bazBat');
     $this->assertTrue($this->_dispatcher->isDispatchable($request), var_export($this->_dispatcher->getControllerDirectory(), 1));
     $response = new Response\Cli();
     set_error_handler(array($this, 'handleErrors'));
     try {
         $this->_dispatcher->dispatch($request, $response);
         $body = $this->_dispatcher->getResponse()->getBody();
         restore_error_handler();
         $this->assertTrue(isset($this->error));
         $this->assertContains('deprecated', $this->error);
     } catch (Controller\Exception $e) {
         restore_error_handler();
         $this->fail('camelCased actions should succeed when forced');
     }
 }