Skip to content

Commit

Permalink
Update from internal 0.18 release
Browse files Browse the repository at this point in the history
  • Loading branch information
alenpokos committed Dec 9, 2020
1 parent 04c7cac commit b77abe7
Show file tree
Hide file tree
Showing 128 changed files with 5,154 additions and 469 deletions.
54 changes: 54 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,60 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Changed
- Form validation errors now generate pointers from cause instead of origin

### Fixed
- Form validation errors pointers now correctly target deep properties
- Specified includes now get properly included on create requests

## [0.17.5] 2019-11-15

### Added
- Added CI checks for symfony 4.3

### Fixed
- Show routes with additional parameters now generate properly
- CI checks for Symfony 3.4 now uses same version router component

### Changed
- Show route calculations no longer use route collection but instead use convention naming
- Self url calculation no longer uses route collection but instead uses current route parameters

## [0.17.4] 2019-10-21

### Added
- Support for relationship editing trait, see `src/Resources/doc/flow/write_actions.md#updaterelationship`

### Depretacted
- dropped support for symfony versions 3.1, 3.2, 3.3

## [0.17.3] 2019-09-16

### Changed
- default content type in responses changed to `application/vnd.api+json`

## [0.17.2] 2019-08-28

### Fixed
- Create trait uses property accessor to get ID instead of expecting getId method

## [0.17.1] 2019-08-22

### Fixed
- JsonApi controller check to allow for callable
- Form errors now provide correct code from violation

## [0.17.0] 2019-08-09

### Added
- The `\Trikoder\JsonApiBundle\Listener\KernelListener` listener priorities for the
`kernel.view` and `kernel.exception` events can now be configured via the extension's
`kernel_listener_on_kernel_view_priority` and `kernel_listener_on_kernel_exception_priority`
keys respectively.
- `Trikoder\JsonApiBundle\Schema\Builtin\GenericSchema` to be used for exposing 1:1 any resource
- `Trikoder\JsonApiBundle\Services\Client\ResponseBodyDecoder` to be used as client for jsonapi response

## [0.16.0] 2019-02-14

