Skip to content

Commit 479eead

Browse files
authored
Merge pull request #178 from wayofdev/feat/factories
2 parents fcf3af0 + 2a2453f commit 479eead

File tree

7 files changed

+91
-30
lines changed

7 files changed

+91
-30
lines changed

composer.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle\Bridge\Laravel\Factories;
6+
7+
use Illuminate\Contracts\Container\BindingResolutionException;
8+
use Illuminate\Contracts\Foundation\Application;
9+
use Spiral\Core\FactoryInterface;
10+
11+
final class SpiralFactory implements FactoryInterface
12+
{
13+
public function __construct(private readonly Application $app)
14+
{
15+
}
16+
17+
/**
18+
* @throws BindingResolutionException
19+
*/
20+
public function make(string $alias, array $parameters = []): mixed
21+
{
22+
return $this->app->make($alias, $parameters);
23+
}
24+
}

src/Bridge/Laravel/Providers/Registrators/RegisterMigrations.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __invoke(Application $app): void
2222
$config = $app->get(MigrationConfig::class);
2323

2424
return new FileRepository(
25-
config: $config
25+
config: $config,
2626
);
2727
});
2828

src/Bridge/Laravel/Providers/Registrators/RegisterORM.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Cycle\ORM\ORMInterface;
1515
use Cycle\ORM\SchemaInterface;
1616
use Illuminate\Contracts\Foundation\Application;
17+
use WayOfDev\Cycle\Bridge\Laravel\Factories\SpiralFactory;
1718
use WayOfDev\Cycle\Schema\Config\SchemaConfig;
1819

1920
/**
@@ -32,6 +33,7 @@ public function __invoke(Application $app): void
3233
return new Factory(
3334
dbal: $app->get(DatabaseProviderInterface::class),
3435
config: $app->get(RelationConfig::class),
36+
factory: $app->get(SpiralFactory::class),
3537
defaultCollectionFactory: $factory
3638
);
3739
});

src/Repository.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\Cycle;
6+
7+
use Cycle\ORM\EntityManagerInterface;
8+
use Cycle\ORM\Select;
9+
use Cycle\ORM\Select\Repository as CycleRepository;
10+
use Throwable;
11+
12+
/**
13+
* Repository provides ability to load entities and construct queries.
14+
*
15+
* @template TEntity of object
16+
*/
17+
class Repository extends CycleRepository
18+
{
19+
/**
20+
* Create repository linked to one specific selector.
21+
*
22+
* @param Select<TEntity> $select
23+
*/
24+
public function __construct(
25+
// @phpstan-ignore-next-line
26+
protected Select $select,
27+
protected EntityManagerInterface $entityManager
28+
) {
29+
parent::__construct($select);
30+
}
31+
32+
/**
33+
* @throws Throwable
34+
*/
35+
public function persist(object $entity, bool $cascade = true): void
36+
{
37+
$this->entityManager->persist(
38+
$entity,
39+
$cascade
40+
);
41+
42+
$this->entityManager->run();
43+
}
44+
}

tests/app/Repositories/UserRepository.php

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,13 @@
44

55
namespace WayOfDev\App\Repositories;
66

7-
use Cycle\ORM\EntityManager;
8-
use Cycle\ORM\ORMInterface;
9-
use Cycle\ORM\Select;
10-
use Cycle\ORM\Select\Repository;
11-
use Throwable;
127
use WayOfDev\App\Entities\User;
8+
use WayOfDev\Cycle\Repository;
139

14-
class UserRepository extends Repository
10+
class UserRepository extends Repository implements UserRepositoryInterface
1511
{
16-
private EntityManager $entityManager;
17-
18-
public function __construct(Select $select, ORMInterface $orm)
12+
public function findByUsername(string $username): ?User
1913
{
20-
parent::__construct($select);
21-
$this->entityManager = new EntityManager($orm);
22-
}
23-
24-
/**
25-
* @throws Throwable
26-
*/
27-
public function persist(User $user, bool $cascade = true): void
28-
{
29-
$this->entityManager->persist(
30-
$user,
31-
$cascade
32-
);
33-
34-
$this->entityManager->run();
14+
return $this->findOne(['username' => $username]);
3515
}
3616
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace WayOfDev\App\Repositories;
6+
7+
use Cycle\ORM\RepositoryInterface;
8+
9+
interface UserRepositoryInterface extends RepositoryInterface
10+
{
11+
}

0 commit comments

Comments
 (0)