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

aimeos / aimeos-base / d75eff4d-70b2-4a0c-9fb5-c3e263dbf45f

12 Dec 2024 01:03PM UTC coverage: 88.603%. Remained the same
d75eff4d-70b2-4a0c-9fb5-c3e263dbf45f

push

circleci

aimeos
Fixed test

1687 of 1904 relevant lines covered (88.6%)

5.98 hits per line

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

68.18
/src/DB/Manager/Standard.php
1
<?php
2

3
/**
4
 * @license LGPLv3, https://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016-2024
6
 * @package Base
7
 * @subpackage DB
8
 */
9

10

11
namespace Aimeos\Base\DB\Manager;
12

13

14
/**
15
 * Manager for database connections
16
 *
17
 * @package Base
18
 * @subpackage DB
19
 */
20
class Standard implements \Aimeos\Base\DB\Manager\Iface
21
{
22
        private array $objects = [];
23
        private array $config;
24
        private string $type;
25

26

27
        /**
28
         * Initializes the database manager object
29
         *
30
         * @param array $config Database resource configuration
31
         * @param string $type Type of the connection
32
         */
33
        public function __construct( array $config, string $type = 'PDO' )
34
        {
35
                $this->config = $config;
3✔
36
                $this->type = $type;
3✔
37
        }
38

39

40
        /**
41
         * Cleans up the object
42
         */
43
        public function __destruct()
44
        {
45
                foreach( $this->objects as $key => $conn ) {
3✔
46
                        unset( $this->objects[$key] );
2✔
47
                }
48
        }
49

50

51
        /**
52
         * Reset when cloning the object
53
         */
54
        public function __clone()
55
        {
56
                $this->objects = [];
×
57
        }
58

59

60
        /**
61
         * Clean up the objects inside
62
         *
63
         * @return array List of properties to serialize
64
         */
65
        public function __sleep() : array
66
        {
67
                $this->__destruct();
×
68
                $this->objects = [];
×
69

70
                return array_keys( get_object_vars( $this ) );
×
71
        }
72

73

74
        /**
75
         * Returns a database connection.
76
         *
77
         * @param string $name Name of the resource in configuration
78
         * @param bool $new Create a new connection instead of returning the existing one
79
         * @return \Aimeos\Base\DB\Connection\Iface
80
         */
81
        public function get( string $name = 'db', bool $new = false ) : \Aimeos\Base\DB\Connection\Iface
82
        {
83
                if( $new ) {
55✔
84
                        return $this->create( $this->config( $name ) );
1✔
85
                }
86

87
                if( !isset( $this->objects[$name] ) ) {
54✔
88
                        $this->objects[$name] = $this->create( $this->config( $name ) );
2✔
89
                }
90

91
                return $this->objects[$name];
54✔
92
        }
93

94

95
        /**
96
         * Returns the configuration for the given name
97
         *
98
         * @param string $name Name of the resource, e.g. "db" or "db-product"
99
         * @return array Configuration values
100
         * @throws \Aimeos\Base\DB\Exception If an no configuration for that name is found
101
         */
102
        protected function config( string $name ) : array
103
        {
104
                foreach( [$name, 'db'] as $dbname )
3✔
105
                {
106
                        if( isset( $this->config[$dbname] ) ) {
3✔
107
                                return $this->config[$dbname];
3✔
108
                        }
109
                }
110

111
                $msg = sprintf( 'No resource configuration for "%1$s" available', $name );
×
112
                throw new \Aimeos\Base\DB\Exception( $msg );
×
113
        }
114

115

116
        /**
117
         * Creates and returns a database connection.
118
         *
119
         * @param array $config Database connection configurations
120
         * @param string $type Type of the connection
121
         * @return \Aimeos\Base\DB\Connection\Iface Instance of a database connection
122
         * @throws \Aimeos\Base\DB\Exception if database connection class isn't found
123
         */
124
        protected function create( array $config )
125
        {
126
                $classname = '\Aimeos\Base\DB\Connection\\' . $this->type;
3✔
127

128
                if( !class_exists( $classname ) ) {
3✔
129
                        throw new \Aimeos\Base\DB\Exception( sprintf( 'Database connection "%1$s" not found', $this->type ) );
×
130
                }
131

132
                return new $classname( $config );
3✔
133
        }
134
}
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