From 78d2de76a6c25bf7fa2461ae11cad5d43a4f7b1e Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Sun, 10 Sep 2023 16:18:31 +0200 Subject: [PATCH] ICanBoogie/ActiveRecord update --- README.md | 4 +--- composer.json | 3 +-- config/services.yml | 4 ---- lib/Console/ListModelsCommand.php | 7 ++++--- lib/ContainerExtension.php | 14 +++++++------- lib/Hooks.php | 17 ++++------------- lib/Record.php | 20 ++++++++------------ tests/app/all/config/services.yml | 4 ++-- tests/lib/Console/ListModelsCommandTest.php | 2 ++ tests/lib/ContainerTest.php | 5 ++--- tests/lib/IntegrationTest.php | 3 ++- 11 files changed, 33 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index e0f2b89..548c17e 100644 --- a/README.md +++ b/README.md @@ -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); ``` diff --git a/composer.json b/composer.json index d6cf892..7a58fa6 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/config/services.yml b/config/services.yml index e8248fe..b301e80 100644 --- a/config/services.yml +++ b/config/services.yml @@ -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: diff --git a/lib/Console/ListModelsCommand.php b/lib/Console/ListModelsCommand.php index ebcb3a2..995e6ff 100644 --- a/lib/Console/ListModelsCommand.php +++ b/lib/Console/ListModelsCommand.php @@ -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(); diff --git a/lib/ContainerExtension.php b/lib/ContainerExtension.php index 14ab825..f2e6413 100644 --- a/lib/ContainerExtension.php +++ b/lib/ContainerExtension.php @@ -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; @@ -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); } } } diff --git a/lib/Hooks.php b/lib/Hooks.php index 1980a7f..7c3692d 100644 --- a/lib/Hooks.php +++ b/lib/Hooks.php @@ -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; @@ -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); } ); } diff --git a/lib/Record.php b/lib/Record.php index df8b258..01f988f 100644 --- a/lib/Record.php +++ b/lib/Record.php @@ -6,12 +6,16 @@ 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_class */ @@ -19,15 +23,7 @@ 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); - } } diff --git a/tests/app/all/config/services.yml b/tests/app/all/config/services.yml index f743978..6e119b0 100644 --- a/tests/app/all/config/services.yml +++ b/tests/app/all/config/services.yml @@ -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 diff --git a/tests/lib/Console/ListModelsCommandTest.php b/tests/lib/Console/ListModelsCommandTest.php index c08ff34..14a6ae3 100644 --- a/tests/lib/Console/ListModelsCommandTest.php +++ b/tests/lib/Console/ListModelsCommandTest.php @@ -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 @@ -17,6 +18,7 @@ public static function provideExecute(): array ListModelsCommand::class, [], [ + Node::class, NodeModel::class, 'primary' ] diff --git a/tests/lib/ContainerTest.php b/tests/lib/ContainerTest.php index 360cc85..39b454e 100644 --- a/tests/lib/ContainerTest.php +++ b/tests/lib/ContainerTest.php @@ -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; @@ -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 ], diff --git a/tests/lib/IntegrationTest.php b/tests/lib/IntegrationTest.php index e219f8e..86db155 100644 --- a/tests/lib/IntegrationTest.php +++ b/tests/lib/IntegrationTest.php @@ -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; use Test\ICanBoogie\Binding\ActiveRecord\Acme\SampleService; @@ -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);