Skip to content

Commit

Permalink
Add Record attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 10, 2023
1 parent a5679d2 commit e53ee9d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
7 changes: 4 additions & 3 deletions composer.json
Expand Up @@ -27,10 +27,11 @@
"prefer-dist": true,
"require": {
"php": ">=8.1",
"icanboogie/icanboogie": "^6.0",
"icanboogie/activerecord": "dev-remove-model-id as 6.0",
"olvlvl/composer-attribute-collector": "^2.0"
},
"icanboogie/icanboogie": "^6.0",
"olvlvl/composer-attribute-collector": "^2.0",
"symfony/expression-language": "^6.3"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.0"
Expand Down
33 changes: 33 additions & 0 deletions lib/Record.php
@@ -0,0 +1,33 @@
<?php

namespace ICanBoogie\Binding\ActiveRecord;

use Attribute;
use ICanBoogie\ActiveRecord;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

use function sprintf;
use function str_replace;

#[Attribute(Attribute::TARGET_PARAMETER)]
class Record extends Autowire
{
/**
* @param class-string<ActiveRecord> $activerecord_class
*/
public function __construct( // @phpstan-ignore-line
public readonly string $activerecord_class,
) {
parent::__construct(
expression: sprintf(
"service('%s').model_for_activerecord('%s')",
self::escape(ActiveRecord\ModelProvider::class),
self::escape($this->activerecord_class)
)
);
}

private static function escape(string $str): string {

Check failure on line 30 in lib/Record.php

View workflow job for this annotation

GitHub Actions / phpcs

Opening brace should be on a new line
return str_replace('\\', '\\\\', $str);
}
}
3 changes: 3 additions & 0 deletions tests/app/all/config/services.yml
Expand Up @@ -13,3 +13,6 @@ services:

test.active_record.model.node_by_class:
alias: Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel

Test\ICanBoogie\Binding\ActiveRecord\Acme\SampleService:
autowire: true
17 changes: 17 additions & 0 deletions tests/lib/Acme/SampleService.php
@@ -0,0 +1,17 @@
<?php

namespace Test\ICanBoogie\Binding\ActiveRecord\Acme;

use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\Binding\ActiveRecord\Record;

final class SampleService
{
/**
* @param Model<int, Article> $model
*/
public function __construct(
#[Record(Article::class)] public readonly Model $model,
) {
}
}
11 changes: 11 additions & 0 deletions tests/lib/IntegrationTest.php
Expand Up @@ -4,8 +4,11 @@

use ICanBoogie\ActiveRecord\ModelProvider;
use PHPUnit\Framework\TestCase;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Article;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel;

Check failure on line 8 in tests/lib/IntegrationTest.php

View workflow job for this annotation

GitHub Actions / phpcs

Header blocks must not contain blank lines

use Test\ICanBoogie\Binding\ActiveRecord\Acme\SampleService;

use function ICanBoogie\app;

final class IntegrationTest extends TestCase
Expand All @@ -19,4 +22,12 @@ public function test_nodes_model(): void
$this->assertEquals('id', $nodes->schema->primary);
$this->assertEquals('id', $nodes->primary);
}

public function test_qualified_model(): void
{
$service = app()->service_for_class(SampleService::class);
$actual = $service->model->activerecord_class;

$this->assertEquals(Article::class, $actual);
}
}

0 comments on commit e53ee9d

Please sign in to comment.