Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions src/Bridge/Laravel/Factories/SpiralFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace WayOfDev\Cycle\Bridge\Laravel\Factories;

use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Foundation\Application;
use Spiral\Core\FactoryInterface;

final class SpiralFactory implements FactoryInterface
{
public function __construct(private readonly Application $app)
{
}

/**
* @throws BindingResolutionException
*/
public function make(string $alias, array $parameters = []): mixed
{
return $this->app->make($alias, $parameters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __invoke(Application $app): void
$config = $app->get(MigrationConfig::class);

return new FileRepository(
config: $config
config: $config,
);
});

Expand Down
2 changes: 2 additions & 0 deletions src/Bridge/Laravel/Providers/Registrators/RegisterORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Cycle\ORM\ORMInterface;
use Cycle\ORM\SchemaInterface;
use Illuminate\Contracts\Foundation\Application;
use WayOfDev\Cycle\Bridge\Laravel\Factories\SpiralFactory;
use WayOfDev\Cycle\Schema\Config\SchemaConfig;

/**
Expand All @@ -32,6 +33,7 @@ public function __invoke(Application $app): void
return new Factory(
dbal: $app->get(DatabaseProviderInterface::class),
config: $app->get(RelationConfig::class),
factory: $app->get(SpiralFactory::class),
defaultCollectionFactory: $factory
);
});
Expand Down
44 changes: 44 additions & 0 deletions src/Repository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace WayOfDev\Cycle;

use Cycle\ORM\EntityManagerInterface;
use Cycle\ORM\Select;
use Cycle\ORM\Select\Repository as CycleRepository;
use Throwable;

/**
* Repository provides ability to load entities and construct queries.
*
* @template TEntity of object
*/
class Repository extends CycleRepository
{
/**
* Create repository linked to one specific selector.
*
* @param Select<TEntity> $select
*/
public function __construct(
// @phpstan-ignore-next-line
protected Select $select,
protected EntityManagerInterface $entityManager
) {
parent::__construct($select);
}

/**
* @throws Throwable
*/
public function persist(object $entity, bool $cascade = true): void
{
$this->entityManager->persist(
$entity,
$cascade
);

$this->entityManager->run();
}
}
28 changes: 4 additions & 24 deletions tests/app/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,13 @@

namespace WayOfDev\App\Repositories;

use Cycle\ORM\EntityManager;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Select;
use Cycle\ORM\Select\Repository;
use Throwable;
use WayOfDev\App\Entities\User;
use WayOfDev\Cycle\Repository;

class UserRepository extends Repository
class UserRepository extends Repository implements UserRepositoryInterface
{
private EntityManager $entityManager;

public function __construct(Select $select, ORMInterface $orm)
public function findByUsername(string $username): ?User
{
parent::__construct($select);
$this->entityManager = new EntityManager($orm);
}

/**
* @throws Throwable
*/
public function persist(User $user, bool $cascade = true): void
{
$this->entityManager->persist(
$user,
$cascade
);

$this->entityManager->run();
return $this->findOne(['username' => $username]);
}
}
11 changes: 11 additions & 0 deletions tests/app/Repositories/UserRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace WayOfDev\App\Repositories;

use Cycle\ORM\RepositoryInterface;

interface UserRepositoryInterface extends RepositoryInterface
{
}