Skip to content

Commit

Permalink
Add setup command
Browse files Browse the repository at this point in the history
  • Loading branch information
odan committed Jul 31, 2022
1 parent 848d344 commit 59031f4
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 92 deletions.
83 changes: 0 additions & 83 deletions bin/setup.php

This file was deleted.

1 change: 1 addition & 0 deletions config/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
// Console commands
$settings['commands'] = [
\App\Console\ExampleCommand::class,
\App\Console\SetupCommand::class,
];

return $settings;
12 changes: 3 additions & 9 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ nav_order: 1

# Installation

Run this command from the directory in which you want to install your new
Slim Framework application.

**Step 1:** Create a new project:

```shell
Expand All @@ -28,23 +25,20 @@ sudo chmod -R 760 tmp/
sudo chmod -R 760 logs/

chmod +x bin/console.php
chmod +x bin/setup.php
```

**Step 3:** Setup database

Start the setup script and follow the instructions:

```bash
php bin/setup.php
php bin/console.php setup
```

**Step 4:** Start

To start the internal webserver, run:
**Step 4:** Start the internal webserver

```
composer start
```

Then navigate to: `http://localhost:8080/`
Then navigate to: <http://localhost:8080/>
116 changes: 116 additions & 0 deletions src/Console/SetupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace App\Console;

use Exception;
use PDO;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

final class SetupCommand extends Command
{
protected function configure(): void
{
parent::configure();

$this->setName('setup');
$this->setDescription('Configuration and database installation');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('<info>**** Slim Setup ****</info>');

if (file_exists(__DIR__ . '/../../config/env.php')) {
$output->writeln('<error>The file config/env.php already exists.</error>');

return 1;
}

$dbHost = readline('Enter the database hostname or ip address [127.0.0.1]:') ?: '127.0.0.1';
$dbPort = readline('Enter the database port [3306]:') ?: '3306';
$dbName = readline('Enter the DEV database name [slim_example]:') ?: 'slim_example';
$dbNameTest = readline('Enter the TEST database name [slim_example_test]:') ?: 'slim_example_test';
$dbUsername = readline('Enter the database username [root]:') ?: 'root';
$dbPassword = readline('Enter the database password [empty]:') ?: '';

$pdoOptions = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
];

try {
$output->writeln('Open TEST database connection');
$pdoTest = new PDO("mysql:host=$dbHost;charset=utf8mb4", $dbUsername, $dbPassword, $pdoOptions);

$output->writeln('Create TEST database');
$pdoTest->exec(
"CREATE DATABASE IF NOT EXISTS `$dbNameTest` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
);

$pdoTest = null;
} catch (Exception $exception) {
$output->writeln('<error>TEST database connection failed.</error>');
$output->writeln($exception->getMessage());

return 1;
}

$output->writeln('<info>TEST database connection successfully</info>');

try {
$output->writeln('Connect to DEV database server');
$pdo = new PDO("mysql:host=$dbHost;charset=utf8mb4", $dbUsername, $dbPassword, $pdoOptions);

$output->writeln('Create DEV database');
$pdo->exec("CREATE DATABASE IF NOT EXISTS `$dbName` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;");
$pdo->exec("USE `$dbName`;");
} catch (Exception $exception) {
$output->writeln('<error>DEV database connection failed.</error>');
$output->writeln($exception->getMessage());

return 1;
}

$output->writeln('<info>DEV database created successfully</info>');

$output->writeln('Import database schema');
$pdo->exec((string)file_get_contents(__DIR__ . '/../../resources/schema/schema.sql'));
$pdo = null;

$output->writeln('Create config/env.php file');

$code = [
'<?php',
'',
'// Secret credentials',
'',
'return function (array $settings): array {',
'',
" \$settings['db']['host'] = '$dbHost';",
" \$settings['db']['port'] = '$dbPort';",
" \$settings['db']['username'] = '$dbUsername';",
" \$settings['db']['password'] = '$dbPassword';",
'',
" if (defined('PHPUNIT_COMPOSER_INSTALL')) {",
' // PHPUnit test database',
" \$settings['db']['database'] = '$dbNameTest';",
' } else {',
' // Local dev database',
" \$settings['db']['database'] = '$dbName';",
' }',
'',
' return $settings;',
'};',
'',
];

file_put_contents(__DIR__ . '/../../config/env.php', implode("\n", $code));

$output->writeln('<info>Setup was successfully!</info>');
$output->writeln('To start all tests, run: composer test');
$output->writeln('To start the internal webserver, run: composer start');

return 0;
}
}

0 comments on commit 59031f4

Please sign in to comment.