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

daycry / doctrine / 16045998748

03 Jul 2025 08:53AM UTC coverage: 78.852% (-1.3%) from 80.15%
16045998748

push

github

daycry
Feature: Doctrine Collector

- Add doctrine collector to codeigniter toolbar

49 of 66 new or added lines in 3 files covered. (74.24%)

261 of 331 relevant lines covered (78.85%)

15.57 hits per line

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

73.33
/src/Doctrine.php
1
<?php
2

3
namespace Daycry\Doctrine;
4

5
use Config\Cache;
6
use Config\Database;
7
use Daycry\Doctrine\Collectors\DoctrineCollector;
8
use Daycry\Doctrine\Collectors\DoctrineQueryMiddleware;
9
use Daycry\Doctrine\Config\Doctrine as DoctrineConfig;
10
use Daycry\Doctrine\Libraries\Memcached;
11
use Daycry\Doctrine\Libraries\Redis;
12
use Doctrine\DBAL\DriverManager;
13
use Doctrine\DBAL\Tools\DsnParser;
14
use Doctrine\ORM\Configuration;
15
use Doctrine\ORM\EntityManager;
16
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
17
use Doctrine\ORM\Mapping\Driver\XmlDriver;
18
use Exception;
19
use Symfony\Component\Cache\Adapter\ArrayAdapter;
20
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
21
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
22
use Symfony\Component\Cache\Adapter\RedisAdapter;
23

24
/**
25
 * Class General
26
 */
27
class Doctrine
28
{
29
    public ?EntityManager $em = null;
30

31
    public function __construct(?DoctrineConfig $doctrineConfig = null, ?Cache $cacheConfig = null, ?string $dbGroup = null)
32
    {
33
        if ($doctrineConfig === null) {
52✔
34
            /** @var DoctrineConfig $doctrineConfig */
35
            $doctrineConfig = config('Doctrine');
2✔
36
        }
37

38
        if ($cacheConfig === null) {
52✔
39
            /** @var Cache $cacheConfig */
40
            $cacheConfig = config('Cache');
44✔
41
        }
42

43
        $devMode = (ENVIRONMENT === 'development') ? true : false;
52✔
44

45
        switch ($cacheConfig->handler) {
52✔
46
            case 'file':
52✔
47
                $cacheQuery    = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
46✔
48
                $cacheResult   = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
46✔
49
                $cacheMetadata = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
46✔
50
                break;
46✔
51

52
            case 'redis':
6✔
53
                $redis = new Redis($cacheConfig);
2✔
54
                $redis = $redis->getInstance();
2✔
55
                $redis->select($cacheConfig->redis['database']);
2✔
56
                $cacheQuery    = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl);
2✔
57
                $cacheResult   = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl);
2✔
58
                $cacheMetadata = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl);
2✔
59
                break;
2✔
60

61
            case 'memcached':
4✔
62
                $memcached     = new Memcached($cacheConfig);
2✔
63
                $cacheQuery    = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl);
2✔
64
                $cacheResult   = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl);
2✔
65
                $cacheMetadata = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl);
2✔
66
                break;
2✔
67

68
            default:
69
                $cacheQuery = $cacheResult = $cacheMetadata = new ArrayAdapter($cacheConfig->ttl);
2✔
70
        }
71

72
        $config = new Configuration();
52✔
73

74
        $config->setProxyDir($doctrineConfig->proxies);
52✔
75
        $config->setProxyNamespace($doctrineConfig->proxiesNamespace);
52✔
76
        $config->setAutoGenerateProxyClasses($doctrineConfig->setAutoGenerateProxyClasses);
52✔
77

78
        if ($doctrineConfig->queryCache) {
52✔
79
            $config->setQueryCache($cacheQuery);
52✔
80
        }
81

82
        if ($doctrineConfig->resultsCache) {
52✔
83
            $config->setResultCache($cacheResult);
52✔
84
        }
85

86
        if ($doctrineConfig->metadataCache) {
52✔
87
            $config->setMetadataCache($cacheMetadata);
52✔
88
        }
89

90
        switch ($doctrineConfig->metadataConfigurationMethod) {
52✔
91
            case 'xml':
52✔
92
                $config->setMetadataDriverImpl(new XmlDriver($doctrineConfig->entities, XmlDriver::DEFAULT_FILE_EXTENSION, $doctrineConfig->isXsdValidationEnabled));
×
93
                break;
×
94

95
            case 'attribute':
52✔
96
            default:
97
                $config->setMetadataDriverImpl(new AttributeDriver($doctrineConfig->entities));
52✔
98
                break;
52✔
99
        }
100

101
        // INTEGRACIÓN DEL COLLECTOR Y MIDDLEWARE
102
        $collector  = new DoctrineCollector();
52✔
103
        $middleware = new DoctrineQueryMiddleware($collector);
52✔
104
        if (method_exists($config, 'setMiddlewares')) {
52✔
105
            $config->setMiddlewares([$middleware]);
52✔
106
        }
107

108
        /** @var Database $dbConfig */
109
        $dbConfig = config('Database');
52✔
110
        if ($dbGroup === null) {
52✔
111
            $dbGroup = (ENVIRONMENT === 'testing') ? 'tests' : $dbConfig->defaultGroup;
50✔
112
        }
113
        // Database connection information
114
        $connectionOptions = $this->convertDbConfig($dbConfig->{$dbGroup});
