c4eff2153b3f332b7faf6fc75c47c35e9e6bc46d
[yaffs-website] / vendor / drupal / console / src / Command / Shared / ConnectTrait.php
1 <?php
2
3 /**
4  * @file
5  * Contains Drupal\Console\Command\Shared\ConnectTrait.
6  */
7
8 namespace Drupal\Console\Command\Shared;
9
10 use Drupal\Core\Database\Database;
11
12 trait ConnectTrait
13 {
14     protected $supportedDrivers = ['mysql','pgsql'];
15
16     public function resolveConnection($database = 'default')
17     {
18         $connectionInfo = Database::getConnectionInfo();
19
20         if (!$connectionInfo || !isset($connectionInfo[$database])) {
21             $this->getIo()->error(
22                 sprintf(
23                     $this->trans('commands.database.connect.messages.database-not-found'),
24                     $database
25                 )
26             );
27
28             return null;
29         }
30
31         $databaseConnection = $connectionInfo[$database];
32         if (!in_array($databaseConnection['driver'], $this->supportedDrivers)) {
33             $this->getIo()->error(
34                 sprintf(
35                     $this->trans('commands.database.connect.messages.database-not-supported'),
36                     $databaseConnection['driver']
37                 )
38             );
39
40             return null;
41         }
42
43         return $databaseConnection;
44     }
45
46     public function getRedBeanConnection($database = 'default')
47     {
48         $connectionInfo = Database::getConnectionInfo();
49         $databaseConnection = $connectionInfo[$database];
50         if ($databaseConnection['driver'] == 'mysql') {
51             $dsn = sprintf(
52                 'mysql:host=%s;dbname=%s',
53                 $databaseConnection['host'],
54                 $databaseConnection['database']
55             );
56
57             $this->redBean->setup(
58                 $dsn,
59                 $databaseConnection['username'],
60                 $databaseConnection['password'],
61                 true
62             );
63
64             return $this->redBean;
65         }
66
67         return null;
68     }
69 }