• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ICanBoogie / bind-activerecord / 4437492734

pending completion
4437492734

push

github

Olivier Laviale
Use ActiveRecord's SchemaBuilder

28 of 28 new or added lines in 5 files covered. (100.0%)

38 of 119 relevant lines covered (31.93%)

0.39 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/lib/ContainerExtension.php
1
<?php
2

3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <olivier.laviale@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
namespace ICanBoogie\Binding\ActiveRecord;
13

14
use ICanBoogie\ActiveRecord\Config;
15
use ICanBoogie\ActiveRecord\Connection;
16
use ICanBoogie\ActiveRecord\ConnectionProvider;
17
use ICanBoogie\ActiveRecord\Model;
18
use ICanBoogie\ActiveRecord\ModelProvider;
19
use ICanBoogie\Application;
20
use ICanBoogie\Binding\SymfonyDependencyInjection\ExtensionWithFactory;
21
use Symfony\Component\DependencyInjection\ContainerBuilder;
22
use Symfony\Component\DependencyInjection\Definition;
23
use Symfony\Component\DependencyInjection\Extension\Extension;
24
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
25
use Symfony\Component\DependencyInjection\Reference;
26

27
use function is_string;
28

29
final class ContainerExtension extends Extension implements ExtensionWithFactory
30
{
31
    public static function from(Application $app): ExtensionInterface
32
    {
33
        return new self(
×
34
            $app->configs->config_for_class(Config::class)
×
35
        );
×
36
    }
37

38
    private function __construct(
39
        private readonly Config $config
40
    ) {
41
    }
×
42

43
    /**
44
     * @param array<string, mixed> $configs
45
     */
46
    public function load(array $configs, ContainerBuilder $container): void
47
    {
48
        $this->register_connections($container);
×
49
        $this->register_models($container);
×
50
    }
51

52
    /**
53
     * Creates a `active_record.connection.$id` definition for each of the connection configured.
54
     */
55
    private function register_connections(ContainerBuilder $container): void
56
    {
57
        foreach ($this->config->connections as $id => $connection) {
×
58
            $definition = (new Definition(Connection::class))
×
59
                ->setFactory([ new Reference(ConnectionProvider::class), 'connection_for_id' ])
×
60
                ->setArguments([ $id ])
×
61
                ->setPublic(true);
×
62

63
            $container->setDefinition("active_record.connection.$id", $definition);
×
64
        }
65
    }
66

67
    /**
68
     * Creates a `active_record.model.$id` definition for each of the models configured.
69
     */
70
    private function register_models(ContainerBuilder $container): void
71
    {
72
        foreach ($this->config->models as $id => $model) {
×
73
            $class = $model->model_class ?? Model::class;
×
74

75
            assert(is_string($class));
76

77
            $definition = (new Definition($class))
×
78
                ->setFactory([ new Reference(ModelProvider::class), 'model_for_id' ])
×
79
                ->setArguments([ $id ])
×
80
                ->setPublic(true);
×
81

82
            $alias = "active_record.model.$id";
×
83

84
            if ($class === Model::class) {
×
85
                $container->setDefinition($alias, $definition);
×
86
            } else {
87
                $container->setDefinition($class, $definition);
×
88
                $container->setAlias($alias, $class);
×
89
            }
90
        }
91
    }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc