Skip to content

Commit

Permalink
ICanBoogie/ActiveRecord update
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Sep 10, 2023
1 parent e53ee9d commit 78d2de7
Show file tree
Hide file tree
Showing 11 changed files with 33 additions and 50 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
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
"php": ">=8.1",
"icanboogie/activerecord": "dev-remove-model-id as 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
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);
}
}
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
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
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 78d2de7

Please sign in to comment.