Skip to content

Commit

Permalink
ActiveRecord update
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 10, 2023
1 parent 3ec6733 commit 95a0b51
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 69 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ $primary_connection = $app->service_for_id('active_record.connection.primary', C
# or
$primary_connection = $app->service_for_class(ConnectionProvider::class)->connection_for_id('primary');

$nodes = $app->service_for_class(NodeModel::class);
# or
$nodes = $app->service_for_class(ModelProvider::class)->model_for_class(NodeModel::class);
$nodes = $app->service_for_class(ModelProvider::class)->model_for_record(Node::class);
```


Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
"prefer-dist": true,
"require": {
"php": ">=8.1",
"icanboogie/activerecord": "dev-remove-model-id as 6.0",
"icanboogie/activerecord": "^6.0",
"icanboogie/icanboogie": "^6.0",
"olvlvl/composer-attribute-collector": "^2.0",
"symfony/expression-language": "^6.3"
"olvlvl/composer-attribute-collector": "^2.0"
},
"require-dev": {
"phpstan/phpstan": "^1.10",
Expand Down
4 changes: 0 additions & 4 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ services:
ICanBoogie\ActiveRecord\ModelIterator:
alias: ICanBoogie\ActiveRecord\ModelCollection

ICanBoogie\ActiveRecord\ModelResolver:
alias: ICanBoogie\ActiveRecord\ModelCollection
public: true # required by StaticModelResolver

# Console

ICanBoogie\Binding\ActiveRecord\Console\ListModelsCommand:
Expand Down
9 changes: 8 additions & 1 deletion lib/ConfigBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ICanBoogie\ActiveRecord\Config;
use ICanBoogie\ActiveRecord\Config\ConnectionDefinition;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\Query;
use ICanBoogie\ActiveRecord\SchemaBuilder;
use ICanBoogie\Config\Builder;

Expand Down Expand Up @@ -67,19 +68,25 @@ public function add_connection(
}

/**
* @param class-string<ActiveRecord> $activerecord_class
* @param class-string<Model> $model_class
* @param class-string<Query> $query_class
* @param (Closure(SchemaBuilder $schema): SchemaBuilder)|null $schema_builder
*/
public function add_model(

Check failure on line 76 in lib/ConfigBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Method ICanBoogie\Binding\ActiveRecord\ConfigBuilder::add_model() has parameter $activerecord_class with generic class ICanBoogie\ActiveRecord but does not specify its types: TKey

Check failure on line 76 in lib/ConfigBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Method ICanBoogie\Binding\ActiveRecord\ConfigBuilder::add_model() has parameter $model_class with generic class ICanBoogie\ActiveRecord\Model but does not specify its types: TKey, TValue

Check failure on line 76 in lib/ConfigBuilder.php

View workflow job for this annotation

GitHub Actions / phpstan

Method ICanBoogie\Binding\ActiveRecord\ConfigBuilder::add_model() has parameter $query_class with generic class ICanBoogie\ActiveRecord\Query but does not specify its types: TRecord
string $model_class,
string $activerecord_class,
string $model_class = Model::class,
string $query_class = Query::class,
string|null $table_name = null,
string|null $alias = null,
Closure $schema_builder = null,
Closure $association_builder = null,
string $connection = Config::DEFAULT_CONNECTION_ID,
): self {
$this->inner->add_model(
activerecord_class: $activerecord_class,
model_class: $model_class,
query_class: $query_class,
table_name: $table_name,
alias: $alias,
schema_builder: $schema_builder,
Expand Down
7 changes: 4 additions & 3 deletions lib/Console/ListModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
{
$rows = [];

foreach ($this->config->models as $model_id => $attributes) {
foreach ($this->config->models as $attributes) {
$rows[] = [
$model_id,
$attributes->activerecord_class,
$attributes->model_class,
$attributes->connection,
];
}

$table = new Table($output);
$table->setHeaders([ 'Id', 'Connection' ]);
$table->setHeaders([ 'ActiveRecord', 'Model', 'Connection' ]);
$table->setRows($rows);
$table->setStyle($this->style);
$table->render();
Expand Down
14 changes: 7 additions & 7 deletions lib/ContainerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use ICanBoogie\ActiveRecord\ModelProvider;
use ICanBoogie\Application;
use ICanBoogie\Binding\SymfonyDependencyInjection\ExtensionWithFactory;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\Extension;
Expand Down Expand Up @@ -67,14 +66,15 @@ private function register_connections(ContainerBuilder $container): void
*/
private function register_models(ContainerBuilder $container): void
{
foreach ($this->config->models as $model) {
$class = $model->model_class;
$definition = (new Definition($class))
->setFactory([ new Reference(ModelProvider::class), 'model_for_class' ])
->setArguments([ $class ])
foreach ($this->config->models as $definition) {
$activerecord_class = $definition->activerecord_class;

$d = (new Definition($definition->model_class))
->setFactory([ new Reference(ModelProvider::class), 'model_for_record' ])
->setArguments([ $activerecord_class ])
->setPublic(true);

$container->setDefinition($class, $definition);
$container->setDefinition(Record::format_service_id($activerecord_class), $d);
}
}
}
17 changes: 4 additions & 13 deletions lib/Hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
use ICanBoogie\ActiveRecord\ActiveRecordCache\RuntimeActiveRecordCache;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\ModelProvider;
use ICanBoogie\ActiveRecord\ModelResolver;
use ICanBoogie\ActiveRecord\StaticModelResolver;
use ICanBoogie\ActiveRecord\StaticModelProvider;
use ICanBoogie\Application;
use ICanBoogie\Validate\ValidationErrors;

Expand All @@ -35,19 +34,11 @@ public static function on_app_boot(Application\BootEvent $event): void
{
$app = $event->app;

ActiveRecord\StaticModelProvider::define(function (string $id) use ($app): Model {
static $models;

$models ??= $app->service_for_class(ModelProvider::class);

return $models->model_for_id($id);
});

StaticModelResolver::define(
static function () use ($app): ModelResolver {
StaticModelProvider::define(
static function () use ($app): ModelProvider {
static $resolver;

return $resolver ??= $app->service_for_class(ModelResolver::class);
return $resolver ??= $app->service_for_class(ModelProvider::class);
}
);
}
Expand Down
20 changes: 8 additions & 12 deletions lib/Record.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,24 @@
use ICanBoogie\ActiveRecord;
use Symfony\Component\DependencyInjection\Attribute\Autowire;

use function sprintf;
use function str_replace;

#[Attribute(Attribute::TARGET_PARAMETER)]
class Record extends Autowire
{
public const SERVICE_PREFIX = 'active_record.model.';

public static function format_service_id(string $activerecord_class): string
{
return self::SERVICE_PREFIX . $activerecord_class;
}

/**
* @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)
)
service: self::format_service_id($activerecord_class)
);
}

private static function escape(string $str): string {
return str_replace('\\', '\\\\', $str);
}
}
2 changes: 2 additions & 0 deletions tests/app/all/config/activerecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

use ICanBoogie\ActiveRecord\Config;
use ICanBoogie\ActiveRecord\SchemaBuilder;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Node;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel;

return fn(ConfigBuilder $config) => $config
->add_connection(Config::DEFAULT_CONNECTION_ID, 'sqlite::memory:')
->add_model(
activerecord_class: Node::class,
model_class: NodeModel::class,
schema_builder: fn(SchemaBuilder $schema) => $schema
->add_serial('id', primary: true)
Expand Down
4 changes: 2 additions & 2 deletions tests/app/all/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ services:
test.active_record.models:
alias: ICanBoogie\ActiveRecord\ModelProvider

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

Test\ICanBoogie\Binding\ActiveRecord\Acme\SampleService:
autowire: true
4 changes: 2 additions & 2 deletions tests/app/default/config/activerecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
namespace ICanBoogie\Binding\ActiveRecord;

use ICanBoogie\ActiveRecord\SchemaBuilder;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\ArticleModel;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Article;

return fn(ConfigBuilder $config) => $config
->add_connection('cache', 'sqlite::memory:')
->add_model(
model_class: ArticleModel::class,
activerecord_class: Article::class,
schema_builder: fn(SchemaBuilder $schema) => $schema
->add_text('body')
->add_datetime('date'),
Expand Down
10 changes: 0 additions & 10 deletions tests/lib/Acme/ArticleModel.php

This file was deleted.

1 change: 0 additions & 1 deletion tests/lib/Acme/NodeModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
/**
* @extends Model<int, Node>
*/
#[Model\Record(Node::class)]
class NodeModel extends Model
{
}
3 changes: 1 addition & 2 deletions tests/lib/ActiveRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use ICanBoogie\Validate\ValidationErrors;
use PHPUnit\Framework\TestCase;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Article;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\ArticleModel;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\SampleRecord;

/**
Expand All @@ -33,6 +32,6 @@ public function test_model(): void
{
$record = Article::from();

$this->assertInstanceOf(ArticleModel::class, $record->model);
$this->assertEquals(Article::class, $record->model->activerecord_class);
}
}
6 changes: 4 additions & 2 deletions tests/lib/ConfigBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
use ICanBoogie\ActiveRecord\ConfigBuilder;
use ICanBoogie\ActiveRecord\SchemaBuilder;
use PHPUnit\Framework\TestCase;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\ArticleModel;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Article;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Node;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel;

use function ICanBoogie\app;
Expand Down Expand Up @@ -57,13 +58,14 @@ public function test_integration(): void
->add_connection(Config::DEFAULT_CONNECTION_ID, 'sqlite::memory:')
->add_connection('cache', 'sqlite::memory:')
->add_model(
activerecord_class: Node::class,
model_class: NodeModel::class,
schema_builder: fn(SchemaBuilder $b) => $b
->add_serial('id', primary: true)
->add_character('title'),
)
->add_model(
model_class: ArticleModel::class,
activerecord_class: Article::class,
schema_builder: fn(SchemaBuilder $b) => $b
->add_text('body')
->add_datetime('date'),
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/Console/ListModelsCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ICanBoogie\Binding\ActiveRecord\Console\ListModelsCommand;
use ICanBoogie\Console\Test\CommandTestCase;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Node;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel;

final class ListModelsCommandTest extends CommandTestCase
Expand All @@ -17,6 +18,7 @@ public static function provideExecute(): array
ListModelsCommand::class,
[],
[
Node::class,
NodeModel::class,
'primary'
]
Expand Down
5 changes: 2 additions & 3 deletions tests/lib/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

namespace Test\ICanBoogie\Binding\ActiveRecord;

use ICanBoogie\ActiveRecord\Config;
use ICanBoogie\ActiveRecord\Connection;
use ICanBoogie\ActiveRecord\ConnectionProvider;
use ICanBoogie\ActiveRecord\Model;
use ICanBoogie\ActiveRecord\ModelProvider;
use ICanBoogie\ActiveRecord\Config;
use PHPUnit\Framework\TestCase;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel;

Expand Down Expand Up @@ -43,7 +42,7 @@ public static function provide_service(): array
[ 'test.active_record.config', Config::class ],
[ 'test.active_record.connections', ConnectionProvider::class ],
[ 'test.active_record.models', ModelProvider::class ],
[ 'test.active_record.model.node_by_class', NodeModel::class ],
[ 'test.active_record.model.node', NodeModel::class ],
[ 'active_record.connection.primary', Connection::class ],
[ 'active_record.connection.cache', Connection::class ],

Expand Down
2 changes: 2 additions & 0 deletions tests/lib/HooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ICanBoogie\ActiveRecord\Config;
use ICanBoogie\ActiveRecord\ConnectionProvider;
use ICanBoogie\ActiveRecord\ModelCollection;
use ICanBoogie\ActiveRecord\Query;
use ICanBoogie\ActiveRecord\SchemaBuilder;
use ICanBoogie\ActiveRecord\TableDefinition;
use ICanBoogie\Binding\ActiveRecord\Hooks;
Expand Down Expand Up @@ -51,6 +52,7 @@ public function test_should_return_activerecord_cache(): void
),
model_class: NodeModel::class,
activerecord_class: Node::class,
query_class: Query::class,
connection: Config::DEFAULT_CONNECTION_ID,
)
);
Expand Down
3 changes: 2 additions & 1 deletion tests/lib/IntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use ICanBoogie\ActiveRecord\ModelProvider;
use PHPUnit\Framework\TestCase;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Article;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\Node;
use Test\ICanBoogie\Binding\ActiveRecord\Acme\NodeModel;

Check failure on line 9 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;
Expand All @@ -16,7 +17,7 @@ final class IntegrationTest extends TestCase
public function test_nodes_model(): void
{
$model_provider = app()->service_for_id('test.active_record.models', ModelProvider::class);
$nodes = $model_provider->model_for_class(NodeModel::class);
$nodes = $model_provider->model_for_record(Node::class);

$this->assertInstanceOf(NodeModel::class, $nodes);
$this->assertEquals('id', $nodes->schema->primary);
Expand Down

0 comments on commit 95a0b51

Please sign in to comment.