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

ICanBoogie / bind-activerecord / 4325699063

pending completion
4325699063

push

github

Olivier Laviale
Use SchemaBuilder

3 of 3 new or added lines in 1 file covered. (100.0%)

57 of 134 relevant lines covered (42.54%)

0.51 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\Connection;
15
use ICanBoogie\ActiveRecord\ConnectionProvider;
16
use ICanBoogie\ActiveRecord\Model;
17
use ICanBoogie\ActiveRecord\ModelProvider;
18
use ICanBoogie\Application;
19
use ICanBoogie\Binding\SymfonyDependencyInjection\ExtensionWithFactory;
20
use Symfony\Component\DependencyInjection\ContainerBuilder;
21
use Symfony\Component\DependencyInjection\Definition;
22
use Symfony\Component\DependencyInjection\Extension\Extension;
23
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
24
use Symfony\Component\DependencyInjection\Reference;
25

26
use function is_string;
27

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

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

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

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

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

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

74
            assert(is_string($class));
75

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

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

83
            if ($class === Model::class) {
×
84
                $container->setDefinition($alias, $definition);
×
85
            } else {
86
                $container->setDefinition($class, $definition);
×
87
                $container->setAlias($alias, $class);
×
88
            }
89
        }
90
    }
91
}
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