From c44f093761dc50686fbf6dbb15d4c7bf0880c08a Mon Sep 17 00:00:00 2001 From: roxblnfk Date: Wed, 23 Oct 2019 19:14:00 +0300 Subject: [PATCH] Added Cycle ORM support via `yii-cycle` package and configured; `UserRepository` extends `Cycle\ORM\Select\Repository` --- composer.json | 3 +-- config/common.php | 11 ++--------- config/params.php | 30 ++++++++++++++++++++++++++++++ src/Controller.php | 2 +- src/Entity/User.php | 2 +- src/Repository/UserRepository.php | 15 +++++++-------- 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 300ff37c1..4997507e1 100644 --- a/composer.json +++ b/composer.json @@ -42,8 +42,7 @@ "yiisoft/mailer-swiftmailer": "^3.0@dev", "yiisoft/html": "^3.0@dev", "yiisoft/security": "^3.0@dev", - "cycle/orm": "^1.1", - "cycle/annotated": "^2.0" + "yiisoft/yii-cycle": "^3.0@dev" }, "require-dev": { "hiqdev/composer-config-plugin": "^1.0@dev" diff --git a/config/common.php b/config/common.php index bfa44be2f..05000726f 100644 --- a/config/common.php +++ b/config/common.php @@ -23,12 +23,8 @@ '@src' => '@root/src', '@runtime' => '@root/runtime', ], - CacheInterface::class => [ - '__class' => Cache::class, - 'handler' => [ - '__class' => ArrayCache::class, - ], - ], + Psr\SimpleCache\CacheInterface::class => ArrayCache::class, + CacheInterface::class => Cache::class, LoggerInterface::class => new LoggerFactory(), FileRotatorInterface::class => [ '__class' => FileRotator::class, @@ -48,7 +44,4 @@ 'setPassword()' => [$params['mailer.password']], ], MailerInterface::class => new MailerFactory(), - - // Cycle ORM - ORMInterface::class => new CycleOrmFactory(), ]; diff --git a/config/params.php b/config/params.php index 5cac1ad2a..8756a1a69 100644 --- a/config/params.php +++ b/config/params.php @@ -14,4 +14,34 @@ 'commands' => [ 'user/create' => CreateUser::class, ], + + // cycle DBAL config + 'cycle.dbal' => [ + 'default' => 'default', + 'aliases' => [], + 'databases' => [ + 'default' => ['connection' => 'sqlite'] + ], + 'connections' => [ + 'sqlite' => [ + 'driver' => \Spiral\Database\Driver\SQLite\SQLiteDriver::class, + 'connection' => 'sqlite:@runtime/database.db', + 'username' => '', + 'password' => '', + ] + ], + ], + // cycle common config + 'cycle.common' => [ + 'entityPaths' => [ + '@src/Entity' + ], + ], + // cycle migration config + 'cycle.migrations' => [ + 'directory' => '@root/migrations', + 'namespace' => 'App\\Migration', + 'table' => 'migration', + 'safe' => false, + ], ]; diff --git a/src/Controller.php b/src/Controller.php index 35cf6db3b..ccb77a3d8 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -64,7 +64,7 @@ private function findLayoutFile(?string $file): ?string return $file; } - return $file . '.' . $this->view->defaultExtension; + return $file . '.' . $this->view->getDefaultExtension(); } abstract protected function getId(): string; diff --git a/src/Entity/User.php b/src/Entity/User.php index ee204c82d..bf3587678 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -8,7 +8,7 @@ use Yiisoft\Yii\Web\User\IdentityInterface; /** - * @Entity + * @Entity(repository="App\Repository\UserRepository") */ class User implements IdentityInterface { diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index 9de33ddd2..0fa55a640 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -4,26 +4,25 @@ use App\Entity\User; use Cycle\ORM\ORMInterface; +use Cycle\ORM\Select; use Yiisoft\Yii\Web\User\IdentityInterface; use Yiisoft\Yii\Web\User\IdentityRepositoryInterface; -class UserRepository implements IdentityRepositoryInterface +class UserRepository extends Select\Repository implements IdentityRepositoryInterface { - private $orm; - - public function __construct(ORMInterface $orm) + public function __construct(ORMInterface $orm, $role = User::class) { - $this->orm = $orm; + parent::__construct(new Select($orm, $role)); } private function findIdentityBy(string $field, string $value): ?IdentityInterface { - return $this->orm->getRepository(User::class)->findOne([$field => $value]); + return $this->findOne([$field => $value]); } public function findIdentity(string $id): ?IdentityInterface { - return $this->findIdentityBy('id', $id); + return $this->findByPK($id); } public function findIdentityByToken(string $token, string $type): ?IdentityInterface @@ -31,7 +30,7 @@ public function findIdentityByToken(string $token, string $type): ?IdentityInter return $this->findIdentityBy('token', $token); } - public function findByLogin(string $login): ?User + public function findByLogin(string $login): ?IdentityInterface { return $this->findIdentityBy('login', $login); }