Skip to content
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

[PoC] Custom ActiveQuery collections based on an attribute #20

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
145 changes: 73 additions & 72 deletions composer.lock

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions src/Attribute/Collection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Cycle\ActiveRecord\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
readonly class Collection
{
public function __construct(public string $name)
{
}
}
34 changes: 32 additions & 2 deletions src/Query/ActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace Cycle\ActiveRecord\Query;

use Cycle\ActiveRecord\Attribute\Collection;
use Cycle\ActiveRecord\Facade;
use Cycle\ORM\ORMInterface;
use Cycle\ORM\Select;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionException;

/**
* @template-covariant TEntity of object
Expand All @@ -14,11 +19,36 @@
*/
class ActiveQuery extends Select
{
protected ORMInterface $orm;

/**
* @param class-string<TEntity> $class
*/
final public function __construct(string $class)
final public function __construct(protected string $class)
{
parent::__construct(Facade::getOrm(), $class);
$this->orm = Facade::getOrm();

parent::__construct($this->orm, $class);
}

/**
* @throws ReflectionException
*/
public function fetchAll(): iterable
{
$reflection = new ReflectionClass(static::class);
// $reflection = new ReflectionClass($this->class);
$attributes = $reflection->getAttributes(Collection::class, ReflectionAttribute::IS_INSTANCEOF);

dd($attributes);

if ([] === $attributes) {

Check failure on line 45 in src/Query/ActiveQuery.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

Unreachable statement - code above always terminates.
return parent::fetchAll();
}

$attribute = $attributes[0]->newInstance();
$collection = $this->orm->getFactory()->collection($attribute->name);

return $collection->collect($this->getIterator());
}
}
6 changes: 6 additions & 0 deletions tests/app/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Cycle\Annotated\Annotation\Column;
use Cycle\Annotated\Annotation\Entity;
use Cycle\Annotated\Annotation\Relation\BelongsTo;
use Cycle\App\Query\UserCollectionQuery;
use Cycle\App\Query\UserQuery;

#[Entity(table: 'user')]
Expand All @@ -30,6 +31,11 @@ public static function query(): UserQuery
return new UserQuery(static::class);
}

public static function queryWithCollection(): UserCollectionQuery
{
return new UserCollectionQuery(static::class);
}

public function __construct(string $name, ?Identity $identity = null)
{
$this->name = $name;
Expand Down
23 changes: 23 additions & 0 deletions tests/app/Query/UserCollectionQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Cycle\App\Query;

use Cycle\ActiveRecord\Attribute\Collection;
use Cycle\ActiveRecord\Query\ActiveQuery;
use Cycle\ORM\Collection\IlluminateCollectionFactory;

/**
* @template-covariant TEntity of object
*
* @extends ActiveQuery<TEntity>
*/
#[Collection(name: IlluminateCollectionFactory::class)]
class UserCollectionQuery extends ActiveQuery
{
public function active(bool $state = true): UserCollectionQuery
{
return $this->where(['active' => $state]);
}
}
5 changes: 4 additions & 1 deletion tests/src/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Cycle\ActiveRecord\Facade;
use Cycle\App\Entity\User;
use Cycle\Database\Database;
use Cycle\Database\DatabaseManager;
use Cycle\ORM\Select\Repository;
use PHPUnit\Framework\Attributes\Test;
Expand All @@ -21,8 +22,10 @@ public function tearDown(): void
parent::tearDown();

$databaseManager = $this->getContainer()->get(DatabaseManager::class);
/** @var Database $database */
$database = $databaseManager->database('default');

$this->dropDatabase($databaseManager->database('default'));
$this->dropDatabase($database);
Facade::reset();
}

Expand Down
47 changes: 47 additions & 0 deletions tests/src/Query/ActiveQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Cycle\Tests\Query;

use Cycle\ActiveRecord\Facade;
use Cycle\App\Entity\User;
use Cycle\Database\Database;
use Cycle\Database\DatabaseManager
use Cycle\Tests\DatabaseTestCase;

Check failure on line 11 in tests/src/Query/ActiveQueryTest.php

View workflow job for this annotation

GitHub Actions / static-analysis (ubuntu-latest, 8.2, locked)

Syntax error, unexpected T_USE, expecting ';' on line 11
use PHPUnit\Framework\Attributes\Test;
use ReflectionException;
use Throwable;

final class ActiveQueryTest extends DatabaseTestCase
{
/**
* @throws Throwable
*/
public function tearDown(): void
{
parent::tearDown();

$databaseManager = $this->getContainer()->get(DatabaseManager::class);
/** @var Database $database */
$database = $databaseManager->database('default');

$this->dropDatabase($database);
Facade::reset();
}

/**
* @test
*
* @throws ReflectionException
*/
#[Test]
public function it_checks_if_fetch_all_method_returns_array_by_default(): void
{
$users = User::queryWithCollection()->fetchAll();

dd($users);

$this::assertIsArray($users);
}
}
Loading