-
-
Notifications
You must be signed in to change notification settings - Fork 162
Use Repository managed by Doctrine automatically #982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 1.13
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -20,6 +20,24 @@ | |||||
<services> | ||||||
<defaults public="true" /> | ||||||
|
||||||
<service id="sylius.doctrine_orm.metadata.resource.metadata_collection_factory" | ||||||
class="Sylius\Resource\Doctrine\ORM\Metadata\Resource\Factory\DoctrineORMResourceMetadataCollectionFactory" | ||||||
decorates="sylius.resource_metadata_collection.factory" | ||||||
decoration-priority="200" | ||||||
> | ||||||
<argument type="service" id="doctrine" /> | ||||||
<argument type="service" id=".inner" /> | ||||||
</service> | ||||||
|
||||||
<service id="sylius.state_provider.doctrine.orm.state.provider" class="Sylius\Resource\Doctrine\ORM\State\Provider"> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Seems a bit superfluous |
||||||
<argument type="service" id="doctrine" /> | ||||||
<argument type="service" id="sylius.repository_argument_resolver.request" /> | ||||||
<argument type="service" id="sylius.expression_language.argument_parser.repository" /> | ||||||
<argument type="tagged_locator" tag="sylius.state_provider" /> | ||||||
<tag name="sylius.state_provider" /> | ||||||
</service> | ||||||
<service id="Sylius\Resource\Doctrine\ORM\State\Provider" alias="sylius.state_provider.doctrine.orm.state.provider" /> | ||||||
|
||||||
<service id="sylius.event_subscriber.orm_mapped_super_class" class="Sylius\Bundle\ResourceBundle\EventListener\ORMMappedSuperClassSubscriber"> | ||||||
<argument type="service" id="sylius.resource_registry" /> | ||||||
<tag name="doctrine.event_listener" event="loadClassMetadata" priority="8192" /> | ||||||
|
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,103 @@ | ||||||||
<?php | ||||||||
|
||||||||
/* | ||||||||
* This file is part of the Sylius package. | ||||||||
* | ||||||||
* (c) Sylius Sp. z o.o. | ||||||||
* | ||||||||
* For the full copyright and license information, please view the LICENSE | ||||||||
* file that was distributed with this source code. | ||||||||
*/ | ||||||||
|
||||||||
declare(strict_types=1); | ||||||||
|
||||||||
namespace Sylius\Resource\Doctrine\ORM\Metadata\Resource\Factory; | ||||||||
|
||||||||
use Doctrine\ORM\EntityManagerInterface; | ||||||||
use Doctrine\Persistence\ManagerRegistry; | ||||||||
use Sylius\Resource\Doctrine\Common\State\PersistProcessor; | ||||||||
use Sylius\Resource\Doctrine\Common\State\RemoveProcessor; | ||||||||
use Sylius\Resource\Metadata\DeleteOperationInterface; | ||||||||
use Sylius\Resource\Metadata\GridAwareOperationInterface; | ||||||||
use Sylius\Resource\Metadata\Operation; | ||||||||
use Sylius\Resource\Metadata\Operations; | ||||||||
use Sylius\Resource\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface; | ||||||||
use Sylius\Resource\Metadata\Resource\ResourceMetadataCollection; | ||||||||
use Sylius\Resource\Metadata\ResourceMetadata; | ||||||||
|
||||||||
final class DoctrineORMResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface | ||||||||
{ | ||||||||
public function __construct( | ||||||||
private ManagerRegistry $managerRegistry, | ||||||||
private ResourceMetadataCollectionFactoryInterface $decorated, | ||||||||
) { | ||||||||
} | ||||||||
|
||||||||
public function create(string $resourceClass): ResourceMetadataCollection | ||||||||
{ | ||||||||
$resourceCollectionMetadata = $this->decorated->create($resourceClass); | ||||||||
|
||||||||
/** @var ResourceMetadata $resource */ | ||||||||
foreach ($resourceCollectionMetadata->getIterator() as $i => $resource) { | ||||||||
Comment on lines
+40
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Setting phpstan params ( |
||||||||
$operations = $resource->getOperations() ?? new Operations(); | ||||||||
$entityClass = $resource->getClass(); | ||||||||
|
||||||||
if (null === $entityClass) { | ||||||||
continue; | ||||||||
} | ||||||||
|
||||||||
/** @var Operation $operation */ | ||||||||
foreach ($operations as $operation) { | ||||||||
/** @var string $key */ | ||||||||
$key = $operation->getName(); | ||||||||
$entityManager = $this->managerRegistry->getManagerForClass($entityClass); | ||||||||
|
||||||||
if (!$entityManager instanceof EntityManagerInterface) { | ||||||||
$operations->add($key, $operation); | ||||||||
|
||||||||
continue; | ||||||||
} | ||||||||
|
||||||||
$operations->add($key, $this->addDefaults($operation)); | ||||||||
} | ||||||||
|
||||||||
$resource = $resource->withOperations($operations); | ||||||||
$resourceCollectionMetadata[$i] = $resource; | ||||||||
} | ||||||||
|
||||||||
return $resourceCollectionMetadata; | ||||||||
} | ||||||||
|
||||||||
private function addDefaults(Operation $operation): Operation | ||||||||
{ | ||||||||
$operation = $operation->withProvider($this->getProvider($operation)); | ||||||||
|
||||||||
return $operation->withProcessor($this->getProcessor($operation)); | ||||||||
} | ||||||||
|
||||||||
private function getProvider(Operation $operation): callable|string|null | ||||||||
{ | ||||||||
if (null !== $provider = $operation->getProvider()) { | ||||||||
return $provider; | ||||||||
} | ||||||||
|
||||||||
if ($operation instanceof GridAwareOperationInterface && null !== $operation->getGrid()) { | ||||||||
return null; | ||||||||
} | ||||||||
|
||||||||
return 'sylius.state_provider.doctrine.orm.state.provider'; | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe put it in a const, or pass through the constructor as a parameter? |
||||||||
} | ||||||||
|
||||||||
private function getProcessor(Operation $operation): callable|string | ||||||||
{ | ||||||||
if (null !== $processor = $operation->getProcessor()) { | ||||||||
return $processor; | ||||||||
} | ||||||||
|
||||||||
if ($operation instanceof DeleteOperationInterface) { | ||||||||
return RemoveProcessor::class; | ||||||||
} | ||||||||
|
||||||||
return PersistProcessor::class; | ||||||||
} | ||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would make it easier to find since it'd be in line with the decorated service