|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MakerBundle\Maker; |
| 13 | + |
| 14 | +use Symfony\Bundle\MakerBundle\ConsoleStyle; |
| 15 | +use Symfony\Bundle\MakerBundle\DependencyBuilder; |
| 16 | +use Symfony\Bundle\MakerBundle\Docker\DockerDatabaseServices; |
| 17 | +use Symfony\Bundle\MakerBundle\FileManager; |
| 18 | +use Symfony\Bundle\MakerBundle\Generator; |
| 19 | +use Symfony\Bundle\MakerBundle\InputConfiguration; |
| 20 | +use Symfony\Bundle\MakerBundle\Util\ComposeFileManipulator; |
| 21 | +use Symfony\Bundle\MakerBundle\Validator; |
| 22 | +use Symfony\Component\Console\Command\Command; |
| 23 | +use Symfony\Component\Console\Input\InputInterface; |
| 24 | +use Symfony\Component\Yaml\Yaml; |
| 25 | + |
| 26 | +/** |
| 27 | + * @author Jesse Rushlow <[email protected]> |
| 28 | + * |
| 29 | + * @internal |
| 30 | + */ |
| 31 | +final class MakeDockerDatabase extends AbstractMaker |
| 32 | +{ |
| 33 | + private $fileManager; |
| 34 | + private $composeFilePath; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var ComposeFileManipulator |
| 38 | + */ |
| 39 | + private $composeFileManipulator; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var string type of database selected by the user |
| 43 | + */ |
| 44 | + private $databaseChoice; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var string Service identifier to be set in docker-compose.yaml |
| 48 | + */ |
| 49 | + private $serviceName = 'database'; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var string Version set in docker-compose.yaml for the service. e.g. latest |
| 53 | + */ |
| 54 | + private $serviceVersion = 'latest'; |
| 55 | + |
| 56 | + public function __construct(FileManager $fileManager) |
| 57 | + { |
| 58 | + $this->fileManager = $fileManager; |
| 59 | + $this->composeFilePath = sprintf('%s/docker-compose.yaml', $this->fileManager->getRootDirectory()); |
| 60 | + } |
| 61 | + |
| 62 | + public static function getCommandName(): string |
| 63 | + { |
| 64 | + return 'make:docker:database'; |
| 65 | + } |
| 66 | + |
| 67 | + public function configureCommand(Command $command, InputConfiguration $inputConfig): void |
| 68 | + { |
| 69 | + $command |
| 70 | + ->setDescription('Adds a database container to your docker-compose.yaml file.') |
| 71 | + ; |
| 72 | + } |
| 73 | + |
| 74 | + public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void |
| 75 | + { |
| 76 | + $io->section('- Docker Compose Setup-'); |
| 77 | + |
| 78 | + $composeFileContents = ''; |
| 79 | + $statusMessage = 'Existing docker-compose.yaml not found: a new one will be generated!'; |
| 80 | + |
| 81 | + if ($this->fileManager->fileExists($this->composeFilePath)) { |
| 82 | + $composeFileContents = $this->fileManager->getFileContents($this->composeFilePath); |
| 83 | + |
| 84 | + $statusMessage = 'We found your existing docker-compose.yaml: Let\'s update it!'; |
| 85 | + } |
| 86 | + |
| 87 | + $io->text($statusMessage); |
| 88 | + |
| 89 | + $this->composeFileManipulator = new ComposeFileManipulator($composeFileContents); |
| 90 | + |
| 91 | + $io->newLine(); |
| 92 | + |
| 93 | + $this->databaseChoice = strtolower($io->choice( |
| 94 | + 'Which database service will you be creating?', |
| 95 | + ['MySQL', 'MariaDB', 'Postgres'] |
| 96 | + )); |
| 97 | + |
| 98 | + $io->text([sprintf( |
| 99 | + 'For a list of supported versions, check out https://hub.docker.com/_/%s', |
| 100 | + $this->databaseChoice |
| 101 | + )]); |
| 102 | + |
| 103 | + $this->serviceVersion = $io->ask('What version would you like to use?', DockerDatabaseServices::getSuggestedServiceVersion($this->databaseChoice)); |
| 104 | + |
| 105 | + if ($this->composeFileManipulator->serviceExists($this->serviceName)) { |
| 106 | + $io->comment(sprintf('A <fg=yellow>"%s"</> service is already defined.', $this->serviceName)); |
| 107 | + $io->newLine(); |
| 108 | + |
| 109 | + $serviceNameMsg[] = 'If you are using the Symfony Binary, it will expose the connection config for'; |
| 110 | + $serviceNameMsg[] = 'this service as environment variables. The name of the service determines the'; |
| 111 | + $serviceNameMsg[] = 'name of those environment variables.'; |
| 112 | + $serviceNameMsg[] = ''; |
| 113 | + $serviceNameMsg[] = 'For example, if you name the service <fg=yellow>database_alt</>, the binary will expose a'; |
| 114 | + $serviceNameMsg[] = '<fg=yellow>DATABASE_ALT_URL</> environment variable.'; |
| 115 | + |
| 116 | + $io->text($serviceNameMsg); |
| 117 | + |
| 118 | + $this->serviceName = $io->ask(sprintf('What name should we call the new %s service? e.g. database', $this->serviceName), null, [Validator::class, 'notBlank']); |
| 119 | + } |
| 120 | + |
| 121 | + $this->checkForPDOSupport($this->databaseChoice, $io); |
| 122 | + } |
| 123 | + |
| 124 | + public function generate(InputInterface $input, ConsoleStyle $io, Generator $generator): void |
| 125 | + { |
| 126 | + $io->newLine(); |
| 127 | + |
| 128 | + $service = DockerDatabaseServices::getDatabaseSkeleton($this->databaseChoice, $this->serviceVersion); |
| 129 | + |
| 130 | + $this->composeFileManipulator->addDockerService($this->serviceName, $service); |
| 131 | + $this->composeFileManipulator->exposePorts($this->serviceName, DockerDatabaseServices::getDefaultPorts($this->databaseChoice)); |
| 132 | + |
| 133 | + $generator->dumpFile($this->composeFilePath, $this->composeFileManipulator->getDataString()); |
| 134 | + $generator->writeChanges(); |
| 135 | + |
| 136 | + $this->writeSuccessMessage($io); |
| 137 | + |
| 138 | + $io->text(sprintf('The new <fg=yellow>"%s"</> service is now ready!', $this->serviceName)); |
| 139 | + $io->newLine(); |
| 140 | + |
| 141 | + $ports = DockerDatabaseServices::getDefaultPorts($this->databaseChoice); |
| 142 | + $closing[] = 'Next:'; |
| 143 | + $closing[] = sprintf(' A) Run <fg=yellow>docker-compose up -d %s</> to start your database container', $this->serviceName); |
| 144 | + $closing[] = sprintf(' or <fg=yellow>docker-compose up -d</> to start all of them.'); |
| 145 | + $closing[] = ''; |
| 146 | + $closing[] = ' B) If you are using the Symfony Binary, it will detect the new service automatically.'; |
| 147 | + $closing[] = ' Run <fg=yellow>symfony var:export --multiline</> to see the environment variables the binary is exposing.'; |
| 148 | + $closing[] = ' These will override any values you have in your .env files.'; |
| 149 | + $closing[] = ''; |
| 150 | + $closing[] = ' C) Run <fg=yellow>docker-compose stop</> will stop all the containers in docker-compose.yaml.'; |
| 151 | + $closing[] = ' <fg=yellow>docker-compose down</> will stop and destroy the containers.'; |
| 152 | + $closing[] = ''; |
| 153 | + $closing[] = sprintf( |
| 154 | + 'Port%s %s will be exposed to %s random port%s on your host machine.', |
| 155 | + 1 === \count($ports) ? '' : 's', |
| 156 | + implode(' ', $ports), |
| 157 | + 1 === \count($ports) ? 'a' : '', |
| 158 | + 1 === \count($ports) ? '' : 's' |
| 159 | + ); |
| 160 | + |
| 161 | + $io->text($closing); |
| 162 | + $io->newLine(); |
| 163 | + } |
| 164 | + |
| 165 | + public function configureDependencies(DependencyBuilder $dependencies): void |
| 166 | + { |
| 167 | + $dependencies->addClassDependency( |
| 168 | + Yaml::class, |
| 169 | + 'yaml' |
| 170 | + ); |
| 171 | + } |
| 172 | + |
| 173 | + private function checkForPDOSupport(string $databaseType, ConsoleStyle $io): void |
| 174 | + { |
| 175 | + $extension = DockerDatabaseServices::getMissingExtensionName($databaseType); |
| 176 | + |
| 177 | + if (null !== $extension) { |
| 178 | + $io->note( |
| 179 | + sprintf('Cannot find PHP\'s pdo_%s extension. Be sure it\'s installed & enabled to talk to the database.', $extension) |
| 180 | + ); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments