示例#1
0
 /**
  * match(): defined by Route interface
  *
  * @see    Route::match()
  * @param  HttpRequest $request
  * @param  integer     $pathOffset
  * @return boolean
  */
 public function match(HttpRequest $request, $pathOffset = null)
 {
     if ($pathOffset !== null) {
         $result = preg_match('(\\G' . $this->_regex . ')i', $request->getRequestUri(), $match, null, $pathOffset);
     } else {
         $result = preg_match('(^' . $this->_regex . '$)i', $request->getRequestUri(), $match);
     }
     if ($result === null) {
         return null;
     }
     // @todo: examine $match
     return $this->_defaults;
 }
示例#2
0
 /**
  * match(): defined by Route interface
  *
  * @see    Route::match()
  * @param  HttpRequest $request
  * @param  integer     $pathOffset
  * @return boolean
  */
 public function match(HttpRequest $request, $pathOffset = null)
 {
     if ($pathOffset !== null) {
         if (strpos($request->getRequestUri(), $this->_route) === $pathOffset) {
             return $this->_defaults;
         }
     } else {
         if ($request->getRequestUri() === $this->_route) {
             return $this->_defaults;
         }
     }
     return null;
 }
示例#3
0
    /**
     * 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;
    }
示例#4
0
文件: ChainTest.php 项目: narixx/zf2
 public function __construct($uri = null)
 {
     if (null === $uri) {
         $uri = 'http://localhost/foo/bar/baz/2';
     }
     $uri = new \Zend\Uri\Url($uri);
     $this->_host = $uri->getHost();
     $this->_port = $uri->getPort();
     parent::__construct($uri);
 }
示例#5
0
 /**
  * @group ZF-2910
  */
 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);
 }
示例#6
0
 public function testRenderUsingViewRenderer()
 {
     HelperBroker::addHelper(new Helper\ViewRenderer());
     $request = new Request\Http();
     $request->setControllerName('view')->setActionName('script');
     $response = new Response\Cli();
     Controller\Front::getInstance()->setControllerDirectory(__DIR__ . DIRECTORY_SEPARATOR . '_files');
     $controller = new \ViewController($request, $response);
     $controller->scriptAction();
     $this->assertContains('Inside custom/renderScript.php', $response->getBody());
 }
示例#7
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;
 }
示例#8
0
    public function __construct($uri = null)
    {
        if (null === $uri) {
            $uri = 'http://localhost/foo/bar/baz/2';
        }

        $uri = UriFactory::factory($uri, 'http');
        $this->_host = $uri->getHost();
        $this->_port = $uri->getPort();

        parent::__construct($uri);
    }
示例#9
0
    /**
     * @group 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');
        }
    }
示例#10
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);
 }
示例#11
0
 public function testHelperPullsResponseFromFrontControllerWithNoRegisteredActionController()
 {
     $helper = HelperBroker::getStaticHelper('viewRenderer');
     $this->assertNull($helper->getActionController());
     $aRequest = new Request();
     $aRequest->setModuleName('default')->setControllerName('zend_controller_action_helper-broker')->setActionName('index');
     $aResponse = new Response();
     $fRequest = new Request();
     $fRequest->setModuleName('foo')->setControllerName('foo-bar')->setActionName('baz');
     $fResponse = new Response();
     $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);
 }