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

daycry / doctrine / 13569484215

27 Feb 2025 03:06PM UTC coverage: 56.929% (-35.2%) from 92.083%
13569484215

push

github

Jordi de la Mano
Fix: tests

152 of 267 relevant lines covered (56.93%)

12.24 hits per line

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

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

3
namespace Daycry\Doctrine;
4

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

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

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

36
        if ($cacheConfig === null) {
50✔
37
            /** @var Cache $cacheConfig */
38
            $cacheConfig = config('Cache');
42✔
39
        }
40

41
        $devMode = (ENVIRONMENT === 'development') ? true : false;
50✔
42

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

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

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

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

70
        $config = new Configuration();
50✔
71

72
        $config->setProxyDir($doctrineConfig->proxies);
50✔
73
        $config->setProxyNamespace($doctrineConfig->proxiesNamespace);
50✔
74
        $config->setAutoGenerateProxyClasses($doctrineConfig->setAutoGenerateProxyClasses);
50✔
75

76
        if ($doctrineConfig->queryCache) {
50✔
77
            $config->setQueryCache($cacheQuery);
50✔
78
        }
79

80
        if ($doctrineConfig->resultsCache) {
50✔
81
            $config->setResultCache($cacheResult);
50✔
82
        }
83

84
        if ($doctrineConfig->metadataCache) {
50✔
85
            $config->setMetadataCache($cacheMetadata);
50✔
86
        }
87

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

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

99
        /** @var Database $dbConfig */
100
        $dbConfig = config('Database');
50✔
101
        $dbGroup  = (ENVIRONMENT === 'testing') ? 'tests' : $dbConfig->defaultGroup;
50✔
102

103
        // Database connection information
104
        $connectionOptions = $this->convertDbConfig($dbConfig->{$dbGroup});
50✔
105
        $connection        = DriverManager::getConnection($connectionOptions, $config);
50✔
106

107
        // Create EntityManager
108
        $this->em = new EntityManager($connection, $config);
50✔
109

110
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
50✔
111
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
50✔
112
    }
113

114
    public function reOpen()
115
    {
116
        $this->em = new EntityManager($this->em->getConnection(), $this->em->getConfiguration(), $this->em->getEventManager());
2✔
117
    }
118

119
    /**
120
     * Convert CodeIgniter database config array to Doctrine's
121
     *
122
     * See http://www.codeigniter.com/user_guide/database/configuration.html
123
     * See http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
124
     *
125
     * @param object $db
126
     *
127
     * @return array
128
     *
129
     * @throws Exception
130
     */
131
    public function convertDbConfig($db)
132
    {
133
        $connectionOptions = [];
50✔
134

135
        $db = (is_array($db)) ? json_decode(json_encode($db)) : $db;
50✔
136

137
        if ($db->DSN) {
50✔
138
            $driverMapper = ['MySQLi' => 'mysqli', 'Postgre' => 'pgsql', 'OCI8' => 'oci8', 'SQLSRV' => 'sqlsrv', 'SQLite3' => 'sqlite3'];
44✔
139

140
            if (str_contains($db->DSN, 'SQLite')) {
44✔
141
                $db->DSN = strtolower($db->DSN);
4✔
142
            }
143

144
            $dsnParser         = new DsnParser($driverMapper);
44✔
145
            $connectionOptions = $dsnParser->parse($db->DSN);
44✔
146
        } else {
147
            switch (strtolower($db->DBDriver)) {
6✔
148
                case 'sqlite3':
6✔
149
                    if ($db->database === ':memory:') {
4✔
150
                        $connectionOptions = [
2✔
151
                            'driver' => strtolower($db->DBDriver),
2✔
152
                            'memory' => true,
2✔
153
                        ];
2✔
154
                    } else {
155
                        $connectionOptions = [
2✔
156
                            'driver' => strtolower($db->DBDriver),
2✔
157
                            'path'   => $db->database,
2✔
158
                        ];
2✔
159
                    }
160
                    break;
4✔
161

162
                default:
163
                    $connectionOptions = [
2✔
164
                        'driver'   => strtolower($db->DBDriver),
2✔
165
                        'user'     => $db->username,
2✔
166
                        'password' => $db->password,
2✔
167
                        'host'     => $db->hostname,
2✔
168
                        'dbname'   => $db->database,
2✔
169
                        'charset'  => $db->charset,
2✔
170
                        'port'     => $db->port,
2✔
171
                    ];
2✔
172
            }
173
        }
174
        /*if ($db->DBDriver === 'pdo') {
175
            return $this->convertDbConfigPdo($db);
176
        } else {
177
            $connectionOptions = [
178
                'driver'   => strtolower($db->DBDriver),
179
                'user'     => $db->username,
180
                'password' => $db->password,
181
                'host'     => $db->hostname,
182
                'dbname'   => $db->database,
183
                'charset'  => $db->charset,
184
                'port'     => $db->port,
185
                'servicename' => $db->servicename //OCI8
186
            ];
187
        }*/
188

189
        return $connectionOptions;
50✔
190
    }
191

192
    /**
193
     * @codeCoverageIgnore
194
     *
195
     * @param mixed $db
196
     */
197
    protected function convertDbConfigPdo($db)
198
    {
199
        $connectionOptions = [];
×
200

201
        if (substr($db->hostname, 0, 7) === 'sqlite:') {
×
202
            $connectionOptions = [
×
203
                'driver'   => 'pdo_sqlite',
×
204
                'user'     => $db->username,
×
205
                'password' => $db->password,
×
206
                'path'     => preg_replace('/\Asqlite:/', '', $db->hostname),
×
207
            ];
×
208
        } elseif (substr($db->dsn, 0, 7) === 'sqlite:') {
×
209
            $connectionOptions = [
×
210
                'driver'   => 'pdo_sqlite',
×
211
                'user'     => $db->username,
×
212
                'password' => $db->password,
×
213
                'path'     => preg_replace('/\Asqlite:/', '', $db->dsn),
×
214
            ];
×
215
        } elseif (substr($db->dsn, 0, 6) === 'mysql:') {
×
216
            $connectionOptions = [
×
217
                'driver'   => 'pdo_mysql',
×
218
                'user'     => $db->username,
×
219
                'password' => $db->password,
×
220
                'host'     => $db->hostname,
×
221
                'dbname'   => $db->database,
×
222
                'charset'  => $db->charset,
×
223
                'port'     => $db->port,
×
224
            ];
×
225
        } else {
226
            throw new Exception('Your Database Configuration is not confirmed by CodeIgniter Doctrine');
×
227
        }
228

229
        return $connectionOptions;
×
230
    }
231
}
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

© 2026 Coveralls, Inc