ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyNameCollectionFactory::create PHP Method

create() public method

public create ( string $resourceClass, array $options = [] ) : PropertyNameCollection
$resourceClass string
$options array
return ApiPlatform\Core\Metadata\Property\PropertyNameCollection
    public function create(string $resourceClass, array $options = []) : PropertyNameCollection
    {
        $cacheKey = self::CACHE_KEY_PREFIX . md5(serialize([$resourceClass, $options]));
        try {
            $cacheItem = $this->cacheItemPool->getItem($cacheKey);
            if ($cacheItem->isHit()) {
                return $cacheItem->get();
            }
        } catch (CacheException $e) {
            // do nothing
        }
        $propertyNameCollection = $this->decorated->create($resourceClass, $options);
        if (!isset($cacheItem)) {
            return $propertyNameCollection;
        }
        $cacheItem->set($propertyNameCollection);
        $this->cacheItemPool->save($cacheItem);
        return $propertyNameCollection;
    }

Usage Example

 public function testCreateWithGetCacheItemThrowsCacheException()
 {
     $decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
     $decoratedPropertyNameCollectionFactory->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummy']))->shouldBeCalled();
     $cacheException = $this->prophesize(CacheException::class);
     $cacheException->willExtend(\Exception::class);
     $cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
     $cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalled();
     $cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
     $resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);
     $this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
     $this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);
 }
CachedPropertyNameCollectionFactory