| @@ 12-72 (lines=61) @@ | ||
| 9 | use Symfony\Component\Console\Input\InputOption; |
|
| 10 | use Symfony\Component\Console\Output\OutputInterface; |
|
| 11 | ||
| 12 | class CreateDatabaseCommand extends Command |
|
| 13 | { |
|
| 14 | const COMMAND_NAME = 'orm:database:create'; |
|
| 15 | ||
| 16 | const OPTION_IF_NOT_EXISTS = 'if-not-exists'; |
|
| 17 | ||
| 18 | /** @var Connection */ |
|
| 19 | private $connection; |
|
| 20 | ||
| 21 | /** @var string */ |
|
| 22 | private $name; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
|
| 26 | */ |
|
| 27 | public function __construct( |
|
| 28 | string $host, |
|
| 29 | string $port, |
|
| 30 | string $name, |
|
| 31 | string $user, |
|
| 32 | string $pass |
|
| 33 | ) { |
|
| 34 | $this->connection = DriverManager::getConnection( |
|
| 35 | [ |
|
| 36 | 'driver' => 'pdo_mysql', |
|
| 37 | 'host' => $host, |
|
| 38 | 'port' => $port, |
|
| 39 | 'user' => $user, |
|
| 40 | 'password' => $pass, |
|
| 41 | ] |
|
| 42 | ); |
|
| 43 | ||
| 44 | $this->name = $name; |
|
| 45 | ||
| 46 | parent::__construct(); |
|
| 47 | } |
|
| 48 | ||
| 49 | protected function configure() |
|
| 50 | { |
|
| 51 | $this |
|
| 52 | ->setName(self::COMMAND_NAME) |
|
| 53 | ->setDescription('Creates the configured database') |
|
| 54 | ->addOption( |
|
| 55 | self::OPTION_IF_NOT_EXISTS, |
|
| 56 | null, |
|
| 57 | InputOption::VALUE_NONE, |
|
| 58 | 'Don\'t trigger an error, when the database already exists' |
|
| 59 | ) |
|
| 60 | ; |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
| 65 | */ |
|
| 66 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 67 | { |
|
| 68 | if ($input->getOption(self::OPTION_IF_NOT_EXISTS) |
|
| 69 | && in_array($this->name, $this->connection->getSchemaManager()->listDatabases()) |
|
| 70 | ) { |
|
| 71 | return; |
|
| 72 | } |
|
| 73 | ||
| 74 | $this->connection->getSchemaManager()->createDatabase($this->name); |
|
| 75 | ||
| @@ 12-72 (lines=61) @@ | ||
| 9 | use Symfony\Component\Console\Input\InputOption; |
|
| 10 | use Symfony\Component\Console\Output\OutputInterface; |
|
| 11 | ||
| 12 | class DropDatabaseCommand extends Command |
|
| 13 | { |
|
| 14 | const COMMAND_NAME = 'orm:database:drop'; |
|
| 15 | ||
| 16 | const OPTION_IF_EXISTS = 'if-exists'; |
|
| 17 | ||
| 18 | /** @var Connection */ |
|
| 19 | private $connection; |
|
| 20 | ||
| 21 | /** @var string */ |
|
| 22 | private $name; |
|
| 23 | ||
| 24 | /** |
|
| 25 | * @SuppressWarnings(PHPMD.ExcessiveParameterList) |
|
| 26 | */ |
|
| 27 | public function __construct( |
|
| 28 | string $host, |
|
| 29 | string $port, |
|
| 30 | string $name, |
|
| 31 | string $user, |
|
| 32 | string $pass |
|
| 33 | ) { |
|
| 34 | $this->connection = DriverManager::getConnection( |
|
| 35 | [ |
|
| 36 | 'driver' => 'pdo_mysql', |
|
| 37 | 'host' => $host, |
|
| 38 | 'port' => $port, |
|
| 39 | 'user' => $user, |
|
| 40 | 'password' => $pass, |
|
| 41 | ] |
|
| 42 | ); |
|
| 43 | ||
| 44 | $this->name = $name; |
|
| 45 | ||
| 46 | parent::__construct(); |
|
| 47 | } |
|
| 48 | ||
| 49 | protected function configure() |
|
| 50 | { |
|
| 51 | $this |
|
| 52 | ->setName(self::COMMAND_NAME) |
|
| 53 | ->setDescription('Drop the configured database') |
|
| 54 | ->addOption( |
|
| 55 | self::OPTION_IF_EXISTS, |
|
| 56 | null, |
|
| 57 | InputOption::VALUE_NONE, |
|
| 58 | 'Don\'t trigger an error, when the database does not exist' |
|
| 59 | ) |
|
| 60 | ; |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
|
| 65 | */ |
|
| 66 | protected function execute(InputInterface $input, OutputInterface $output) |
|
| 67 | { |
|
| 68 | if ($input->getOption(self::OPTION_IF_EXISTS) |
|
| 69 | && !in_array($this->name, $this->connection->getSchemaManager()->listDatabases()) |
|
| 70 | ) { |
|
| 71 | return; |
|
| 72 | } |
|
| 73 | ||
| 74 | $this->connection->getSchemaManager()->dropDatabase($this->name); |
|
| 75 | ||