### Added
Expand Down
45 changes: 23 additions & 22 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,39 @@
"type": "symfony-bundle",
"require": {
"php": ">=7.0.0|>=7.1.0",
"symfony/framework-bundle": ">=3.1 <5",
"symfony/dependency-injection": ">=3.1 <5",
"symfony/config": ">=3.1 <5",
"sensio/framework-extra-bundle": ">3.0|^5.1|^5.2",
"symfony/http-foundation": ">=3.1 <5",
"symfony/form": ">=3.1 <5.0",
"symfony/routing": ">=3.1 <4.1.8",
"symfony/security-bundle": "^3.1|^4.0",
"symfony/translation": ">=3.1 <5",
"symfony/validator": ">=3.1 <5.0",
"symfony/monolog-bundle": ">=3.1 <5.0",
"symfony/framework-bundle": "^3.4|^4.4",
"symfony/dependency-injection": "^3.4|^4.4",
"symfony/config": "^3.4|^4.4",
"sensio/framework-extra-bundle": "^5.3",
"symfony/http-foundation": "^3.4|^4.4",
"symfony/form": "^3.4|^4.4",
"symfony/routing": "^3.4|^4.4",
"symfony/security-bundle": "^3.4|^4.4",
"symfony/translation": "^3.4|^4.4",
"symfony/validator": "^3.4|^4.4",
"symfony/monolog-bundle": "^3.4",
"neomerx/json-api": "^1.0",
"doctrine/orm": "^2.4",
"doctrine/common": "^2",
"doctrine/doctrine-bundle": "~1.6|^1.8|^1.9",
"ext-json": "*"
"doctrine/doctrine-bundle": "^1.11|^2.0",
"ext-json": "*",
"symfony/property-access": "^3.4|^4.4"
},
"require-dev": {
"phpunit/phpunit": "^5.7",
"phpunit/php-code-coverage": "^4.0",
"symfony/phpunit-bridge": "^3.0|^4.0",
"symfony/phpunit-bridge": "^3.4|^4.4",
"justinrainbow/json-schema": "^1.6",
"doctrine/doctrine-fixtures-bundle": "^2.3",
"doctrine/doctrine-fixtures-bundle": "^3.3",
"fzaninotto/faker": "^1.5",
"sensiolabs/security-checker": "^4.1",
"symfony/twig-bundle": "^3.3|^4.0",
"sensiolabs/security-checker": "^5",
"symfony/twig-bundle": "^3.4|^4.4",
"doctrine/cache": "^1.6",
"symfony/debug-bundle": "^3.3|^4.0",
"symfony/web-profiler-bundle": "^3.3|^4.0",
"symfony/web-server-bundle": "^3.3|^4.0",
"symfony/browser-kit": "^3.3|^4.0",
"friendsofphp/php-cs-fixer": "2.13.0"
"symfony/debug-bundle": "^3.4|^4.4",
"symfony/web-profiler-bundle": "^3.4|^4.4",
"symfony/web-server-bundle": "^3.4|^4.4",
"symfony/browser-kit": "^3.4|^4.4",
"friendsofphp/php-cs-fixer": "^2.13"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<server name="KERNEL_DIR" value="tests/Resources/app" />
<server name="KERNEL_CLASS" value="\AppKernel" />
<!-- \Doctrine\Common\ClassLoader is deprecated in doctrine/common v2.9.0 -->
<env name="SYMFONY_DEPRECATIONS_HELPER" value="1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0&amp;verbose=0" />
</php>
<testsuites>
<testsuite name="Unit">
Expand Down
98 changes: 95 additions & 3 deletions src/Bridge/Doctrine/DoctrineRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Mapping\MappingException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Trikoder\JsonApiBundle\Contracts\RelationshipDoesNotExistException;
use Trikoder\JsonApiBundle\Contracts\RelationshipRepositoryInterface;
use Trikoder\JsonApiBundle\Contracts\RepositoryInterface;
use Trikoder\JsonApiBundle\Contracts\ResourceDoesNotExistException;

/**
* Class DoctrineRepository
*/
class DoctrineRepository implements RepositoryInterface
class DoctrineRepository implements RepositoryInterface, RelationshipRepositoryInterface
{
/**
* @var EntityRepository
Expand All @@ -20,13 +25,22 @@ class DoctrineRepository implements RepositoryInterface
*/
protected $entityManager;

/**
* @var PropertyAccessorInterface
*/
protected $propertyAccessor;

/**
* DoctrineRepository constructor.
*/
public function __construct(EntityRepository $entityRepository, EntityManager $entityManager)
{
public function __construct(
EntityRepository $entityRepository,
EntityManager $entityManager,
PropertyAccessorInterface $propertyAccessor
) {
$this->entityRepository = $entityRepository;
$this->entityManager = $entityManager;
$this->propertyAccessor = $propertyAccessor;
}

/**
Expand Down Expand Up @@ -75,4 +89,82 @@ public function remove($model)
$this->entityManager->remove($model);
$this->entityManager->flush();
}

/**
* {@inheritdoc}
*/
public function addToRelationship($model, string $relationshipName, array $relationshipData)
{
$modelMeta = $this->entityManager->getClassMetadata($this->entityRepository->getClassName());

try {
$relationshipModelClassName = $modelMeta->getAssociationMapping($relationshipName)['targetEntity'];
} catch (MappingException $e) {
throw new RelationshipDoesNotExistException($relationshipName);
}

$relationshipModelMeta = $this->entityManager->getClassMetadata($relationshipModelClassName);
$relationshipIdentifier = $relationshipModelMeta->getSingleIdentifierFieldName();

$repository = $this->entityManager->getRepository($relationshipModelClassName);
$currentRelationshipModels = $this->propertyAccessor->getValue($model, $relationshipName);

$relationshipModels = [];

//add current relationship resources to array
foreach ($currentRelationshipModels as $data) {
$relationshipModels[$this->propertyAccessor->getValue($data, $relationshipIdentifier)] = $data;
}

$newRelationshipModels = $repository->findBy([$relationshipIdentifier => array_column($relationshipData, 'id')]);

/*
* @see https://gitlab.trikoder.net/trikoder/jsonapibundle/merge_requests/102#note_251076
*/
if (\count($newRelationshipModels) !== \count($relationshipData)) {
throw new ResourceDoesNotExistException();
}

//add new relationship resources to array
foreach ($newRelationshipModels as $data) {
$relationshipModels[$this->propertyAccessor->getValue($data, $relationshipIdentifier)] = $data;
}

$this->propertyAccessor->setValue($model, $relationshipName, $relationshipModels);

return $this->save($model);
}

/**
* {@inheritdoc}
*/
public function removeFromRelationship($model, string $relationshipName, array $relationshipData)
{
$modelMeta = $this->entityManager->getClassMetadata($this->entityRepository->getClassName());

try {
$relationshipModelClassName = $modelMeta->getAssociationMapping($relationshipName)['targetEntity'];
} catch (MappingException $e) {
throw new RelationshipDoesNotExistException($relationshipName);
}

$relationshipModelMeta = $this->entityManager->getClassMetadata($relationshipModelClassName);
$relationshipIdentifier = $relationshipModelMeta->getSingleIdentifierFieldName();

$relationshipModels = $this->propertyAccessor->getValue($model, $relationshipName);
$relationshipIds = array_column($relationshipData, 'id');

$this->propertyAccessor->setValue(
$model,
$relationshipName,
array_filter(
iterator_to_array($relationshipModels),
function ($data) use ($relationshipIdentifier, $relationshipIds) {
return !\in_array($this->propertyAccessor->getValue($data, $relationshipIdentifier), $relationshipIds);
}
)
);

return $this->save($model);
}
}
8 changes: 6 additions & 2 deletions src/Bridge/Doctrine/RepositoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Trikoder\JsonApiBundle\Bridge\Doctrine;

use Doctrine\ORM\EntityManager;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Trikoder\JsonApiBundle\Contracts\RepositoryInterface;
use Trikoder\JsonApiBundle\Repository\RepositoryFactoryInterface;

Expand All @@ -15,13 +16,15 @@ class RepositoryFactory implements RepositoryFactoryInterface
* @var EntityManager
*/
private $entityManager;
private $propertyAccessor;

/**
* RepositoryFactory constructor.
*/
public function __construct(EntityManager $entityManager)
public function __construct(EntityManager $entityManager, PropertyAccessorInterface $propertyAccessor)
{
$this->entityManager = $entityManager;
$this->propertyAccessor = $propertyAccessor;
}

/**
Expand All @@ -31,7 +34,8 @@ public function create(string $modelClass): RepositoryInterface
{
return new DoctrineRepository(
$this->entityManager->getRepository($modelClass),
$this->entityManager
$this->entityManager,
$this->propertyAccessor
);
}
}
15 changes: 15 additions & 0 deletions src/Config/Annotation/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ class Config
*/
public $requestBodyDecoder;

/**
* @var string
*/
public $relationshipRequestBodyDecoder;

/**
* @var string
*/
public $requestBodyValidator;

/**
* @var bool
*/
Expand All @@ -59,4 +69,9 @@ class Config
* @var Trikoder\JsonApiBundle\Config\Annotation\DeleteConfig
*/
public $delete;

/**
* @var Trikoder\JsonApiBundle\Config\Annotation\UpdateRelationshipConfig
*/
public $updateRelationship;
}
23 changes: 23 additions & 0 deletions src/Config/Annotation/UpdateRelationshipConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Trikoder\JsonApiBundle\Config\Annotation;

use Doctrine\Common\Annotations\Annotation;
use Doctrine\Common\Annotations\Annotation\Target;

/**
* @Annotation
* @Target("ANNOTATION")
*/
class UpdateRelationshipConfig
{
/**
* @return array|null
*/
public $allowedRelationships;

/**
* @return array|null
*/
public $requiredRoles;
}
Loading

0 comments on commit b77abe7

Please sign in to comment.