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

daycry / doctrine / 5878638528

31 Jul 2023 08:19AM UTC coverage: 96.262%. Remained the same
5878638528

push

github

daycry
fix composer

206 of 214 relevant lines covered (96.26%)

7.85 hits per line

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

92.75
/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\ORMSetup;
9
use Doctrine\ORM\Configuration;
10
use Doctrine\ORM\EntityManager;
11
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
12
use Doctrine\ORM\Mapping\Driver\XmlDriver;
13
use Doctrine\ORM\Mapping\Driver\YamlDriver;
14
use Symfony\Component\Cache\Adapter\PhpFilesAdapter;
15
use Daycry\Doctrine\Libraries\Redis;
16
use Daycry\Doctrine\Libraries\Memcached;
17
use Symfony\Component\Cache\Adapter\RedisAdapter;
18
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
19
use Symfony\Component\Cache\Adapter\ArrayAdapter;
20
use Exception;
21

22
/**
23
 * Class General
24
 *
25
 */
26
class Doctrine
27
{
28
    public $em = null;
29
    private $cache;
30
    
31
    public function __construct(DoctrineConfig $doctrineConfig = null, Cache $cacheConfig = null)
32
    {
33
        if ($doctrineConfig === NULL) {
20✔
34
            $doctrineConfig = config('Doctrine');
1✔
35
        }
36

37
        if ($cacheConfig === NULL) {
20✔
38
            $cacheConfig = config('Cache');
16✔
39
        }
40

41
        $db = \Config\Database::connect();
20✔
42

43
        $devMode = (ENVIRONMENT == "development") ? true : false;
20✔
44

45
        switch ($cacheConfig->handler) {
20✔
46
            case 'file':
20✔
47
                $cacheQuery = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
17✔
48
                $cacheResult = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
17✔
49
                $cacheMetadata = new PhpFilesAdapter($cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl, $cacheConfig->file['storePath'] . DIRECTORY_SEPARATOR . 'doctrine');
17✔
50
                break;
17✔
51
            case 'redis':
3✔
52
                $redis = new Redis($cacheConfig);
1✔
53
                $redis = $redis->getInstance();
1✔
54
                $redis->select($cacheConfig->redis[ 'database' ]);
1✔
55
                $cacheQuery = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl);
1✔
56
                $cacheResult = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl);
1✔
57
                $cacheMetadata = new RedisAdapter($redis, $cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl);
1✔
58
                break;
1✔
59
            case 'memcached':
2✔
60
                $memcached = new Memcached($cacheConfig);
1✔
61
                $cacheQuery = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->queryCacheNamespace, $cacheConfig->ttl);
1✔
62
                $cacheResult = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->resultsCacheNamespace, $cacheConfig->ttl);
1✔
63
                $cacheMetadata = new MemcachedAdapter($memcached->getInstance(), $cacheConfig->prefix . $doctrineConfig->metadataCacheNamespace, $cacheConfig->ttl);
1✔
64
                break;
1✔
65
            default:
66
                $cacheQuery = $cacheResult = $cacheMetadata = new ArrayAdapter($cacheConfig->ttl);
1✔
67
        }
68

69
        $dataConfig = [$doctrineConfig->entities, $devMode, $doctrineConfig->proxies, null];
20✔
70

71
        $config = new Configuration();
20✔
72

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

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

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

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

92
        switch ($doctrineConfig->metadataConfigurationMethod) {
20✔
93
            case 'yaml':
20✔
94
                $config->setMetadataDriverImpl(new YamlDriver($doctrineConfig->entities));
×
95
                break;
×
96
            case 'xml':
20✔
97
                $config->setMetadataDriverImpl(new XmlDriver($doctrineConfig->entities, XmlDriver::DEFAULT_FILE_EXTENSION, $doctrineConfig->isXsdValidationEnabled));
×
98
                break;
×
99
            case 'attribute':
20✔
100
                $config->setMetadataDriverImpl(new AttributeDriver($doctrineConfig->entities));
1✔
101
                break;
1✔
102
            default:
103
            $config->setMetadataDriverImpl(ORMSetup::createDefaultAnnotationDriver($doctrineConfig->entities));
19✔
104
        }
105

106
        // Database connection information
107
        $connectionOptions = $this->convertDbConfig($db);
20✔
108

109
        $connection = DriverManager::getConnection($connectionOptions, $config);
20✔
110
        
111
        // Create EntityManager
112
        $this->em = new EntityManager($connection, $config);
20✔
113

114
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('set', 'string');
20✔
115
        $this->em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string');
20✔
116
    }
117

118
    public function reOpen()
119
    {
120
        $this->em = new EntityManager($this->em->getConnection(), $this->em->getConfiguration(), $this->em->getEventManager());
1✔
121
    }
122

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

135
    public function convertDbConfig($db)
136
    {
137
        $connectionOptions = [];
20✔
138

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

154
        return $connectionOptions;
20✔
155
    }
156

157
    /**
158
     * @codeCoverageIgnore
159
     */
160
    protected function convertDbConfigPdo($db)
161
    {
162
        $connectionOptions = [];
163

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

192
        return $connectionOptions;
193
    }
194
}
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