ApiPlatform\Core\Bridge\Symfony\Routing\CachedRouteNameResolver::getRouteName PHP Method

getRouteName() public method

public getRouteName ( string $resourceClass, boolean $collection ) : string
$resourceClass string
$collection boolean
return string
    public function getRouteName(string $resourceClass, bool $collection) : string
    {
        $cacheKey = self::CACHE_KEY_PREFIX . md5(serialize([$resourceClass, $collection]));
        try {
            $cacheItem = $this->cacheItemPool->getItem($cacheKey);
            if ($cacheItem->isHit()) {
                return $cacheItem->get();
            }
        } catch (CacheException $e) {
            // do nothing
        }
        $routeName = $this->decorated->getRouteName($resourceClass, $collection);
        if (!isset($cacheItem)) {
            return $routeName;
        }
        try {
            $cacheItem->set($routeName);
            $this->cacheItemPool->save($cacheItem);
        } catch (CacheException $e) {
            // do nothing
        }
        return $routeName;
    }

Usage Example

 public function testGetRouteNameForCollectionRouteOnCacheHit()
 {
     $cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
     $cacheItemProphecy->isHit()->willReturn(true)->shouldBeCalled();
     $cacheItemProphecy->get()->willReturn('some_collection_route')->shouldBeCalled();
     $cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
     $cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy);
     $cacheItemPoolProphecy->save($cacheItemProphecy)->shouldNotBeCalled();
     $decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
     $decoratedProphecy->getRouteName(Argument::cetera())->shouldNotBeCalled();
     $cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
     $actual = $cachedRouteNameResolver->getRouteName('AppBundle\\Entity\\User', true);
     $this->assertSame('some_collection_route', $actual);
 }
CachedRouteNameResolver