More tidying.
[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\Console\Core\Style\DrupalStyle;
11 use Drupal\Core\Database\Database;
12
13 trait ConnectTrait
14 {
15     protected $supportedDrivers = ['mysql','pgsql'];
16
17     public function resolveConnection(DrupalStyle $io, $database = 'default')
18     {
19         $connectionInfo = Database::getConnectionInfo();
20
21         if (!$connectionInfo || !isset($connectionInfo[$database])) {
22             $io->error(
23                 sprintf(
24                     $this->trans('commands.database.connect.messages.database-not-found'),
25                     $database
26                 )
27             );
28
29             return null;
30         }
31
32         $databaseConnection = $connectionInfo[$database];
33         if (!in_array($databaseConnection['driver'], $this->supportedDrivers)) {
34             $io->error(
35                 sprintf(
36                     $this->trans('commands.database.connect.messages.database-not-supported'),
37                     $databaseConnection['driver']
38                 )
39             );
40
41             return null;
42         }
43
44         return $databaseConnection;
45     }
46
47     public function getRedBeanConnection($database = 'default')
48     {
49         $connectionInfo = Database::getConnectionInfo();
50         $databaseConnection = $connectionInfo[$database];
51         if ($databaseConnection['driver'] == 'mysql') {
52             $dsn = sprintf(
53                 'mysql:host=%s;dbname=%s',
54                 $databaseConnection['host'],
55                 $databaseConnection['database']
56             );
57
58             $this->redBean->setup(
59                 $dsn,
60                 $databaseConnection['username'],
61                 $databaseConnection['password'],
62                 true
63             );
64
65             return $this->redBean;
66         }
67
68         return null;
69     }
70 }