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

daycry / doctrine / 8139183062

04 Mar 2024 11:07AM UTC coverage: 97.143% (+0.9%) from 96.262%
8139183062

push

github

web-flow
Update phpunit.yml

204 of 210 relevant lines covered (97.14%)

15.56 hits per line

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

95.38
/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 Symfony\Component\Cache\Adapter\RedisAdapter;
16
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
17
use Symfony\Component\Cache\Adapter\ArrayAdapter;
18
use Exception;
19

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

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

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

40
        $db = \Config\Database::connect();
38✔
41

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

44
        
45
        switch ($cacheConfig->handler) {
38✔
46
            case 'file':
38✔
47

48
                
49
                $cacheQuery = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
32✔
50
                $cacheResult = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
32✔
51
                $cacheMetadata = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
32✔
52
                break;
32✔
53
            case 'redis':
6✔
54
                $redis = new Redis($cacheConfig);
2✔
55
                $redis = $redis->getInstance();
2✔
56
                $redis->select($cacheConfig->redis[ 'database' ]);
2✔
57
                $cacheQuery = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl);
2✔
58
                $cacheResult = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl);
2✔
59
                $cacheMetadata = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl);
2✔
60
                break;
2✔
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
            default:
68
                $cacheQuery = $cacheResult = $cacheMetadata = new ArrayAdapter($cacheConfig->ttl);
2✔
69
        }
70

71
        $dataConfig = [$doctrineConfig->entities, $devMode, $doctrineConfig->proxies, null];
38✔
72

73
        $config = new Configuration();
38✔
74

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

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

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

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

91
        switch ($doctrineConfig->metadataConfigurationMethod) {
38✔
92
            case 'xml':
38✔
93
                $config->setMetadataDriverImpl(new XmlDriver($doctrineConfig->entities, XmlDriver::DEFAULT_FILE_EXTENSION, $doctrineConfig->isXsdValidationEnabled));
×
94
                break;
×
95
            case 'attribute':
38✔
96
                default:
97
                $config->setMetadataDriverImpl(new AttributeDriver($doctrineConfig->entities));
38✔
98
                break;
38✔
99
        }
100

101
        // Database connection information
102
        $connectionOptions = $this->convertDbConfig($db);
38✔
103

104
        $connection = DriverManager::getConnection($connectionOptions, $config);
38✔
105

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

109
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
38✔
110
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
38✔
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 = [];
38✔
133

134
        if ($db->DBDriver === 'pdo') {
38✔
135
            return $this->convertDbConfigPdo($db);
×
136
        } else {
137
            $connectionOptions = [
38✔
138
                'driver'   => strtolower($db->DBDriver),
38✔
139
                'user'     => $db->username,
38✔
140
                'password' => $db->password,
38✔
141
                'host'     => $db->hostname,
38✔
142
                'dbname'   => $db->database,
38✔
143
                'charset'  => $db->charset,
38✔
144
                'port'     => $db->port,
38✔
145
                'servicename' => $db->servicename //OCI8
38✔
146
            ];
38✔
147
        }
148

149
        return $connectionOptions;
38✔
150
    }
151

152
    /**
153
     * @codeCoverageIgnore
154
     */
155
    protected function convertDbConfigPdo($db)
156
    {
157
        $connectionOptions = [];
158

159
        if (substr($db->hostname, 0, 7) === 'sqlite:') {
160
            $connectionOptions = [
161
                'driver'   => 'pdo_sqlite',
162
                'user'     => $db->username,
163
                'password' => $db->password,
164
                'path'     => preg_replace('/\Asqlite:/', '', $db->hostname),
165
            ];
166
        } elseif (substr($db->dsn, 0, 7) === 'sqlite:') {
167
            $connectionOptions = [
168
                'driver'   => 'pdo_sqlite',
169
                'user'     => $db->username,
170
                'password' => $db->password,
171
                'path'     => preg_replace('/\Asqlite:/', '', $db->dsn),
172
            ];
173
        } elseif (substr($db->dsn, 0, 6) === 'mysql:') {
174
            $connectionOptions = [
175
                'driver'   => 'pdo_mysql',
176
                'user'     => $db->username,
177
                'password' => $db->password,
178
                'host'     => $db->hostname,
179
                'dbname'   => $db->database,
180
                'charset'  => $db->charset,
181
                'port'     => $db->port
182
            ];
183
        } else {
184
            throw new Exception('Your Database Configuration is not confirmed by CodeIgniter Doctrine');
185
        }
186

187
        return $connectionOptions;
188
    }
189
}
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