diff --git a/module/CLI/config/cli.config.php b/module/CLI/config/cli.config.php index 2edbae99..512090c2 100644 --- a/module/CLI/config/cli.config.php +++ b/module/CLI/config/cli.config.php @@ -28,6 +28,8 @@ return [ Command\Tag\CreateTagCommand::NAME => Command\Tag\CreateTagCommand::class, Command\Tag\RenameTagCommand::NAME => Command\Tag\RenameTagCommand::class, Command\Tag\DeleteTagsCommand::NAME => Command\Tag\DeleteTagsCommand::class, + + Command\Db\CreateDatabaseCommand::NAME => Command\Db\CreateDatabaseCommand::class, ], ], diff --git a/module/CLI/config/dependencies.config.php b/module/CLI/config/dependencies.config.php index 0a90c1ff..b71598a9 100644 --- a/module/CLI/config/dependencies.config.php +++ b/module/CLI/config/dependencies.config.php @@ -10,8 +10,9 @@ use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface; use Shlinkio\Shlink\Common\Service\PreviewGenerator; use Shlinkio\Shlink\Core\Service; use Shlinkio\Shlink\Rest\Service\ApiKeyService; -use Symfony\Component\Console\Application; -use Symfony\Component\Lock; +use Symfony\Component\Console as SymfonyCli; +use Symfony\Component\Lock\Factory as Locker; +use Symfony\Component\Process\PhpExecutableFinder; use Zend\ServiceManager\AbstractFactory\ConfigAbstractFactory; use Zend\ServiceManager\Factory\InvokableFactory; @@ -19,7 +20,9 @@ return [ 'dependencies' => [ 'factories' => [ - Application::class => Factory\ApplicationFactory::class, + SymfonyCli\Application::class => Factory\ApplicationFactory::class, + SymfonyCli\Helper\ProcessHelper::class => Factory\ProcessHelperFactory::class, + PhpExecutableFinder::class => InvokableFactory::class, GeolocationDbUpdater::class => ConfigAbstractFactory::class, @@ -44,11 +47,13 @@ return [ Command\Tag\CreateTagCommand::class => ConfigAbstractFactory::class, Command\Tag\RenameTagCommand::class => ConfigAbstractFactory::class, Command\Tag\DeleteTagsCommand::class => ConfigAbstractFactory::class, + + Command\Db\CreateDatabaseCommand::class => ConfigAbstractFactory::class, ], ], ConfigAbstractFactory::class => [ - GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, Lock\Factory::class], + GeolocationDbUpdater::class => [DbUpdater::class, Reader::class, Locker::class], Command\ShortUrl\GenerateShortUrlCommand::class => [Service\UrlShortener::class, 'config.url_shortener.domain'], Command\ShortUrl\ResolveUrlCommand::class => [Service\UrlShortener::class], @@ -60,7 +65,7 @@ return [ Command\Visit\LocateVisitsCommand::class => [ Service\VisitService::class, IpLocationResolverInterface::class, - Lock\Factory::class, + Locker::class, GeolocationDbUpdater::class, ], Command\Visit\UpdateDbCommand::class => [DbUpdater::class], @@ -73,6 +78,12 @@ return [ Command\Tag\CreateTagCommand::class => [Service\Tag\TagService::class], Command\Tag\RenameTagCommand::class => [Service\Tag\TagService::class], Command\Tag\DeleteTagsCommand::class => [Service\Tag\TagService::class], + + Command\Db\CreateDatabaseCommand::class => [ + Locker::class, + SymfonyCli\Helper\ProcessHelper::class, + PhpExecutableFinder::class, + ], ], ]; diff --git a/module/CLI/src/Command/Db/CreateDatabaseCommand.php b/module/CLI/src/Command/Db/CreateDatabaseCommand.php new file mode 100644 index 00000000..ab334c40 --- /dev/null +++ b/module/CLI/src/Command/Db/CreateDatabaseCommand.php @@ -0,0 +1,61 @@ +processHelper = $processHelper; + $this->phpBinary = $phpFinder->find(false) ?: 'php'; + } + + protected function configure(): void + { + $this + ->setName(self::NAME) + ->setDescription( + 'Creates the database needed for shlink to work. It will do nothing if the database already exists' + ); + } + + protected function lockedExecute(InputInterface $input, OutputInterface $output): int + { + $this->checkDbExists(); + + $command = [$this->phpBinary, self::DOCTRINE_HELPER_SCRIPT, self::DOCTRINE_HELPER_COMMAND]; + $this->processHelper->run($output, $command); + + return ExitCodes::EXIT_SUCCESS; + } + + private function checkDbExists(): void + { + // TODO + } + + protected function getLockConfig(): LockedCommandConfig + { + return new LockedCommandConfig($this->getName(), true); + } +} diff --git a/module/CLI/src/Factory/ProcessHelperFactory.php b/module/CLI/src/Factory/ProcessHelperFactory.php new file mode 100644 index 00000000..005d513d --- /dev/null +++ b/module/CLI/src/Factory/ProcessHelperFactory.php @@ -0,0 +1,20 @@ +setHelperSet(new Helper\HelperSet([ + new Helper\FormatterHelper(), + new Helper\DebugFormatterHelper(), + ])); + + return $processHelper; + } +} diff --git a/module/CLI/test/Factory/ProcessHelperFactoryTest.php b/module/CLI/test/Factory/ProcessHelperFactoryTest.php new file mode 100644 index 00000000..5042fb73 --- /dev/null +++ b/module/CLI/test/Factory/ProcessHelperFactoryTest.php @@ -0,0 +1,29 @@ +factory = new ProcessHelperFactory(); + } + + /** @test */ + public function createsTheServiceWithTheProperSetOfHelpers(): void + { + $processHelper = ($this->factory)(); + $helperSet = $processHelper->getHelperSet(); + + $this->assertCount(2, $helperSet); + $this->assertTrue($helperSet->has('formatter')); + $this->assertTrue($helperSet->has('debug_formatter')); + } +}