protected function checkRoute( $selectedController, $selectedAction )
 {
     if (self::$parsedSkipRoutes === null )
     {
         self::$parsedSkipRoutes = array();
         foreach ( self::$skipRoutes as $routeRule )
         {
             list( $routeController, $routeAction ) = explode( '_', $routeRule[0] );
             $routeVersion = isset( $routeRule[1] ) ? (int)$routeRule[1] : 1;
             self::$parsedSkipRoutes[$routeController][$routeAction] = $routeVersion;
         }
     }
     $retVal = false;
     if ( isset( self::$parsedSkipRoutes[$selectedController] ) )
     {
         if ( isset( self::$parsedSkipRoutes[$selectedController][$selectedAction] ) )
         {
             $retVal = self::$parsedSkipRoutes[$selectedController][$selectedAction] === ezpRestPrefixFilterInterface::getApiVersion();
         }
         else if ( isset( self::$parsedSkipRoutes[$selectedController]['*'] ) )
         {
             $retVal = self::$parsedSkipRoutes[$selectedController]['*'] === ezpRestPrefixFilterInterface::getApiVersion();
         }
     }
     return !$retVal;
 }
 public function matches(ezcMvcRequest $request)
 {
     // IF we put versionToken back into route pattern, then the following is true
     // new ezcMvcRailsRoute( '/api/:versionToken/foo', 'myController', 'myAction' ),
     // the token is available at: $request->variables['versionToken']
     // Which means, specifying it in the route pattern, allows us to reuse more of the current MvcTols code.
     // But results in more code and configuration up-front for developers.
     //
     // In the case of ezpRestRequest, a specific getVersion() could also be implemented.
     // /api/v1 + /foo ezpRestVersionedRailsRoute
     // /api/v1/foo -> ezPrestVersionedRailsRoute /api/v1/foo -> /foo
     // matches() ==> is this version string registered? if so call it, if not call the default, as if no version info is provided or fail?
     switch (ezpRestPrefixFilterInterface::getApiVersion()) {
         case $this->version:
             return $this->route->matches($request);
             break;
     }
 }
예제 #3
0
    /**
     * Creates a new cache storage for a given location through eZ Publish cluster mechanism
     * Options can contain the 'ttl' ( Time-To-Life ). This is per default set
     * to 1 day.
     * @param string $location Path to the cache location inside the cluster
     * @param array(string=>string) $options Options for the cache.
     */
    public function __construct( $location, $options = array() )
    {
        $apiName = ezpRestPrefixFilterInterface::getApiProviderName();
        $apiVersion = ezpRestPrefixFilterInterface::getApiVersion();
        $location = eZSys::cacheDirectory().'/rest/'.$location;
        if( !file_exists( $location ) )
        {
            if( !eZDir::mkdir( $location, false, true ) )
            {
                throw new ezcBaseFilePermissionException(
                    $location,
                    ezcBaseFileException::WRITE,
                    'Cache location is not writeable.'
                );
            }
        }

        parent::__construct( $location );
        $this->properties['options'] = new ezpCacheStorageClusterOptions( $options );
    }
 /**
  * @group restApplicationCache
  * @group restCache
  * @dataProvider cacheIdVariableProvider
  * @param array $internalVariables
  * @param array $contentVariables
  */
 public function testGenerateCacheId(array $internalVariables, array $contentVariables)
 {
     /*
      * The cache ID is a MD5 hash and takes into account :
      *  - API Name
      *  - API Version
      *  - Controller class
      *  - Action
      *  - Internal variables (passed parameters, ResponseGroups...)
      *  - Content variables (Translation...)
      */
     $request = new ezpRestRequest();
     $request->uri = $this->restINI->variable('System', 'ApiPrefix') . '/test/rest/foo';
     $request->variables = $internalVariables;
     $request->contentVariables = $contentVariables;
     $request->protocol = 'http-get';
     $controller = $this->getTestControllerFromRequest($request);
     $routingInfos = $controller->getRouter()->getRoutingInformation();
     /*
      * Reproduce the hash algorythm
      */
     $aCacheId = array(ezpRestPrefixFilterInterface::getApiProviderName(), ezpRestPrefixFilterInterface::getApiVersion(), $routingInfos->controllerClass, $routingInfos->action);
     foreach ($contentVariables + $internalVariables as $name => $val) {
         if (is_array($val)) {
             $aCacheId[] = $name . '=' . implode(',', $val);
         } else {
             $aCacheId[] = $name . '=' . $val;
         }
     }
     $hashedCacheId = md5(implode('-', $aCacheId));
     $refObj = new ReflectionObject($controller);
     $refMethod = $refObj->getMethod('generateCacheId');
     $refMethod->setAccessible(true);
     self::assertSame($hashedCacheId, $refMethod->invoke($controller), 'Cache ID algo must take into account : API Name, API Version, Controller class, Action, Internal variables, Content variables');
     // Compare currently generated hash with the previous one. Must be different
     static $previousHash;
     self::assertNotEquals($hashedCacheId, $previousHash, 'Cache IDs must be unique !');
     $previousHash = $hashedCacheId;
 }
 /**
  * Generates unique cache ID for current request.
  *
  * The cache ID is a MD5 hash and takes into account :
  *  - API Name
  *  - API Version
  *  - Controller class
  *  - Action
  *  - Internal variables (passed parameters, ResponseGroups...)
  *  - Content variables (Translation...)
  *
  * @return string
  */
 private function generateCacheId()
 {
     $routingInfos = $this->getRouter()->getRoutingInformation();
     $aCacheId = array(ezpRestPrefixFilterInterface::getApiProviderName(), ezpRestPrefixFilterInterface::getApiVersion(), $routingInfos->controllerClass, $routingInfos->action);
     // Add internal variables, caught in the URL. See ezpRestHttpRequestParser::fillVariables()
     // Also add content variables
     foreach ($this->request->contentVariables + $this->request->variables as $name => $val) {
         $aCacheId[] = $name . '=' . (is_array($val) ? implode(',', $val) : $val);
     }
     return md5(implode('-', $aCacheId));
 }
예제 #6
0
    /**
     * Generates unique cache ID for current request.
     * The cache ID is a MD5 hash and takes into account :
     *  - API Name
     *  - API Version
     *  - Controller class
     *  - Action
     *  - Internal variables (passed parameters, ResponseGroups...)
     *  - Content variables (Translation...)
     * @return string
     */
    private function generateCacheId()
    {
        $apiName = ezpRestPrefixFilterInterface::getApiProviderName();
        $apiVersion = ezpRestPrefixFilterInterface::getApiVersion();
        $routingInfos = $this->getRouter()->getRoutingInformation();

        $aCacheId = array( $apiName, $apiVersion, $routingInfos->controllerClass, $routingInfos->action );
        // Add internal variables, caught in the URL. See ezpRestHttpRequestParser::fillVariables()
        // Also add content variables
        $allInternalVariables = array_merge( $this->request->variables, $this->getAllContentVariables() );
        foreach( $allInternalVariables as $name => $val )
        {
            if( is_array( $val ) )
                $aCacheId[] = $name.'='.implode( ',', $val );
            else
                $aCacheId[] = $name.'='.$val;
        }

        $cacheId = implode( '-', $aCacheId );
        return md5( $cacheId );
    }