Doctrine a OAuth2 | apitte-skeleton a OAuth2

NouF
Backer | 65
+
0
-

Ahoj,

můžu vás poprosit o radu? Jak vyřešit tuto chybu?

Service of type App\Model\Database\Repository\ClientRepository: Service of type Doctrine\ORM\Mapping\ClassMetadata needed by $class in Doctrine\ORM\EntityRepository::__construct() not found. Did you add it to configuration file?

Snažím se použít tento **skeleton **- https://github.com/…tte-skeleton
A **OAuth2 **https://github.com/…auth2-server

OAuth2 vyžaduje zaregistrování repo. jako služby, ale jestli se nepletu, tak to zase Doctrine nechce.

extensions:
oauth2.server: Contributte\OAuth2Server\DI\OAuth2ServerExtension

services:
## OAuth2 ================
- App\Model\Database\Repository\ClientRepository
- App\Model\Database\Repository\AccessTokenRepository
- App\Model\Database\Repository\ScopeRepository
- App\Model\Database\Repository\AuthCodeRepository
- App\Model\Database\Repository\RefreshTokenRepository
- App\Model\Database\Repository\UserRepository

ClientRepository:

/**
 * @extends AbstractRepository<Client>
 */
class ClientRepository extends AbstractRepository implements ClientRepositoryInterface
{

EntityManager:

/**
 * @mixin EntityManager
 */
trait TRepositories
{

    public function getLoginRepository(): LoginRepository
    {
        return $this->getRepository(Login::class);
    }

    public function getAccessTokenRepository(): AccessTokenRepository
    {
        return $this->getRepository(AccessToken::class);
    }

    public function getAuthCodeRepository(): AuthCodeRepository
    {
        return $this->getRepository(AuthCode::class);
    }

    public function getClientRepository(): ClientRepository
    {
        return $this->getRepository(Client::class);
    }

    public function getRefreshTokenRepository(): RefreshTokenRepository
    {
        return $this->getRepository(RefreshToken::class);
    }

    public function getScopeRepository(): ScopeRepository
    {
        return $this->getRepository(Scope::class);
    }

    public function getUserRepository(): UserRepository
    {
        return $this->getRepository(OAuthUser::class);
    }
}

Moc děkuju,

David Matějka
Moderator | 6445
+
0
-

AbstractRepository je repository, které dědí od doctrine entity repository? Nedělej to tak, udělej raději, ať ty oauth repositories od ničeho nedědí.

NouF
Backer | 65
+
0
-

David Matějka napsal(a):

AbstractRepository je repository, které dědí od doctrine entity repository? Nedělej to tak, udělej raději, ať ty oauth repositories od ničeho nedědí.

moc děkuju za rychlou reakci. Dobře, zkusím to přepsat.

/**
 * @template TEntityClass
 * @extends EntityRepository<TEntityClass>
 */
abstract class AbstractRepository extends EntityRepository
{
NouF
Backer | 65
+
0
-

David Matějka napsal(a):

AbstractRepository je repository, které dědí od doctrine entity repository? Nedělej to tak, udělej raději, ať ty oauth repositories od ničeho nedědí.

Pochopil jsem tvou myšlenku dobře?

/**
 * @method AccessToken|NULL find($id, ?int $lockMode = NULL, ?int $lockVersion = NULL)
 * @method AccessToken|NULL findOneBy(array $criteria, array $orderBy = NULL)
 * @method AccessToken[] findAll()
 * @method AccessToken[] findBy(array $criteria, array $orderBy = NULL, ?int $limit = NULL, ?int $offset = NULL)
 */
class AccessTokenRepository implements AccessTokenRepositoryInterface
{
    private EntityManager $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }
....


		/** @var AccessToken $token */
        $token = $this->em->getAccessTokenRepository()->find($tokenId);

Děkuju,
M

NouF
Backer | 65
+
0
-

Když použiju to co jsem postnul, tak mám tuto chybu:

App\Model\Database\Repository\ClientRepository::__construct(): Argument #1 ($em) must be of type
App\Model\Database\EntityManager, Doctrine\ORM\EntityManager given, called in
/var/www/html/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php on line 68

Když upravím USE z App\Model\Database\EntityManager na Doctrine\ORM\EntityManager

Nette\DI\ServiceCreationException: Service of type App\Model\Database\Repository\ClientRepository: Service of
type Doctrine\ORM\EntityManager needed by $em in __construct() not found. Did you add it to configuration file?

Jak se to má zapsat dobře? Jsem zvyklej používat Managera, Repository atd.. Ale tady mám v repository odkazovat na metody té stejné repository (které jsou v anotacích find, findOneBy) a to mi hlava nebere..

<?php

declare(strict_types=1);

namespace App\Model\Database\Repository;

use App\Model\Database\Entity\Client;
use App\Model\Database\Entity\OAuthUser;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\UnitOfWork;
use League\OAuth2\Server\Entities\ClientEntityInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;

/**
 * @method Client|NULL find($id, ?int $lockMode = NULL, ?int $lockVersion = NULL)
 * @method Client|NULL findOneBy(array $criteria, array $orderBy = NULL)
 * @method Client[] findAll()
 * @method Client[] findBy(array $criteria, array $orderBy = NULL, ?int $limit = NULL, ?int $offset = NULL)
 */
class ClientRepository implements ClientRepositoryInterface
{
    private EntityManager $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    /**
     * @param string $clientIdentifier
     * @param null|string $grantType
     * @param null|string $clientSecret
     * @param bool $mustValidateSecret
     *
     * @return ClientEntityInterface|null
     */
    public function getClientEntity(
        $clientIdentifier,
        $grantType = null,
        $clientSecret = null,
        $mustValidateSecret = true
    ) {
        /** @var Client $client */
        $client = $this->em->getRepository(Client::class)->findOneBy([
            'identifier' => $clientIdentifier
        ]);
David Matějka
Moderator | 6445
+
0
-

jestli se nepletu, tak se v nettrine používá nějaký EntityManagerDecorator a nikoliv přímo EntityManager.

A ty anotace nad tou třídou tam máš proč?

NouF
Backer | 65
+
0
-

David Matějka napsal(a):

jestli se nepletu, tak se v nettrine používá nějaký EntityManagerDecorator a nikoliv přímo EntityManager.

A ty anotace nad tou třídou tam máš proč?

V Entity manageru máš pravdu. Ohledně anotací, vycházel jsem u „ukázkového“ repozitáře. Ukazka

Felix
Nette Core | 1183
+
0
-

EntityManagerDecorator je primo od Doctrine. V budoucich verzich bude EntityManager final a nepujde ho podedit a pridat vlastni funkcnost, viz https://github.com/…yManager.php#L80.

Proto existuje EntityManagerDecorator (https://github.com/…ecorator.php#L30).