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

daycry / doctrine / 9081793418

14 May 2024 03:10PM UTC coverage: 92.116% (-5.0%) from 97.143%
9081793418

push

github

daycry
Coveralls

222 of 241 relevant lines covered (92.12%)

15.11 hits per line

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

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

3
namespace Daycry\Doctrine;
4

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

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

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

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

42
        $devMode = (ENVIRONMENT == "development") ? true : false;
50✔
43

44
        switch ($cacheConfig->handler) {
50✔
45
            case 'file':
50✔
46
                $cacheQuery = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
44✔
47
                $cacheResult = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
44✔
48
                $cacheMetadata = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
44✔
49
                break;
44✔
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
            case 'memcached':
4✔
59
                $memcached = new Memcached($cacheConfig);
2✔
60
                $cacheQuery = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl);
2✔
61
                $cacheResult = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl);
2✔
62
                $cacheMetadata = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl);
2✔
63
                break;
2✔
64
            default:
65
                $cacheQuery = $cacheResult = $cacheMetadata = new ArrayAdapter($cacheConfig->ttl);
2✔
66
        }
67

68
        $config = new Configuration();
50✔
69

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

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

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

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

86
        switch ($doctrineConfig->metadataConfigurationMethod) {
50✔
87
            case 'xml':
50✔
88
                $config->setMetadataDriverImpl(new XmlDriver($doctrineConfig->entities, XmlDriver::DEFAULT_FILE_EXTENSION, $doctrineConfig->isXsdValidationEnabled));
×
89
                break;
×
90
            case 'attribute':
50✔
91
            default:
92
                $config->setMetadataDriverImpl(new AttributeDriver($doctrineConfig->entities));
50✔
93
                break;
50✔
94
        }
95

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

100
        // Database connection information
101
        $connectionOptions = $this->convertDbConfig($dbConfig->$dbGroup);
50✔
102

103
        log_message('debug', 'Doctrine: Database connection options: ' . print_r($connectionOptions, true));
50✔
104
        $connection = DriverManager::getConnection($connectionOptions, $config);
50✔
105

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

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

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

118
    /**
119
     * Convert CodeIgniter database config array to Doctrine's
120
     *
121
     * See http://www.codeigniter.com/user_guide/database/configuration.html
122
     * See http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
123
     *
124
     * @param object $db
125
     * @return array
126
     * @throws Exception
127
     *
128
     */
129

130
    public function convertDbConfig($db)
131
    {
132
        $connectionOptions = [];
50✔
133

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

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

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

143
            $dsnParser = new DsnParser($driverMapper);
44✔
144
            $connectionOptions = $dsnParser->parse($db->DSN);
44✔
145

146
        } else {
147

148
            switch(strtolower($db->DBDriver)) {
6✔
149
                case 'sqlite3':
6✔
150
                    if($db->database === ':memory:') {
4✔
151
                        $connectionOptions = [
2✔
152
                            'driver' => strtolower($db->DBDriver),
2✔
153
                            'memory' => true
2✔
154
                        ];
2✔
155
                    } else {
156
                        $connectionOptions = [
2✔
157
                            'driver' => strtolower($db->DBDriver),
2✔
158
                            'path' => $db->database
2✔
159
                        ];
2✔
160
                    }
161
                    break;
4✔
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
        }
175
        /*if ($db->DBDriver === 'pdo') {
176
            return $this->convertDbConfigPdo($db);
177
        } else {
178
            $connectionOptions = [
179
                'driver'   => strtolower($db->DBDriver),
180
                'user'     => $db->username,
181
                'password' => $db->password,
182
                'host'     => $db->hostname,
183
                'dbname'   => $db->database,
184
                'charset'  => $db->charset,
185
                'port'     => $db->port,
186
                'servicename' => $db->servicename //OCI8
187
            ];
188
        }*/
189

190
        return $connectionOptions;
50✔
191
    }
192

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

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

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