52✔
115
        $connection        = DriverManager::getConnection($connectionOptions, $config);
52✔
116

117
        // Create EntityManager
118
        $this->em = new EntityManager($connection, $config);
52✔
119

120
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
52✔
121
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
52✔
122

123
        // Si la Toolbar está activa, registra el collector manualmente
124
        // (El método addCollector no existe, así que se elimina esta línea)
125
        // if (function_exists('service') && service('toolbar')) {
126
        //     service('toolbar')->addCollector($collector);
127
        // }
128
    }
129

130
    public function reOpen()
131
    {
132
        $this->em = new EntityManager($this->em->getConnection(), $this->em->getConfiguration(), $this->em->getEventManager());
2✔
133
    }
134

135
    /**
136
     * Convert CodeIgniter database config array to Doctrine's
137
     *
138
     * See http://www.codeigniter.com/user_guide/database/configuration.html
139
     * See http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
140
     *
141
     * @param object $db
142
     *
143
     * @return array
144
     *
145
     * @throws Exception
146
     */
147
    public function convertDbConfig($db)
148
    {
149
        $connectionOptions = [];
52✔
150
        $db                = (is_array($db)) ? json_decode(json_encode($db)) : $db;
52✔
151
        if ($db->DSN) {
52✔
152
            $driverMapper = ['MySQLi' => 'mysqli', 'Postgre' => 'pgsql', 'OCI8' => 'oci8', 'SQLSRV' => 'sqlsrv', 'SQLite3' => 'sqlite3'];
46✔
153
            if (str_contains($db->DSN, 'SQLite')) {
46✔
154
                $db->DSN = strtolower($db->DSN);
4✔
155
            }
156
            $dsnParser         = new DsnParser($driverMapper);
46✔
157
            $connectionOptions = $dsnParser->parse($db->DSN);
46✔
158
        } else {
159
            switch (strtolower($db->DBDriver)) {
6✔
160
                case 'sqlite3':
6✔
161
                    if ($db->database === ':memory:') {
4✔
162
                        $connectionOptions = [
2✔
163
                            'driver' => strtolower($db->DBDriver),
2✔
164
                            'memory' => true,
2✔
165
                        ];
2✔
166
                    } else {
167
                        $connectionOptions = [
2✔
168
                            'driver' => strtolower($db->DBDriver),
2✔
169
                            'path'   => $db->database,
2✔
170
                        ];
2✔
171
                    }
172
                    break;
4✔
173

174
                default:
175
                    $connectionOptions = [
2✔
176
                        'driver'   => strtolower($db->DBDriver),
2✔
177
                        'user'     => $db->username,
2✔
178
                        'password' => $db->password,
2✔
179
                        'host'     => $db->hostname,
2✔
180
                        'dbname'   => $db->database,
2✔
181
                        'charset'  => $db->charset,
2✔
182
                        'port'     => $db->port,
2✔
183
                    ];
2✔
184
                    // Soporte para SSL y opciones avanzadas
185
                    $sslOptions = ['sslmode', 'sslcert', 'sslkey', 'sslca', 'sslcapath', 'sslcipher', 'sslcrl', 'sslverify', 'sslcompression'];
2✔
186

187
                    foreach ($sslOptions as $opt) {
2✔
188
                        if (isset($db->{$opt})) {
2✔
NEW
189
                            $connectionOptions[$opt] = $db->{$opt};
×
190
                        }
191
                    }
192
                    // Opciones personalizadas
193
                    if (isset($db->options) && is_array($db->options)) {
2✔
NEW
194
                        foreach ($db->options as $key => $value) {
×
NEW
195
                            $connectionOptions[$key] = $value;
×
196
                        }
197
                    }
198
            }
199
        }
200

201
        return $connectionOptions;
52✔
202
    }
203

204
    /**
205
     * @codeCoverageIgnore
206
     *
207
     * @param mixed $db
208
     */
209
    protected function convertDbConfigPdo($db)
210
    {
211
        $connectionOptions = [];
×
212

213
        if (substr($db->hostname, 0, 7) === 'sqlite:') {
×
214
            $connectionOptions = [
×
215
                'driver'   => 'pdo_sqlite',
×
216
                'user'     => $db->username,
×
217
                'password' => $db->password,
×
218
                'path'     => preg_replace('/\Asqlite:/', '', $db->hostname),
×
219
            ];
×
220
        } elseif (substr($db->dsn, 0, 7) === 'sqlite:') {
×
221
            $connectionOptions = [
×
222
                'driver'   => 'pdo_sqlite',
×
223
                'user'     => $db->username,
×
224
                'password' => $db->password,
×
225
                'path'     => preg_replace('/\Asqlite:/', '', $db->dsn),
×
226
            ];
×
227
        } elseif (substr($db->dsn, 0, 6) === 'mysql:') {
×
228
            $connectionOptions = [
×
229
                'driver'   => 'pdo_mysql',
×
230
                'user'     => $db->username,
×
231
                'password' => $db->password,
×
232
                'host'     => $db->hostname,
×
233
                'dbname'   => $db->database,
×
234
                'charset'  => $db->charset,
×
235
                'port'     => $db->port,
×
236
            ];
×
237
        } else {
238
            throw new Exception('Your Database Configuration is not confirmed by CodeIgniter Doctrine');
×
239
        }
240

241
        return $connectionOptions;
×
242
    }
243
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc