Skip to content

Commit

Permalink
Merge branch '2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Jul 21, 2017
2 parents 7041929 + e960ef6 commit 8044dd8
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Expand Up @@ -34,7 +34,7 @@ install:

script:
- if [[ $coverage = 1 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-php build/cov/coverage-phpunit.cov; else vendor/bin/phpunit; fi
- if [[ $coverage = 1 ]]; then for f in $(find features -name '*.feature'); do FEATURE=${f//\//_} phpdbg -qrr vendor/bin/behat --profile coverage $f || exit $?; done; else vendor/bin/behat; fi
- if [[ $coverage = 1 ]]; then for f in $(find features -name '*.feature'); do FEATURE=${f//\//_} phpdbg -qrr vendor/bin/behat --format=progress --profile coverage $f || exit $?; done; else vendor/bin/behat --format=progress; fi
- if [[ $coverage = 1 ]]; then phpdbg -qrr phpcov.phar merge --clover build/logs/clover.xml build/cov; fi
- tests/Fixtures/app/console api:swagger:export > swagger.json && swagger validate swagger.json && rm swagger.json
- if [[ $lint = 1 ]]; then php php-cs-fixer.phar fix --dry-run --diff --no-ansi; fi
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -25,6 +25,14 @@
* Add compatibility with Symfony Flex and Symfony 4
* Allow the Symfony Dependency Injection component to autoconfigure data providers and query extensions

## 2.0.10

* Performance improvement
* Swagger: Allow non-numeric IDs (such as UUIDs) in URLs
* Fix a bug when a composite identifier is missing
* `ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter::extractProperties` now always return an array
* Fix NelmioApiDocParser recursive relations

## 2.0.9

* Add support for Symfony 3.3
Expand Down
2 changes: 1 addition & 1 deletion src/Annotation/ApiResource.php
Expand Up @@ -29,7 +29,7 @@ final class ApiResource
public $shortName;

/**
* @var string|null
* @var string
*/
public $description;

Expand Down
16 changes: 8 additions & 8 deletions src/Metadata/Property/Factory/CachedPropertyMetadataFactory.php
Expand Up @@ -28,7 +28,7 @@ final class CachedPropertyMetadataFactory implements PropertyMetadataFactoryInte

private $cacheItemPool;
private $decorated;
private $memoryCache = [];
private $localCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetadataFactoryInterface $decorated)
{
Expand All @@ -41,18 +41,18 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyMetad
*/
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
{
$localKey = serialize([$resourceClass, $property, $options]);
if (isset($this->memoryCache[$localKey])) {
return $this->memoryCache[$localKey];
$localCacheKey = serialize([$resourceClass, $property, $options]);
if (isset($this->localCache[$localCacheKey])) {
return $this->localCache[$localCacheKey];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($localKey);
$cacheKey = self::CACHE_KEY_PREFIX.md5($localCacheKey);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);

if ($cacheItem->isHit()) {
return $this->memoryCache[$localKey] = $cacheItem->get();
return $this->localCache[$localCacheKey] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -61,12 +61,12 @@ public function create(string $resourceClass, string $property, array $options =
$propertyMetadata = $this->decorated->create($resourceClass, $property, $options);

if (!isset($cacheItem)) {
return $this->memoryCache[$localKey] = $propertyMetadata;
return $this->localCache[$localCacheKey] = $propertyMetadata;
}

$cacheItem->set($propertyMetadata);
$this->cacheItemPool->save($cacheItem);

return $this->memoryCache[$localKey] = $propertyMetadata;
return $this->localCache[$localCacheKey] = $propertyMetadata;
}
}
Expand Up @@ -28,7 +28,7 @@ final class CachedPropertyNameCollectionFactory implements PropertyNameCollectio

private $cacheItemPool;
private $decorated;
private $memoryCache = [];
private $localCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyNameCollectionFactoryInterface $decorated)
{
Expand All @@ -41,18 +41,18 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, PropertyNameC
*/
public function create(string $resourceClass, array $options = []): PropertyNameCollection
{
$localKey = serialize([$resourceClass, $options]);
if (isset($this->memoryCache[$localKey])) {
return $this->memoryCache[$localKey];
$localCacheKey = serialize([$resourceClass, $options]);
if (isset($this->localCache[$localCacheKey])) {
return $this->localCache[$localCacheKey];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($localKey);
$cacheKey = self::CACHE_KEY_PREFIX.md5($localCacheKey);

try {
$cacheItem = $this->cacheItemPool->getItem($cacheKey);

if ($cacheItem->isHit()) {
return $this->memoryCache[$localKey] = $cacheItem->get();
return $this->localCache[$localCacheKey] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -61,12 +61,12 @@ public function create(string $resourceClass, array $options = []): PropertyName
$propertyNameCollection = $this->decorated->create($resourceClass, $options);

if (!isset($cacheItem)) {
return $this->memoryCache[$localKey] = $propertyNameCollection;
return $this->localCache[$localCacheKey] = $propertyNameCollection;
}

$cacheItem->set($propertyNameCollection);
$this->cacheItemPool->save($cacheItem);

return $this->memoryCache[$localKey] = $propertyNameCollection;
return $this->localCache[$localCacheKey] = $propertyNameCollection;
}
}
12 changes: 6 additions & 6 deletions src/Metadata/Resource/Factory/CachedResourceMetadataFactory.php
Expand Up @@ -28,7 +28,7 @@ final class CachedResourceMetadataFactory implements ResourceMetadataFactoryInte

private $cacheItemPool;
private $decorated;
private $memoryCache = [];
private $localCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceMetadataFactoryInterface $decorated)
{
Expand All @@ -41,8 +41,8 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceMetad
*/
public function create(string $resourceClass): ResourceMetadata
{
if (isset($this->memoryCache[$resourceClass])) {
return $this->memoryCache[$resourceClass];
if (isset($this->localCache[$resourceClass])) {
return $this->localCache[$resourceClass];
}

$cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass);
Expand All @@ -51,7 +51,7 @@ public function create(string $resourceClass): ResourceMetadata
$cacheItem = $this->cacheItemPool->getItem($cacheKey);

if ($cacheItem->isHit()) {
return $this->memoryCache[$resourceClass] = $cacheItem->get();
return $this->localCache[$resourceClass] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -60,12 +60,12 @@ public function create(string $resourceClass): ResourceMetadata
$resourceMetadata = $this->decorated->create($resourceClass);

if (!isset($cacheItem)) {
return $this->memoryCache[$resourceClass] = $resourceMetadata;
return $this->localCache[$resourceClass] = $resourceMetadata;
}

$cacheItem->set($resourceMetadata);
$this->cacheItemPool->save($cacheItem);

return $this->memoryCache[$resourceClass] = $resourceMetadata;
return $this->localCache[$resourceClass] = $resourceMetadata;
}
}
Expand Up @@ -28,7 +28,7 @@ final class CachedResourceNameCollectionFactory implements ResourceNameCollectio

private $cacheItemPool;
private $decorated;
private $memoryCache = [];
private $localCache = [];

public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceNameCollectionFactoryInterface $decorated)
{
Expand All @@ -41,15 +41,15 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, ResourceNameC
*/
public function create(): ResourceNameCollection
{
if (isset($this->memoryCache[self::CACHE_KEY])) {
return $this->memoryCache[self::CACHE_KEY];
if (isset($this->localCache[self::CACHE_KEY])) {
return $this->localCache[self::CACHE_KEY];
}

try {
$cacheItem = $this->cacheItemPool->getItem(self::CACHE_KEY);

if ($cacheItem->isHit()) {
return $this->memoryCache[self::CACHE_KEY] = $cacheItem->get();
return $this->localCache[self::CACHE_KEY] = $cacheItem->get();
}
} catch (CacheException $e) {
// do nothing
Expand All @@ -58,12 +58,12 @@ public function create(): ResourceNameCollection
$resourceNameCollection = $this->decorated->create();

if (!isset($cacheItem)) {
return $this->memoryCache[self::CACHE_KEY] = $resourceNameCollection;
return $this->localCache[self::CACHE_KEY] = $resourceNameCollection;
}

$cacheItem->set($resourceNameCollection);
$this->cacheItemPool->save($cacheItem);

return $this->memoryCache[self::CACHE_KEY] = $resourceNameCollection;
return $this->localCache[self::CACHE_KEY] = $resourceNameCollection;
}
}
32 changes: 32 additions & 0 deletions tests/Annotation/AnnotatedClass.php
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Tests\Annotation;

use ApiPlatform\Core\Annotation\ApiResource;

/**
* @ApiResource(
* shortName="shortName",
* description="description",
* iri="http://example.com/res",
* itemOperations={"foo":{"bar"}},
* collectionOperations={"bar":{"foo"}},
* attributes={"foo":"bar"}
* )
*
* @author Marcus Speight <marcus@pmconnect.co.uk>
*/
class AnnotatedClass
{
}
13 changes: 13 additions & 0 deletions tests/Annotation/ApiResourceTest.php
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Tests\Annotation;

use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\Common\Annotations\AnnotationReader;

/**
* @author Kévin Dunglas <dunglas@gmail.com>
Expand All @@ -36,4 +37,16 @@ public function testAssignation()
$this->assertEquals(['bar' => ['foo']], $resource->collectionOperations);
$this->assertEquals(['foo' => 'bar'], $resource->attributes);
}

public function testApiResourceAnnotation()
{
$reader = new AnnotationReader();
$resource = $reader->getClassAnnotation(new \ReflectionClass(AnnotatedClass::class), ApiResource::class);

$this->assertEquals('shortName', $resource->shortName);
$this->assertEquals('description', $resource->description);
$this->assertEquals('http://example.com/res', $resource->iri);
$this->assertEquals(['bar' => ['foo']], $resource->collectionOperations);
$this->assertEquals(['foo' => 'bar'], $resource->attributes);
}
}

0 comments on commit 8044dd8

Please sign in to comment.